0

我有一个程序,我从文本区域获取字符串,然后将其转换为数组,然后读取此数组,然后将其写入 excel,写入工作正常,但不考虑制表符空格。我的代码是

    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();
                }
        }

显示的结果就像这张图片 在此处输入图像描述 ,但我需要它如下所示,就好像我从我的文本区域复制它并将其粘贴到 Excel 工作表中,这是我得到的结果:

在此处输入图像描述 请问我想念什么?

使用Lancef的建议后,结果只有一个单元格列下,如何使它成为一行?!

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

我试图玩 rowLines 和 colLine 但它对我不起作用,我总是把它当作

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

1 回答 1

2

您正在结束行拆分:\n而不是制表符:\t

所以:

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

代替:

String[] strings = getTextArea_1.split("\n");
于 2016-08-10T16:37:27.490 回答