将 List 实例写入 Excel 文件 - Apache POI [英] Writing a List instance to Excel file - Apache POI

查看:44
本文介绍了将 List 实例写入 Excel 文件 - Apache POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由管道分隔的 String 对象的 List.像这样:

String1|String2|String3字符串 4|字符串 5|字符串 6...

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

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

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

<块引用>

设置第 1 行,单元格 1 = String1设置第 1 行,单元格 2 = String2设置第 1 行,单元格 3 = String3设置第 2 行,单元格 1 = String4 ...

目前,我似乎需要为每个值设置单个单元格.

解决方案

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

for (short rowIndex = 0; rowIndex < stringList.length(); rowIndex++) {行行 = 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]);}}

I have a List of String objects that are pipe delimited. something like this:

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

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)

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.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆