0

I am using JXL api. I want to fill an excel table that already have content, like:

123 321 12324   

123 321 231 

123 321 343 

123 321 454 

123 321 565

I am trying to write the next value after the fifth line, but I dont know how to do it. I only know how to create and write an excel table if it is empty. And I know how to read it. I was thinking that first I have to read it, and my array which would contain the values of the table would help me to pick the last value of the table then I would write the other values starting from there. I have these methods:

To read:

arquivo = new File("C:\\Users\\maniceto\\Desktop\\arq.xls");

        planilha = Workbook.getWorkbook(arquivo); 

        Sheet[] abas = planilha.getSheets(); 

        aba = planilha.getSheet(0);  

        matriz = new String[aba.getRows()][aba.getColumns()];

        Cell[] cel; 

        for (int i = 0; i < matriz.length; i++) {
            cel = aba.getRow(i);

            for (int j = 0; j < matriz[0].length; j++) {
                matriz[i][j] = cel[j].getContents(); 
            }
        }

        System.out.println("Lines " + matriz.length);
        System.out.println("Columns " + matriz[0].length);
        System.out.println("");

        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz[0].length; j++) {
                System.out.print(matriz[i][j] + "\t");
            }
            System.out.println("");
        }

To write an empty excel table:

WritableWorkbook workbookVazio = Workbook.createWorkbook(file);


            WritableSheet sheet1 = workbookVazio.createSheet("First Sheet", 0);
            TableModel model = table.getModel();

            for (int i = 0; i < model.getColumnCount(); i++) {
                Label column = new Label(i, 0, model.getColumnName(i));
                sheet1.addCell(column);
            }
            int j = 0;
            for (int i = 0; i < model.getRowCount(); i++) {
                for (j = 0; j < model.getColumnCount(); j++) {
                    Label row = new Label(j, i + 1,
                            model.getValueAt(i, j).toString());
                    sheet1.addCell(row);
                }
            }
            workbookVazio.write();
            workbookVazio.close();
DT7
  • 1,615
  • 14
  • 26
Murilo
  • 4,453
  • 5
  • 19
  • 28

2 Answers2

0

As stated by rgettman in this question, I would also advise to use Apache POI to handle Excel files with JAVA.

To append values using POI, you just need to get the current number of rows :

int rows = sheet.getPhysicalNumberOfRows();

POI is well documented and you will easily find several code examples on the net.

Community
  • 1
  • 1
Julien
  • 1,302
  • 10
  • 23
0

you could try something like

int lastRowNum = workbookVazio.getSheet("First Sheet").getLastRowNum();
int  nextFreeRow = lastRowNum+1;

or you can use Apache POI theres a good tutorial here.

CoffeeTime
  • 305
  • 1
  • 6
  • 17
  • But I will put have to create another Workbook ? like : WritableWorkbook workbookVazio = Workbook.createWorkbook(file); , and I will have to create a WritableSheet sheet1 = workbookVazio.createSheet("First Sheet", 0); too ? – Murilo Nov 01 '13 at 13:30
  • @Murilo no, you've already created the workbook and the writeable sheet instances so use them. All you need to do then is iterate over sheet1 using the nextFreeRow as your starting point. – CoffeeTime Nov 01 '13 at 17:27