首页 > 解决方案 > 如何使用 dataExporter 从数字中删除空格?

问题描述

我的应用程序中有许多数据表(用作自定义标签),所有这些数据表都可以使用 dataExporter 函数导出到一个 Excel 文件中。我的问题是,包含数字的列在前端被格式化(如:124 284,4),如果它们被导出到 Excel 中,它们不能被处理(例如汇总)为数字。那么...如何仅从这些列中删除空格,这些列仅包含 postProcess 函数中的数字?这甚至可能吗?

我找到了这个 postProcessor 函数,但这正在转换一切,而不仅仅是数字行:

public void postProcessXLS(Object document) {

        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);

        Iterator<Row> rowIterator = sheet.iterator();

        if (rowIterator.hasNext()) {
            rowIterator.next();
        }

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getColumnIndex() > 1) {
                    if (!cell.getStringCellValue().isEmpty()) {
                        cell.setCellValue(Double.valueOf(cell.getStringCellValue().replace("'", "")));
                    }
                }
            }
        }
    }

这是我的 dataExporter 按钮:

            <h:commandLink style="float:right">
                <p:graphicImage name="/images/excel.png" width="24"/>
                <p:dataExporter type="xls" target="#{id}" fileName="list" />
            </h:commandLink>

预期结果:例如 98 923,5 将在 Excel 中导出为 98923,5。所有其他字符串将保持不变。

标签: primefacesapache-poi

解决方案


最佳选择包括扩展org.primefaces.component.export.ExcelExporter和覆盖exportValue应用自定义的方法。在那里,您可以完全访问输出组件。将您的自定义导出器扩展实例提供给p:dataExporter customExporter属性。

一个示例可能如下所示:

<h:form>
    <h:commandLink title="hejjj!">
        Export to XLS
        <p:dataExporter type="xls" customExporter="#{myBean.customExporter}"
            target="tbl" fileName="anyFilename" />
    </h:commandLink>

    <p:dataTable id="tbl" value="#{myBean.rows}" var="row">
        <p:column headerText="Formatted Numbers with Spaces">
            <h:outputText value="#{row.number}">
                <f:converter .../>
                <f:attribute name="isFormattedNumber" value="1" />
            </h:outputText>
        </p:column>
    </p:dataTable>
</h:form>

请注意<f:attribute name="isFormattedNumber" value="1" />添加到h:outputTextwhich 将有助于在使用CustomExcelExporter以下导出时区分组件:

package my.package;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.export.ExcelExporter;

public class CustomExcelExporter extends ExcelExporter {

    @Override
    protected String exportValue(FacesContext context, UIComponent component) {
        String exportedValue = super.exportValue(context, component);
        if (component.getAttributes().containsKey("isFormattedNumber")) {
            return exportedValue.replace(" ", "");
        } else {
            return exportedValue;
        }
    }
}

MyBean 只是为了显示创建导出的位置:

package my.package;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.primefaces.component.export.Exporter;

@Named
@RequestScoped
public class MyBean {
    private Exporter customExporter;

    @PostConstruct
    public void init() {
        customExporter = new CustomExcelExporter();
    }
    /** getters, setters */
}

另一种选择是通过将自定义属性添加到所有相关列来尝试自定义导出 功能。exportFunction


推荐阅读