在类成员上循环并写入Excel Apache POI

时间:2015-05-21 08:30:16

标签: java excel apache-poi

我有一个自定义对象的数组列表。我试图循环这些对象将我的输出写入excel文件。

在我的下面的代码中,在第一个循环中,我通过循环遍历类成员变量来设置excel文件中的标题行。在第二个循环中,我写了对象值。

我的代码:

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();
    }
}

但是,我只在excel文件中获取标头值。

输出:

| a | b | c |

有人可以帮我这个吗?谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

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();
        }
    }
}

另外,我建议您使用POJO而不是ChecklistOutput(带有getter和setter的私有字段)。 我做了同样的事情。如果你想要pojo字段的值,你可以使用反射来调用getter。您可以查看 my question 。它要求其他东西,但提供了一个工作示例来获取带有反射的值(我也将它用于POI)。