Primefaces 如何使用dataExporter从数字中删除空格?
我的应用程序中有许多数据表(用作自定义标记),所有这些数据表都可以使用dataExporter功能导出到一个Excel文件中。我的问题是,包含数字的列在前端被格式化(比如:124 284,4),如果它们被导出到Excel中,它们就不能作为数字处理(例如Summary)。所以如何仅从这些列中删除空格,这些列在后处理函数中仅包含数字?这可能吗 我找到了这个后处理器函数,但它正在转换所有内容,而不仅仅是数字行:Primefaces 如何使用dataExporter从数字中删除空格?,primefaces,apache-poi,Primefaces,Apache Poi,我的应用程序中有许多数据表(用作自定义标记),所有这些数据表都可以使用dataExporter功能导出到一个Excel文件中。我的问题是,包含数字的列在前端被格式化(比如:124 284,4),如果它们被导出到Excel中,它们就不能作为数字处理(例如Summary)。所以如何仅从这些列中删除空格,这些列在后处理函数中仅包含数字?这可能吗 我找到了这个后处理器函数,但它正在转换所有内容,而不仅仅是数字行: public void postProcessXLS(Object document) {
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("'", "")));
}
}
}
}
}
public void后处理xls(对象文档){
HSSF工作手册wb=(HSSF工作手册)文件;
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow标题=sheet.getRow(0);
迭代器rowIterator=sheet.Iterator();
if(roweiterator.hasNext()){
roweiterator.next();
}
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
if(cell.getColumnIndex()>1){
如果(!cell.getStringCellValue().isEmpty()){
cell.setCellValue(Double.valueOf(cell.getStringCellValue().replace(“,”));
}
}
}
}
}
这是我的数据导出器按钮:
<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。所有其他字符串将保持不变。最佳选项包括扩展
org.primefaces.component.export.ExcelExporter
并覆盖exportValue
方法以应用自定义。在这里,您可以完全访问输出组件。为p:dataExporter
属性提供自定义导出器扩展实例
示例可能如下所示:
<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>
MyBean只是为了显示Export的创建位置:
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 */
}
另一个选项是通过向所有相关列添加自定义属性来尝试该功能。最佳选项包括扩展
org.primefaces.component.export.ExcelExporter
并覆盖exportValue
方法以应用自定义。在这里,您可以完全访问输出组件。为p:dataExporter
属性提供自定义导出器扩展实例
示例可能如下所示:
<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>
MyBean只是为了显示Export的创建位置:
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 */
}
另一种选择是,通过向所有相关列添加自定义属性来尝试该功能。然后仅对需要的列执行此操作。如果在支持数据表的对象中以数字形式存储这些值,我几乎100%确定这些值是以数字形式存储的。这对我来说是个新问题,我如何才能做到“只针对需要的列”?datatable至少在30个表上使用,如何过滤这些列?代码中有一个列索引。每个表上的列索引都不同,正如我所说的,这是在30个不同的datatable上使用不同的列。请仅对需要的列执行此操作。如果在支持数据表的对象中以数字形式存储这些值,我几乎100%确定这些值是以数字形式存储的。这对我来说是个新问题,我如何才能做到“只针对需要的列”?datatable至少在30个表上使用,如何过滤这些列?代码中有一个列索引。每个表上的列索引都不同,正如我所说,这是在30个不同的datatable上使用不同的列。我想在我的p列上:导出时弄乱了数字。它在UI上非常好,但在导出方面却不好。如果我按下导出按钮,是否可能不转换数字?@VORiAND我不知道这是如何实现的-但我更新了答案,提供了customExporter实现的详细信息。我需要做什么才能在p:dataExporter上使用customExporter参数?它对我说:“组件dataExporter中没有定义customExporter属性”:(Primefaces 7.0,我不确定它是否在早期版本中可用。我认为在我的p:column中,在导出上弄乱了数字。它在UI上非常好,但在导出方面不是。如果我按下导出按钮,是否可能不转换数字?@VORiAND我不知道这怎么可能-但我更新了我的答案,给出了详细信息l关于customExporter实现。我需要做什么才能在p:dataExporter上使用customExporter参数?它对我说:“组件dataExporter中未定义customExporter属性”:(Primefaces 7.0,我不确定它是否在早期版本中可用。