Search code examples
javaswingjtableexport-to-excelfilewriter

Exporting JTable to excel - escaping the value of the next column


In my file writer, I have these codes, the j == 4 and j == 8 is to execute a different code for the column that contains numbers

for(int i=0; i<model.getRowCount();i++){
        for(int j=0; j<model.getColumnCount();j++){
            if(j==4 || j==8){
                excelWriter.write("\""+model.getValueAt(i, j) + "\t"+"\""); 
            }
            else{
                excelWriter.write(model.getValueAt(i, j) + "\t");
            }
        }
        excelWriter.write("\n");
    }

the problem is after that column with number had been printed, the next column will not be printed.. for example

This is my JTable

| texta | textb | textc | textd | number | texte | textf | ....... 

in the excel file, it will be printed as....

| texta | textb | textc | textd | number | textf | ....... 

it skips the column beside the column with number...

when i remove the 2nd "\" in the code, the rest of the column concatenate to the column with number.

Any idea how could I fix this? Thanks in advance


Solution

  • The second \ have to be written before \t I would guess.

    Wait... I have created an Excel sheet with text columns and number columns. Then I exported that sheet to a tab separated file. When I open that file in a text editor the number isn't escaped on any kind. So I don't think you need this if.