Foro de elhacker.net

Programación => Java => Mensaje iniciado por: NetJava en 23 Mayo 2011, 12:17 pm



Título: [SOLUCIONADO] JExcel, con bucle para escribir.
Publicado por: NetJava en 23 Mayo 2011, 12:17 pm
Buenas,
Me he puesto a estudiar un poco la libr JExcel y me he encontrado con un problema al hacer un ejemplo... como casi siempre... XD

Código
  1. //Los import todos ok.
  2. public class escribir {
  3.  
  4. private WritableWorkbook libro;
  5. private WritableSheet hoja;
  6. private int c = 0;
  7.  
  8. public escribir(){
  9. try{
  10. libro = Workbook.createWorkbook(new File("ejemplo.xls"));
  11. hoja = libro.createSheet("Hoja1", 0);
  12. }catch(Exception e){
  13. JOptionPane.showMessageDialog(null,"Error Escribir/Constructor.","Message",JOptionPane.INFORMATION_MESSAGE);
  14. }
  15. }
  16.  
  17. public void escribircontenido(String dato){
  18. try{
  19.  
  20.    Label label = new Label(0,c,dato);
  21.    c+=1;
  22.    hoja.addCell(label);
  23.    libro.write();
  24.    if(c == 9){libro.close();}
  25.  
  26. }catch(Exception e){
  27. JOptionPane.showMessageDialog(null,"Error Escribir contenido.","Message",JOptionPane.INFORMATION_MESSAGE);
  28. }
  29. }
  30. }
  31.  

En otro lugar hay un bucle que va llamando al método "escribircontenido", pasando el valor a escribir y ya esta... debería de funcionar llenándome un excel con 10 valores, cada uno en una fila de una misma columna,  pero no es así... solo me escribe el primer valor en la posición (0,0) y ya esta. Debe ser algo simple, pero no caigo XD

Saludos y gracias


Título: Re:[Solucionado] JExcel, con bucle para escribir.
Publicado por: NetJava en 25 Mayo 2011, 14:06 pm
Buenas gente,

El problema ya esta solucionado, pongo el ejemplo. Únicamente hay que tener en cuenta que si el bucle es de por ejemplo 90000, por algún motivo no furula...

Dejo el ejemplo que hice.

Código
  1.  
  2. package PackExcel3;
  3.  
  4. import java.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.io.*;
  8. import javax.*;
  9. import javax.swing.*;
  10. import javax.swing.event.*;
  11.  
  12. import jxl.*;
  13. import jxl.LabelCell; //Para leer de la celda, no es necesario hasta que no se lea del excel.
  14. import jxl.Workbook.*;
  15. import jxl.write.*;
  16. import jxl.write.Label;
  17. import jxl.write.Number;
  18. import jxl.write.WritableWorkbook.*;
  19. import jxl.write.WritableSheet.*;;
  20.  
  21.  
  22. public class interfaz extends JFrame implements ActionListener{
  23.  
  24. WritableWorkbook libro;
  25. WritableSheet hoja;
  26. private JLabel jlbl_inf = new JLabel("Estado 0");
  27. private JLabel jlbl_inf1 = new JLabel("Estado 1");
  28. private JButton jbtn_comenzar = new JButton("Generar prueba 0");
  29. private JButton jbtn_comenzar1 = new JButton("Generar prueba 1");
  30.  
  31. public interfaz(){
  32. super("Ejem Excel 3");
  33. definirventana();
  34. definirelementos();
  35. this.setVisible(true);
  36. this.setSize(300, 200);
  37. this.setResizable(false);
  38. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. }
  40.  
  41. private void definirventana(){
  42. this.getContentPane().setLayout(new GridBagLayout());
  43.  
  44. constraint.gridx = 0;
  45. constraint.gridy = 0;
  46. constraint.gridwidth = 1;
  47. constraint.gridheight = 1;
  48. constraint.weightx = 1.0;
  49. constraint.weighty = 1.0;
  50. constraint.fill = GridBagConstraints.BOTH;
  51. this.getContentPane().add(jbtn_comenzar, constraint);
  52.  
  53. constraint.gridx = 1;
  54. constraint.gridy = 0;
  55. constraint.gridwidth = 1;
  56. constraint.gridheight = 1;
  57. constraint.weightx = 1.0;
  58. constraint.weighty = 1.0;
  59. constraint.fill = GridBagConstraints.CENTER;
  60. this.getContentPane().add(jlbl_inf, constraint);
  61.  
  62. constraint.gridx = 0;
  63. constraint.gridy = 1;
  64. constraint.gridwidth = 1;
  65. constraint.gridheight = 1;
  66. constraint.weightx = 1.0;
  67. constraint.weighty = 1.0;
  68. constraint.fill = GridBagConstraints.BOTH;
  69. this.getContentPane().add(jbtn_comenzar1, constraint);
  70.  
  71. constraint.gridx = 1;
  72. constraint.gridy = 1;
  73. constraint.gridwidth = 1;
  74. constraint.gridheight = 1;
  75. constraint.weightx = 1.0;
  76. constraint.weighty = 1.0;
  77. constraint.fill = GridBagConstraints.CENTER;
  78. this.getContentPane().add(jlbl_inf1, constraint);
  79. }
  80.  
  81. private void definirelementos(){
  82. this.jbtn_comenzar.addActionListener(this);
  83. this.jbtn_comenzar1.addActionListener(this);
  84. }
  85.  
  86. public void actionPerformed(ActionEvent ae){
  87. if(ae.getSource() == jbtn_comenzar){
  88. jlbl_inf.setText("Procesando 0");
  89. crearWorkbook();
  90. generar();
  91. }
  92. if(ae.getSource() == jbtn_comenzar1){
  93. jlbl_inf1.setText("Procesando 1");
  94. crearWorkbook();
  95. generar1();
  96. }
  97. }
  98.  
  99. private void crearWorkbook(){
  100. try{
  101. libro = Workbook.createWorkbook(new File("Ejem3.xls"));
  102. hoja = libro.createSheet("HEJEM", 0);
  103. }catch(Exception e){
  104.  
  105. }
  106. }
  107.  
  108. private void generar(){
  109. try{
  110. Label label = new Label(0, 0, "Ejem 3");
  111. hoja.addCell(label);
  112.  
  113. for(int i = 1; i < 10; i++){
  114. Number numero = new Number(0, i, i);
  115. hoja.addCell(numero);
  116. }
  117.  
  118. libro.write();
  119. libro.close();
  120. jlbl_inf.setText("Fin ejecución");
  121. }catch(Exception e){
  122.  
  123. }
  124. }
  125.  
  126. private void generar1(){
  127. try{
  128. for(int a = 0; a < 10; a++){
  129. Label label = new Label(a, 0, "Colum "+a);
  130. hoja.addCell(label);
  131. for(int i = 1; i < 50000; i++){
  132. Number numero = new Number(a, i, i);
  133. hoja.addCell(numero);
  134. }
  135. }
  136.  
  137. libro.write();
  138. libro.close();
  139. jlbl_inf1.setText("Fin ejecución");
  140. }catch(Exception e){
  141.  
  142. }
  143. }
  144.  
  145. }
  146.  
  147.