英文:
Storing ArrayList in Excel using Apache POI HSSF
问题
我有一个数据列表:
List data1 = (List) session.getAttribute("data");
现在,我希望将这些数据存储在一个Excel文件中:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("lOGINID");
rowhead.createCell((short) 1).setCellValue("CUSTOMERID");
rowhead.createCell((short) 2).setCellValue("updatetime");
int index = 1;
for (itr = data1.iterator(); itr.hasNext(); ) {
// 在这里写什么?
}
英文:
I have a data list:
List data1 = (List) session.getAttribute("data");
Now, I want this data to be stored in an Excel file:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("lOGINID");
rowhead.createCell((short) 1).setCellValue("CUSTOMERID");
rowhead.createCell((short) 2).setCellValue("updatetime");
int index = 1;
for (itr = data1.iterator(); itr.hasNext(); ) {
// what to write here?
}
答案1
得分: 1
你没有明确说明你的列表中包含什么。 List data1 = (List) session.getAttribute("data");
必须是 List<SomeClass>
。
你可以简单地遍历列表,获取当前索引上的对象,创建新行并填充单元格中的值。
for (int i = 0; i < data1.size(); i++) { // 遍历列表
SomeClass data = data1.get(i);// 获取当前索引 i 上的对象
HSSFRow row = sheet.createRow(i + 1); // 创建新行
// 填充值
row.createCell(0).setCellValue(data.field1);
row.createCell(1).setCellValue(data.field2);
row.createCell(2).setCellValue(data.field3);
}
一个完整的示例,假设列表包含 RowData
:
import lombok.AllArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
void writeList() throws IOException {
// 读取数据,RowData 在您的情况下可能不同
List<RowData> data1 = (List<RowData>) session.getAttribute("data");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow(0); // 头部
rowhead.createCell(0).setCellValue("lOGINID");
rowhead.createCell(1).setCellValue("CUSTOMERID");
rowhead.createCell(2).setCellValue("updatetime");
for (int i = 0; i < data1.size(); i++) {
RowData data = data1.get(i);// 行数据
HSSFRow row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(data.loginId);
row.createCell(1).setCellValue(data.customerId);
row.createCell(2).setCellValue(data.time);
}
// 写入文件
try (FileOutputStream out = new FileOutputStream("test.xlsx")) {
wb.write(out);
}
}
@AllArgsConstructor
class RowData {
String loginId;
String customerId;
String time;
}
英文:
You haven't clearly stated what your List holds. List data1 = (List) session.getAttribute("data");
It must be List<SomeClass>
.
You can simply iterate over the list, pull the object on current index, create new row and fill the values in cells
for (int i = 0; i < data1.size(); i++) { //iterate over the list
SomeClass data = data1.get(i);//pull the object on current index i
HSSFRow row = sheet.createRow(i + 1); //create new row
//fill the values
row.createCell(0).setCellValue(data.field1);
row.createCell(1).setCellValue(data.field2);
row.createCell(2).setCellValue(data.field3);
A complete example assuming the list contains RowData
,:
import lombok.AllArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
void writeList() throws IOException {
//read data , RowData can be different in your case
List<RowData> data1 = (List<RowData>) session.getAttribute("data");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow(0); //header
rowhead.createCell(0).setCellValue("lOGINID");
rowhead.createCell(1).setCellValue("CUSTOMERID");
rowhead.createCell(2).setCellValue("updatetime");
for (int i = 0; i < data1.size(); i++) {
RowData data = data1.get(i);//rows
HSSFRow row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(data.loginId);
row.createCell(1).setCellValue(data.customerId);
row.createCell(2).setCellValue(data.time);
}
//write to file
try (FileOutputStream out = new FileOutputStream("test.xlsx")) {
wb.write(out);
}
}
@AllArgsConstructor
class RowData {
String loginId;
String customerId;
String time;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论