Java 循环类成员并写入Excel Apache POI
我有一个自定义对象的数组列表。我试图在这些对象上循环以将输出写入excel文件 在下面的代码中,在第一个循环中,我通过循环类成员变量来设置excel文件中的标题行。在第二个循环中,我写入对象值 我的代码:Java 循环类成员并写入Excel Apache POI,java,excel,apache-poi,Java,Excel,Apache Poi,我有一个自定义对象的数组列表。我试图在这些对象上循环以将输出写入excel文件 在下面的代码中,在第一个循环中,我通过循环类成员变量来设置excel文件中的标题行。在第二个循环中,我写入对象值 我的代码: class ChecklistOutput { //Instantiating class data members String a, b, c; public ChecklistOutput() { a = ""; b = ""; c = ""; } } priva
class ChecklistOutput {
//Instantiating class data members
String a, b, c;
public ChecklistOutput() {
a = ""; b = ""; c = ""; }
}
private static ArrayList<ChecklistOutput> MasterOutput = new ArrayList<ChecklistOutput>();
private static void writeToMasterExcel() {
ChecklistOutput obj1 = new ChecklistOutput();
obj1.a = "AA"; obj1.b = "BB"; obj1.c = "CC";
ChecklistOutput obj1 = new ChecklistOutput();
obj2.a = "AA"; obj2.b = "BB"; obj2.c = "CC";
ChecklistOutput obj1 = new ChecklistOutput();
obj3.a = "AA"; obj3.b = "BB"; obj3.c = "CC";
ChecklistOutput obj1 = new ChecklistOutput();
obj4.a = "AA"; obj4.b = "BB"; obj4.c = "CC";
MasterOutput.add(obj1);
MasterOutput.add(obj2);
MasterOutput.add(obj3);
MasterOutput.add(obj4);
System.out.println(MasterOutput.size());
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
int rownum = 0, cellnum = 0;
sheet = workbook.createSheet("Master Spreadsheet");
row = sheet.createRow(rownum);
System.out.println("rownum " + rownum);
Class<?> c = new ChecklistOutput().getClass();
Field[] fields = c.getDeclaredFields();
// First loop
for (Field field : fields) {
cell = row.createCell(cellnum);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(field.getName());
cellnum += 1;
}
System.out.println(MasterOutput.size());
// Second loop
for (ChecklistOutput x : MasterOutput) {
// This prints 4 times meaning that there are 4 values in
// MasterOutput
System.out.println("Hell");
rownum += 1;
cellnum = 0;
row = sheet.createRow(rownum);
for (Field field : fields) {
cell = row.createCell(cellnum);
cell.setCellType(Cell.CELL_TYPE_STRING);
try {
// I can see values here
System.out.println(field.get(x).toString());
cell.setCellValue(field.get(x).toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
cellnum += 1;
}
}
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(
"C:\\Users\\ABC\\Documents\\Checklist-Output.xls",
true));
workbook.write(bos);
bos.close();
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
有人能帮我吗?谢谢 试试这个:
for (int col = 0; col < fields.length; col++) {
Field field = fields[col];
HSSFRow header = sheet.getRow(rownum);
if (header == null) {
header = sheet.createRow(rownum);
}
HSSFCell cell = header.createCell(col);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(field.getName());
}
for (ChecklistOutput x : MasterOutput) {
rownum += 1;
HSSFRow rowData = sheet.getRow(rownum);
if (rowData == null) {
rowData = sheet.createRow(rownum);
}
for (int col = 0; col < fields.length; col++) {
Field field = fields[col];
HSSFCell cellData = rowData.createCell(col);
cellData.setCellType(Cell.CELL_TYPE_STRING);
try {
// I can see values here
System.out.println(field.get(x).toString());
cellData.setCellValue(field.get(x).toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
for(int col=0;col
我还建议您使用POJO而不是ChecklistOutput(带有getter和setter的私有字段)。
我做了和你一样的事。如果希望从pojo中获取字段的值,可以使用反射来调用getter。你可以调查一下。它要求其他东西,但提供了一个工作示例来获取反射值(我也将其用于POI)。这不就是主输出列表为空的情况吗?此外,您在错误的位置重置了
cellnum
。它应该在外环而不是内环,我同意。此外,您还应该按照Java命名约定将字段重命名为“masterOutput”。我选中了masterOutput,它有4个值。masterOutput
列表中的所有ChecklistOutput
实例是否都包含A
、B
和C
值的emtpy字符串(由构造函数初始化)?不是。事实上,它们都不是空字符串。我在实例中存储了值,打印时可以看到它们。我已经更新了相同的代码。
for (int col = 0; col < fields.length; col++) {
Field field = fields[col];
HSSFRow header = sheet.getRow(rownum);
if (header == null) {
header = sheet.createRow(rownum);
}
HSSFCell cell = header.createCell(col);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(field.getName());
}
for (ChecklistOutput x : MasterOutput) {
rownum += 1;
HSSFRow rowData = sheet.getRow(rownum);
if (rowData == null) {
rowData = sheet.createRow(rownum);
}
for (int col = 0; col < fields.length; col++) {
Field field = fields[col];
HSSFCell cellData = rowData.createCell(col);
cellData.setCellType(Cell.CELL_TYPE_STRING);
try {
// I can see values here
System.out.println(field.get(x).toString());
cellData.setCellValue(field.get(x).toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}