简体   繁体   English

如何使用apache-poi在列中显示对象

[英]How to display an Object in a column with apache-poi

I have a big DTO with exactly 234 fields, and I have to display values of each fields of this DTO in a column of an Excel file created with apache-poi. 我有一个大的DTO,正好有234个字段,并且必须在用apache-poi创建的Excel文件的列中显示此DTO的每个字段的值。

This is my code : 这是我的代码:

// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Export values");

// Get the Entity
Simfoot simEntity = simService.findById(simId).get();

Row row = sheet.createRow(0);
row.createCell(1).setCellValue("Consult our values");

// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).

I want to have in my first column : nothing , in my second only the String display ( "Consult our values" ) and in my third column I need to have my 234 fields. 我想在第一列中包含:无,在第二列中仅包含String显示(“ Consult our values”),在第三列中,需要包含234个字段。 With an field ( the value of the field ) in one cell. 在一个单元格中具有一个字段(字段的值)。 So, 234 rows displaying one value in the third column. 因此,在第三列中有234行显示一个值。

I hope that it is clear. 我希望这很清楚。

Thanks a lot for your help. 非常感谢你的帮助。

Using some reflection: 使用一些反射:

    // Blank workbook
    XSSFWorkbook workbook = new XSSFWorkbook();

    final Sheet sheet = workbook.createSheet("Export values");

    // Get the Entity
    final Simfoot simEntity = simService.findById(simId).get();

    Row row = sheet.createRow(0);
    row.createCell(1).setCellValue("Consult our values");

    // and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).
    Arrays.stream(simEntity.getClass().getDeclaredMethods())
            .filter(m -> m.getName().startsWith("get") && m.getParameterTypes().length == 0 && !void.class.equals(m.getReturnType()))
            .forEach(m -> {
                    try {
                            Object value = m.invoke(simEntity, null);
                            Row r = sheet.createRow(sheet.getLastRowNum()+1);
                            r.createCell(2).setCellValue(value == null ? "" : value.toString());
                    }
                    catch (Exception ex) {
                            // Manage Exception....
                    }
            });

I'll add a method on Simfoot to return all the values: 我将在Simfoot上添加一个方法以返回所有值:

public List<String> getAllValues() {
    return Arrays.asList(getAtt1(), getAtt2(), .. , getAtt234());
}

Then create a row per attribute, and then you can merge the rows of the first 2 columns. 然后为每个属性创建一行,然后可以合并前两列的行。 Example here with 6 attributes: 这里有6个属性的示例:

int n = 6; // would be 234 for you
XSSFCellStyle styleAlignTop = workbook.createCellStyle();
styleAlignTop.setVerticalAlignment(VerticalAlignment.TOP);
Row row;
for(int i=0; i<n; i++) {
    row = sheet.createRow(i);
    if(i==0) {
        Cell cell = row.createCell(1);
        cell.setCellStyle(styleAlignTop);
        cell.setCellValue("Consult our values");
    }
    row.createCell(2).setCellValue(simEntity.getAllValues().get(i));
}
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 1, 1));

It shows like this: 它显示如下: 在此处输入图片说明

Another way to list your attributes would be to use Reflection but I find it very clunky: 列出属性的另一种方法是使用Reflection,但是我觉得它很笨拙:

Simfoot simEntity = new Simfoot("pap", "pep", "pip", "pop", "pup", "pyp");

for(PropertyDescriptor propertyDescriptor :
    Introspector.getBeanInfo(Simfoot.class).getPropertyDescriptors()) {
        System.out.println(propertyDescriptor.getReadMethod().invoke(simEntity));
}

Outputs: 输出:

pap
pep
pip
pop
pup
pyp
class Simfoot

so you have to filter out getClass and any other unwanted methods and getters 因此,您必须过滤掉getClass和任何其他不需要的方法和获取器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM