自定义导出p:从.xls到.pdf的dataTable数据

我来自马其顿,在我的国家我们使用西里尔字体。

我正在NetBeans IDE中开发一个Web应用程序,并使用PrimeFaces 5.2框架。 为了导出,我使用poi-3.9.jaritext-2.7.1.jar 。 我创build了一个表格<p:dataTable> ,用一些西里尔文本列(像名字,姓氏等列)来填充。 在我的应用程序中,我喜欢使用导出到PDF并导出到Excel,因此客户端可以从表中导出数据作为报告或其他内容。

导出到Excel的效果很好。 我可以看到所有的数据(包括西里尔语文本),并可以在postProcessor中修改它。 在后处理程序中,我使用蓝色背景创build表格的标题,为每个单元格添加边框,如果行数更多,则行更大,自动调整所有列等。

来自xhtml的代码:

  <p:dataTable var="u" value="#{WebClient.allRemoteParkingUsers}" id="uncheckedParkingRequests" rowKey="#{u.ID}" editable="true" tableStyle="table-layout: auto;" emptyMessage="Не постои ниеден паркинг клиент според податоците по кои пребарувате." filteredValue="#{WebClient.filtered_users}" widgetVar="client_table" paginator="true" rows ="20" rowsPerPageTemplate="20,50,100" paginatorAlwaysVisible="true" paginatorPosition="bottom" paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown} {Exporters}"> <f:facet name="header" id="naslovTabela"> <p:outputLabel value="Листа на паркинг клиенти"/> <p:outputPanel style="text-align: right; font-size: 18px;"> <h:outputText value="Глобално пребарување:" style="margin-right: 5px;"/> <p:inputText id="globalFilter" onkeyup="PF('client_table').filter()" style="width:200px" placeholder="внесете клучен збор" /> </p:outputPanel> </f:facet> <f:facet name="{Exporters}"> <h:commandLink style="color: white; font-size: 15px; text-align: center"> <h:outputText value="Сними во Excel" /> <img src="images/excel_icon.png" alt=""/> <p:dataExporter type="xls" target="uncheckedParkingRequests" fileName="parking_user_excel" postProcessor="#{doc.postProcessXLS}" /> </h:commandLink> <h:commandLink style="color: white; font-size: 15px; text-align: center"> <h:outputText value="Сними во PDF" /> <img src="images/pdf_icon.png" alt=""/> <p:dataExporter type="pdf" target="uncheckedParkingRequests" fileName="file" encoding="UTF-8" /> </h:commandLink> </f:facet> ... </p:dataTable> 

DocumentBuilder类:

  import java.io.Serializable; import javax.faces.bean.ManagedBean; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.util.HSSFColor; //POI libraries to read Excel File import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import java.util.Iterator; //itext libraries to write PDF file /** * * @author adrian */ @ManagedBean(name = "doc", eager = true) public class DocumentBuilder implements Serializable { public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0); //Aply style for all cells Iterator rowIter = sheet.rowIterator(); boolean first_row = true; while (rowIter.hasNext()) { HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); int row_max_number_of_breaks = 0; while (cellIter.hasNext()) { HSSFCell cell = (HSSFCell) cellIter.next(); HSSFCellStyle cell_style = wb.createCellStyle(); if (first_row) { //Header cell cell_style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); cell_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); } cell_style.setAlignment(HSSFCellStyle.ALIGN_CENTER); cell_style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER); cell_style.setBorderBottom(HSSFCellStyle.BORDER_THIN); cell_style.setBorderTop(HSSFCellStyle.BORDER_THIN); cell_style.setBorderRight(HSSFCellStyle.BORDER_THIN); cell_style.setBorderLeft(HSSFCellStyle.BORDER_THIN); cell_style.setWrapText(true); cell.setCellStyle(cell_style); String cell_value = cell.getStringCellValue(); int cell_number_of_break = 0; while (cell_value.contains("<br/>")) { cell_value = cell_value.replaceFirst("<br/>", "\n"); cell.setCellValue(cell_value); cell_number_of_break += 1; } if (row_max_number_of_breaks < cell_number_of_break) { row_max_number_of_breaks = cell_number_of_break; } } myRow.setHeight(Short.valueOf(String.valueOf((myRow.getHeight() * (row_max_number_of_breaks + 1))))); first_row = false; } //Resize columns to fit data int noOfColumns = sheet.getRow(0).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); } } } 

当我尝试使用PrimeFaces导出器将相同的表格导出为PDF时,每个带有西里尔字体文本的字段都不可见。 当我从我的Excel文件导出到.pdf时,一切都很好(我可以看到西里尔文本,另外我从DocumentBuilder类中获得了额外的devise)。

我已经研究过,并且已经尝试将编码改为UTF-8,CP1252,Windows-1252等,但是还是一无所获。

是否有可能创build一个自定义导出器,首先创buildExcel文档,将其导出为PDF并发送(在postProcessor或preProcessor中有一些魔术代码:))

所以我可以保持从Excel的devise和工作出口到PDF。