Java 当列达到特定数字时创建新的excel行
我有下面的代码,它是从一个文本区域获取数据,并把它放在一个excel表格中 我的问题是,当列达到8时,它不会移动到下一行 如果我的文本区域包含以下内容:Java 当列达到特定数字时创建新的excel行,java,filewriter,Java,Filewriter,我有下面的代码,它是从一个文本区域获取数据,并把它放在一个excel表格中 我的问题是,当列达到8时,它不会移动到下一行 如果我的文本区域包含以下内容: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 结果显示在excel中,如下图所示,最后一列包含2个数字 我需要它看起来像: 我做错了什么 我的代码: try { //create .xls and create a worksheet.
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
结果显示在excel中,如下图所示,最后一列包含2个数字
我需要它看起来像:
我做错了什么
我的代码:
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();
}
}
});
试试看{
//创建.xls并创建一个工作表。
FileOutputStream fos=新的FileOutputStream(excelFile);
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet工作表=工作簿.createSheet(“我的工作表”);
Desktop desktop2=Desktop.getDesktop();
//创建第1行((excel中的行号))
HSSFRow row1=null;
//从第1行创建列A并设置数据
HSSFCell cellA1=null;
String[]strings=getTextArea_1.split(“\t”);
//String[]rows=getTextArea_1.split(“\n”);
int行线=0;
//row1=工作表.createRow(行线);
int-colLine=0;
row1=工作表.createRow(行线);
for(字符串b:字符串){
(柯林)