primefaces - 如何使用 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。所有其他字符串将保持不变。
解决方案
最佳选择包括扩展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:outputText
which 将有助于在使用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
推荐阅读
- sql-server - 具有给定参数的 SQL Server 存储过程插入查询
- mysql - 将 Mysql 查询转换为 Mongodb 查询
- jenkins-plugins - Jenkins 插件监控卡住的工作
- javascript - 日期时间与 t 格式转换 dd mmm yyyy hh mm ss 在 java 或 javascript
- mysql - SQL 查询语法错误,带有 INNER JOIN 的 UPDATE 语句
- ios - XCode 9.4.1 上的 Swift + Objective C 框架的问题
- android - 如何使用内部 PendingIntent 序列化对象
- javascript - 繁忙的图像不断加载,不考虑响应
- reactjs - 如何指定比指定的 material-ui 最大空间更多的空间?
- xamarin - OnBackButtonPressed() 没有第二次被触发