Vaadin excel 导出转换器值

Vaadin excel export with converter values

我正在尝试使用 Vaadin TableExport 导出对应于 FilterTable 的 Excel。 Filtertable 有一些列存储日期和其他 class 类型的元素,所以我使用 setConverter 函数将它们打印为特定的字符串:

filerTable.setConverter("dateColumn", dateConverter);
filerTable.setConverter("myClassColumn", myClassConverter);

dateConvertermyClassConverter 是一些 classes 的实例,用于将该列值打印为字符串。

当我想将 table 导出为 Excel 时出现问题:setConverter 转换未应用于输出文件。例如,日期单元格导出为字符串(“42741,0080787037”而不是“06/01/17 0:11”)。导出 Excel 文件的代码部分是:

ExcelExport exp = new ExcelExport(new CustomTableHolder(filerTable), "excel.xls");
exp.setRowHeaders(true);
exp.export();

在应用 setConverter 函数后,是否有任何方法可以完全如图所示导出 table?

查看 add-on sources,似乎支持此功能,但要使其正常工作,必须发生 2 件事:

代码:

public class ExcelExportTableComponent extends VerticalLayout {

    public ExcelExportTableComponent() {
        // basic table configuration
        Table table = new PropertyFormatTable();
        BeanItemContainer<Person> itemContainer = new BeanItemContainer<>(Person.class);
        table.setContainerDataSource(itemContainer);
        table.setConverter("age", new AgeConverter());

        // add some dummy data to the table
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            itemContainer.addItem(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1));
        }

        // add components to the layout
        addComponent(table);
        addComponent(new Button("Export to excel", event -> {
            ExcelExport excelExport = new ExcelExport(table);
            excelExport.setUseTableFormatPropertyValue(true);
            excelExport.excludeCollapsedColumns();
            excelExport.setReportTitle("Demo Report");
            excelExport.export();
        }));
    }


    // basic bean for data binding
    public static class Person {
        private String name;
        private String surname;
        private int age;

        public Person(String name, String surname, int age) {
            this.name = name;
            this.surname = surname;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSurname() {
            return surname;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    // custom converter
    private static class AgeConverter implements Converter<String, Integer> {
        @Override
        public Integer convertToModel(String value, Class<? extends Integer> targetType, Locale locale) throws ConversionException {
            return Integer.valueOf(value.substring(0, value.indexOf(" years")));
        }

        @Override
        public String convertToPresentation(Integer value, Class<? extends String> targetType, Locale locale) throws ConversionException {
            return String.valueOf(value) + " years";
        }

        @Override
        public Class<Integer> getModelType() {
            return Integer.class;
        }

        @Override
        public Class<String> getPresentationType() {
            return String.class;
        }
    }
}

输出: