1

我有一个用管道分隔ListString对象。像这样的东西:

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 ...

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

4

1 回答 1

4

您需要将其拆分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]);
    }
}
于 2012-06-28T22:13:09.223 回答