当列达到特定数字时创建新的Excel行

时间:2016-08-18 09:21:32

标签: java filewriter

我有以下代码,它从文本区域获取数据并将其放在Excel工作表中。

我的问题是,当列达到8时,它不会移动到下一行。

如果我的文字区域包含如下:

0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17

结果如下图所示,最后一栏包含2个数字!!

enter image description here

我需要它看起来像:

enter image description here

我做错了什么?

我的代码:

        try {
                    //create .xls and create a worksheet.
                    FileOutputStream fos = new FileOutputStream(excelFile);
                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet worksheet = workbook.createSheet("My Work Sheet");
                    Desktop desktop2 = Desktop.getDesktop();

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

                    String[] strings = getTextArea_1.split("\t");
                    //String[] rows = getTextArea_1.split("\n");             
                    int rowLines = 0;
                    //row1 = worksheet.createRow(rowLines);
                    int colLine = 0;
                    row1 = worksheet.createRow(rowLines);

                    for(String b : strings) {
                        while(colLine <= 8) {
                            cellA1 = row1.createCell(colLine);
                            cellA1.setCellValue(b);
                            break;
                        }
                        colLine++;
                        rowLines++;
                    }

                    //Save the workbook in .xls file
                    workbook.write(fos);
                    fos.flush();
                    fos.close();

                    // to open the text file
                    if(excelFile.exists()) {
                        if(!getTextArea_1.isEmpty()) {
                            desktop2.open(excelFile);
                        }
                        else {
                            JOptionPane.showMessageDialog(textArea_1, "Please search your data first", "NO DATA TO EXTRACT", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
        }
});

0 个答案:

没有答案