我正在尝试创建一个包含多个工作表的工作簿,但总是在每个 Excel 工作表中只获得一行,这是源文件中的最后一行。我的代码工作如下:
public void parse()
{
BufferedReader reader = null;
BufferedWriter writer = null;
String tmpFile = "tmpFile.tmp";
ExcelFile excelFile = new ExcelFile(_fileName + ".xlsx");
int lineCount = 0;
try
{
reader = new BufferedReader(new FileReader(_fileName));
writer = new BufferedWriter(new FileWriter(tmpFile));
String line = null;
while ( (line = reader.readLine()) != null)
{
String newLine = processLine(line.trim());
if (newLine == null)
continue;
else
{
if (newLine.isEmpty() )
{
lineCount++;
if(lineCount > 1)
{
continue;
}
}
else if (!newLine.isEmpty())
{
lineCount = 0;
}
if (getRepositoryName(newLine) != null && !getRepositoryName(newLine).isEmpty() )
excelFile.writeLine(getRepositoryName(newLine), newLine);
}
}
reader.close();
excelFile.createFile(_fileName.split("\\.")[0]);
}
catch (IOException e) {
e.printStackTrace();
}
}
每一行的创建由: excelFile.writeLine(getRepositoryName(newLine), newLine); 从以前的方法。
ExcelFile 类中的 writeLine 实现如下:
public void writeLine(String sheetName, String line)
{
HSSFSheet sheet = getSheet2(sheetName);
HSSFSheet sheetMain = getSheet2(_mainSheetName);
HSSFRow row = sheet.createRow(sheet.getLastRowNum() == 0 ? 0 :sheet.getLastRowNum()+1);
HSSFRow rowMain = sheetMain.createRow(sheetMain.getLastRowNum() == 0 ? 0 :sheetMain.getLastRowNum()+1);
String[] columnsArr = line.split(",");
for (int i = 0; i < columnsArr.length ; i++)
{
HSSFCell cell = row.createCell(i);
HSSFCell cellMain = rowMain.createCell(i);
cell.setCellValue(columnsArr[i]);
cellMain.setCellValue(columnsArr[i]);
}
}
需要知道我做错了什么,以及在不循环运行时是否可以创建多行。
提前致谢!