0

I have a program where I get a string from a text area and then I convert it to an array, then I read this array and then write it to an excel, the writing is working but it doesn't consider the tab spaces. My code is

    String getTextArea_1 = textArea_1.getText();
        String userHomeFolder = System.getProperty("user.home");
        File excelFile = new File(userHomeFolder, "RESULT.xls");
        try {
                    //create .xls and create a worksheet.
                    FileOutputStream fos = new FileOutputStream(excelFile);
                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet worksheet = workbook.createSheet("My Work Sheet");

                    //Create ROW-1 ((row line number in the excel))
                    HSSFRow row1;
                    //Create COL-A from ROW-1 and set data
                    HSSFCell cellA1;

                    int rowLines = 0;
                    int colLine = 0;
                    String[] strings = getTextArea_1.split("\n");                   
                    for(String b : strings) {
                        rowLines ++;
                        row1 = worksheet.createRow(rowLines -1);
                        cellA1 = row1.createCell(colLine);
                        cellA1.setCellValue(b);

                        colLine = colLine - 1;
                        colLine ++;
                        //System.out.println(b);
                        //colLine ++;
                        //System.out.println(rowLines);
                    }

                    //Save the workbook in .xls file
                    workbook.write(fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }

the result shown is like this image enter image description here but I need it to be like the below, as if I copy it from my text area and paste it to the excel sheet this is the result I get:

enter image description here What do I miss please ?

After using the suggestion of Lancef, the result is only under one cell column, how to make make it under one row?!

1746
C4A
13D06MK
GREECE 
2012-07-04
2015-07-03
2012-07-04
2015-07-03
"Yes"

I tried to play with rowLines and colLine but it doesn't work for me, I always get it as

1746                                
    C4A                         
        13D06MK                     
            GREECE                  
                2012-07-04              
                    2015-07-03          
                        2012-07-04      
                            2015-07-03  
                                "Yes"

1 Answers1

2

You are splitting on end line: \n instead of tab: \t

So:

String[] strings = getTextArea_1.split("\t");

instead of:

String[] strings = getTextArea_1.split("\n");
sinsuren
  • 1,745
  • 2
  • 23
  • 26
Lancef
  • 21
  • 2