将List实例写入Excel文件 - Apache POI [英] Writing a List instance to Excel file - Apache POI
问题描述
我有一个 List
的 String
管道分隔的对象。类似这样的事情:
I have a List
of String
objects that are pipe delimited. something like this:
String1|String2|String3
String4|String5|String6
...
使用Apache POI作为excel库,是否可以迭代整个List写出字符串对象作为每个excel文件中的行?例如:
Using Apache POI as excel library, is it possible to iterate over entire List writing out the string object as each row in the excel file? i.e. something like:
for (String inst : <List instance>)
<sheetInstance>.write(inst)
即将字符串直接输出到excel作为行条目,而不是用字符串设置每个单元格值,即
i.e. output the string directly to the excel as a row entry rather than setting each cell value with a string i.e.
setting row 1, cell 1 = String1
setting row 1, cell 2 = String2
setting row 1, cell 3 = String3
setting row 2, cell 1 = String4 ...
目前看起来我需要为每个值设置单个单元格。
Currently, it looks like I need to set individual cells to each value.
推荐答案
您需要按顺序将 String
拆分为数组填充单元格,如下所示:
You need to split the String
into an array in order to fill the cells, like this:
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]);
}
}
这篇关于将List实例写入Excel文件 - Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!