我有一个 xlsx 文件,其中有多个工作表,我正在使用 apache poi 编写 Excel,在工作表 2 中我有 2 列 我想通过运行 for 循环来填充每一列,但我发现只有最后一个 for 循环被写入,前一个在最终写入的输出文件中变为空白,我想通过这些 for 循环写入这两列,请帮忙。
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(fileNamesArray[i].toString());
}//this dont get written
for(int i=0;i<fileDatesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(1);
cell.setCellValue(fileDatesArray[i].toString());
}//only this get written
这是完整的代码
public class DashBoard {
public void writeDashBoard() throws IOException, SQLException
{
CODToolUtil codToolUtil = new CODToolUtil();
// Read property file to initialize constants
String templateDashBoardFile = codToolUtil.getPropValues("templateDashBoardFile");
String outputDir = codToolUtil.getPropValues("outputDir");
String dirSeprator = codToolUtil.getPropValues("dirSeprator");
String fdate = CODToolUtil.getDate();
CODDAO coddao=new CODDAO();
LinkedHashSet<String> hs= new LinkedHashSet<String>();
LinkedHashSet<String> hs1= new LinkedHashSet<String>();
FileInputStream fsIP= new FileInputStream(new File(templateDashBoardFile)); //Template file
XSSFWorkbook wb = new XSSFWorkbook(fsIP);
XSSFSheet worksheet = wb.getSheetAt(0);
Cell cell = null;
cell = worksheet.getRow(1).getCell(0);
cell.setCellValue(CODToolUtil.getDate());//Date
cell = worksheet.getRow(1).getCell(1);
int allfiles=coddao.getAllfiles();
cell.setCellValue(allfiles);//All Files
cell = worksheet.getRow(1).getCell(2);
int callfilesY=coddao.getAllProcessedfilesCallY();
cell.setCellValue(callfilesY);//All Y Files
cell = worksheet.getRow(1).getCell(3);
int callfilesN=coddao.getAllProcessedfilesCallN();
cell.setCellValue(callfilesN);//All N Files
cell = worksheet.getRow(1).getCell(4);
int allLTE=coddao.getAllProcessedfilesLTE();
cell.setCellValue(allLTE);//All LTE Files
cell = worksheet.getRow(1).getCell(5);
int allWCDMA=coddao.getAllProcessedfilesWCDMA();
cell.setCellValue(allWCDMA);//All WCDMA Files
//Sheet 0 OverView Complete
//Sheet 1 Successfull CT
XSSFSheet worksheet1 = wb.getSheetAt(1);
hs=coddao.getAllProcessedfilesNameY();
hs1=coddao.getAllProcessedfilesDateY();
Object[] fileNamesArray = hs.toArray();
Object[] fileDatesArray = hs1.toArray();
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(fileNamesArray[i].toString());
}//this dont get written
for(int i=0;i<fileDatesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(1);
cell.setCellValue(fileDatesArray[i].toString());
}//only this get written
fsIP.close();
File saveDirectory = new File(outputDir);// Create OutPutDirectory
saveDirectory.mkdir();
String savefilePath = saveDirectory.getAbsolutePath();
FileOutputStream output_file = newFileOutputStream(newFile(savefilePath+dirSeprator+fdate+"-"+templateDashBoardFile)); // save in output
wb.write(output_file); // write changes save it.
output_file.close(); // close the stream
}
public static void main(String[] args) throws IOException, SQLException {
new DashBoard().writeDashBoard();
}
}
最佳答案
您正在创建同一行两次 - 可能会覆盖第一个循环中创建的“第一”行,并使用第二个循环中创建的“第二”行。
如果 fileNamesArray 和 fileDatesArray 大小相同,您可以将循环组合为:
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell1 = row.createCell(0);
cell1.setCellValue(fileNamesArray[i].toString());
cell2 = row.createCell(1);
cell2.setCellValue(fileDatesArray[i].toString());
}
检查哪个数组更大并首先循环遍历它,然后循环遍历第二个数组,但不使用 worksheet1.createRow(i+1)
- 使用 worksheet1.getRow(i+1)
,重用您在第一个循环中创建的行元素。
注意:理论上,即使数组大小不同,您仍然可以使用一个循环,只需确保应用相关检查以避免 ArrayIndexOutOfBoundsException。
关于java - 无法通过多个for循环写入excel行列(apache poi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213349/