繁体   English   中英

将List实例写入Excel文件 - Apache POI

[英]Writing a List instance to Excel file - Apache POI

我有一个管道分隔的String对象List 这样的事情:

String1|String2|String3
String4|String5|String6
...

使用Apache POI作为excel库,是否可以迭代整个List,将字符串对象写为excel文件中的每一行? 即:像:

for (String inst : <List instance>)
       <sheetInstance>.write(inst)

即将字符串直接输出到excel作为行条目,而不是用字符串即设置每个单元格值

 setting row 1, cell 1 = String1 setting row 1, cell 2 = String2 setting row 1, cell 3 = String3 setting row 2, cell 1 = String4 ... 

目前,看起来我需要为每个值设置单个单元格。

您需要将String拆分为数组以填充单元格,如下所示:

for (short rowIndex = 0; rowIndex < stringList.length(); rowIndex++) {
    Row row = sheet.createRow(rowIndex);
    String[] cellValues = stringList.get(rowIndex).split("|");
    for (int colIndex = 0; colIndex < cellValues.length; colIndex++) {
        Cell cell = row.createCell(colIndex);
        cell.setCellValue(cellValues[colIndex]);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM