Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EDITED Write JTable to Excel

I am trying to export my JTable to Excel file. Column and Row names are fine, but all the information I am adding into JTable does not get written. I tried System.out.println() and it prints Null values everywhere apart from column and row names. I tried to get answers from mother google but after 2 hours of reading and trying, still no progress. What stays in my head is that there could be some mistake in code at Write to Excel part or everything that is added to JTable is just a picture on my monitor, not an actual data in it.? Correct me if I am wrong and any help is highly appreciated.

Here is Write to Excel Part. in first For loop I am getting headers and in second For loop, i should get everything that is inside my JTable but I'm not.

TableColumnModel tcm = nrp.rotaTable.getColumnModel();

    String nameOfFile = JOptionPane.showInputDialog("Name of the file");

    Workbook wb = new HSSFWorkbook();
    CreationHelper createhelper = wb.getCreationHelper();

    Sheet sheet = wb.createSheet("new sheet");
    Row row = null;
    Cell cell = null;

    for (int i = 0; i < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        for (int j = 0; j < tcm.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());

        }
    }
    for (int i = 1; i < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        System.out.println("");
        for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue((String) nrp.tableModel.getValueAt(i, j)+" ");
            System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
        }
    }


    File file = new File("Some name.xls");
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();
  }
}

And here is FocusListener code.

rotaTable.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }
            public void focusLost(FocusEvent e) {
                CellEditor cellEditor = rotaTable.getCellEditor();
                if (cellEditor != null)
                    if (cellEditor.getCellEditorValue() != null)
                        cellEditor.stopCellEditing();
                    else
                        cellEditor.cancelCellEditing();
            }
        });

I am using 'DefaultTableModel'

 DefaultTableModel tableModel = new DefaultTableModel(12,8); 
JTable rotaTable = new JTable(tableModel); 

This is first time when I am working with POI library.

Picture of my JTable http://imgur.com/a/jnB8j

Picture of printed results in console http://imgur.com/a/jnB8j

Picture of Excel file created. http://imgur.com/a/jnB8j

like image 392
Helvijs Avatar asked Apr 23 '17 02:04

Helvijs


People also ask

What is JTable used for?

The JTable is used to display and edit regular two-dimensional tables of cells. See How to Use Tables in The Java Tutorial for task-oriented documentation and examples of using JTable .

How do you import data from Excel to JTable in Java?

Download and extract the compressed files from https://poi.apache.org/. This application selects excel file from the computer and displays it on java JTable . You need to click import excel button, then choose excel file you want to import. When you click open button, the file is imported and displayed on the JTable.


1 Answers

You have to start row index by 0 because table row is start from 0 and create excel row from 1 because first for you write column names.I modified your second for loop like this:

for (int i= 0; i < nrp.tableModel.getRowCount(); i++) {
    row = sheet.createRow(i+1);
    System.out.println("");
    for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {
        cell = row.createCell(j);
        if(nrp.tableModel.getValueAt(i, j)!=null){
        cell.setCellValue((String) nrp.tableModel.getValueAt(i, j));
        }
        System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
    }
}
like image 91
Yusuf NACIR Avatar answered Sep 24 '22 00:09

Yusuf NACIR