You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java将两个不同长度的List写入Excel?求助解决

解决List长度不一致时Excel完整写入的问题

原代码中存在两个长度不同的List:list1包含4个元素,list2包含5个元素,但当前循环仅以list1.size()作为循环次数,导致list2的第5个元素无法写入Excel。需要实现第一列完整写入list1的4个元素,第二列完整写入list2的5个元素。

原代码问题片段

HSSFWorkbook workbook = new HSSFWorkbook(); 
HSSFSheet sheet=workbook.createSheet("Demo_Sheet");

// Creating Row Heading 
HSSFRow rowheading=sheet.createRow(0); 
rowheading.createCell(0).setCellValue("INNER CLASS"); 
rowheading.createCell(1).setCellValue("INNER METHOD"); 

for(int i=0;i<2;i++){ 
    HSSFCellStyle cellheaderstyle = workbook.createCellStyle(); 
    HSSFFont font = workbook.createFont(); 
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
    font.setFontName(HSSFFont.FONT_ARIAL); 
    font.setFontHeightInPoints((short)11); 
    cellheaderstyle.setFont(font); 
    cellheaderstyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); 
    rowheading.getCell(i).setCellStyle(cellheaderstyle); 
} 

List<String> list1=DemoOnInnerMethod2.getInnerClasses(); 
List<String> list2 = DemoOnInnerMethod2.getInnerMethods(); 

// 问题核心:仅循环list1的长度,导致list2的第5个元素无法写入
for(int j=0;j<list1.size();j++) { 
    Row row = sheet.createRow(j+1); 
    row.createCell(0).setCellValue(list1.get(j)); 
    row.createCell(1).setCellValue(list2.get(j)); 
}

FileOutputStream fout = new FileOutputStream(new File("/home/dilipp/Desktop/abcd.xls")); 
workbook.write(fout); 
fout.close(); 
System.out.println("Success"); 

解决方案

核心思路是以两个List的最大长度作为循环次数,在每次循环中判断当前索引是否在对应List的有效范围内,存在元素则写入,不存在则跳过(或写入空值),这样就能保证两个List的元素都被完整写入。

修改后的完整代码:

HSSFWorkbook workbook = new HSSFWorkbook(); 
HSSFSheet sheet=workbook.createSheet("Demo_Sheet");

// Creating Row Heading 
HSSFRow rowheading=sheet.createRow(0); 
rowheading.createCell(0).setCellValue("INNER CLASS"); 
rowheading.createCell(1).setCellValue("INNER METHOD"); 

for(int i=0;i<2;i++){ 
    HSSFCellStyle cellheaderstyle = workbook.createCellStyle(); 
    HSSFFont font = workbook.createFont(); 
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
    font.setFontName(HSSFFont.FONT_ARIAL); 
    font.setFontHeightInPoints((short)11); 
    cellheaderstyle.setFont(font); 
    cellheaderstyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); 
    rowheading.getCell(i).setCellStyle(cellheaderstyle); 
} 

List<String> list1=DemoOnInnerMethod2.getInnerClasses(); 
List<String> list2 = DemoOnInnerMethod2.getInnerMethods(); 

// 计算两个List的最大长度,确保所有元素都能被处理
int maxSize = Math.max(list1.size(), list2.size());

for(int j=0;j<maxSize;j++) { 
    Row row = sheet.createRow(j+1); 
    // 写入第一列:判断索引是否在list1的范围内
    if (j < list1.size()) {
        row.createCell(0).setCellValue(list1.get(j));
    }
    // 写入第二列:判断索引是否在list2的范围内
    if (j < list2.size()) {
        row.createCell(1).setCellValue(list2.get(j));
    }
}

FileOutputStream fout = new FileOutputStream(new File("/home/dilipp/Desktop/abcd.xls")); 
workbook.write(fout); 
fout.close(); 
System.out.println("Success"); 

关键修改说明

  • 新增int maxSize = Math.max(list1.size(), list2.size());:获取两个List的最大长度作为循环上限,确保较长的List的所有元素都能被遍历到。
  • 写入单元格时增加if (j < listX.size())判断:避免触发IndexOutOfBoundsException,当索引超过List长度时,不写入对应单元格(如果需要空值填充,可改为row.createCell(0).setCellValue("");)。

内容的提问来源于stack exchange,提问作者Sunil T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:56:01