获取excel的字符串并考虑(TAB)空格

时间:2016-08-10 15:52:29

标签: java file-writing spreadsheet-excel-writer

我有一个程序,我从文本区域获取一个字符串,然后将其转换为数组,然后我读取此数组,然后将其写入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();
                }
        }

显示的结果就像这张图片 enter image description here 但我需要它像下面一样,好像我从我的文本区域复制它并将其粘贴到excel表格,这是我得到的结果:

enter image description here 我想念什么?

使用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"

1 个答案:

答案 0 :(得分:1)

您在结束行分割:<div> <div class="row"> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> </div> <div class="row"> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> </div> <div class="row"> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> <div class="column">Info</div> </div> </div> 而不是制表符:let documents = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

所以:

\n

而不是:

\t