gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:45:15 25 4
gpt4 key购买 nike

我有一个 ListString 对象,它们是管道分隔的。像这样:

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

使用 Apache POI 作为 excel 库,是否可以遍历整个列表,将字符串对象写出为 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]);
}
}

关于java - 将 List 实例写入 Excel 文件 - Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com