问题描述
我想导出具有本例所示的SelectOneMenu的<p:dataTable>
JSF Primefaces SelectOneMenu
I want to Export the <p:dataTable>
which has a SelectOneMenu as shown in this Example JSF Primefaces SelectOneMenu
因此,我使用了素面符号<p:dataExporter type="xls" target="datatbleId" fileName="cars"/>
So, i used the primefaces <p:dataExporter type="xls" target="datatbleId" fileName="cars"/>
其中generated a xls file
为以下格式:
------------------------------------
Name | Car
----------------------------------------
ABC | 1
DDD | 2
我该怎么办?因此,我可以生成以下格式的xls
:
What should i do ? So, that i can generate a xls
in the below format :
------------------------------------
Name | Car
----------------------------------------
ABC | Toyota
DDD | Ford
我的数据表代码如下:
<p:dataTable id="studentDtble" var="studentDetail" value="#{studentController.studentList}" emptyMessage="No records found">
<p:column styleClass="ralign">
<f:facet name="header">
<h:outputText value="Student Id " />
</f:facet>
<p:commandLink id="studentCmdLnk" action="#{profileHandler.showStudentProfile}" update=":tabView:loanOvrviewForm">
<h:outputText id="studentIdOutTxt" value="#{studentDetail.studentId}" />
</p:commandLink>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Class" />
</f:facet>
<h:outputText id="classoutTxt" value="#{studentDetail.class}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Student Name" />
</f:facet>
<h:outputText id="nameoutTxt" value="#{studentDetail.studentName}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Contact No" />
</f:facet>
<h:inputText id="contactInTxt" value="#{studentDetail.studentContact}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="DOJ" />
</f:facet>
<h:outputText id="dojDateOutTxt" value="#{studentDetail.dojDate}">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Group/Branch" />
</f:facet>
<p:selectOneMenu id="branchesLstBox" value="#{studentDetail.branchList.branchId}">
<f:selectItems value="#{studentController.getAllBranches()}" var="branches" itemValue="#{branches.branchId}" itemLabel="#{branches.branchName}" />
</p:selectOneMenu>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Remarks" />
</f:facet>
<p:inputText id="remarksInTxt" value="#{studentDetail.remarks}" />
</p:column>
</p:dataTable>
推荐答案
我不确定您可以使用dataExporter来做到这一点,但是您可以实现自己的导出器,这很容易,您只需要Apache POI.
I'm not sure you can do it with dataExporter, however you can implement your own exporter, it's quite easy, you just need Apache POI.
样品:
放置在小界面中的按钮.
A button to put in the facelet.
<p:commandButton icon="ui-icon-arrowstop-1-s" value="XLS" onclick="PrimeFaces.monitorDownload(start, stop)">
<p:fileDownload value="#{dataExporter.generateXLS()}" />
</p:commandButton >
请求范围的托管Bean(但您也可以实现WebServlet)
A request scoped managed bean (but you can implement a webservlet too)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
@ManagedBean(name="dataExporter")
@RequestScoped
public class DataExporter {
public StreamedContent generateXLS() {
if (list != null) { // the list you are using in the data Table
if (!list.isEmpty()) {
String filePath = "E:\\tmp\\";
String fileName = "xlsFile";
String a = filePath + fileName;
XSSFWorkbook new_workbook = new XSSFWorkbook();
XSSFSheet sheet = new_workbook.createSheet("SAMPLE");
int cellnum = 0;
int rownum = 0 ;
Row row = sheet.createRow(0);
for (Object object : list) { //loop through the data and add them to the cell
row = sheet.createRow(rownum++);
cellnum = 0;
cell = row.createCell(cellnum++);
if (object.getColumn1() == null) {
cell.setCellValue("");
} else {
cell.setCellValue(object.getColumn1());
}
cell = row.createCell(cellnum++);
if (object.getColumn2() == null) {
cell.setCellValue("");
} else {
cell.setCellValue(object.getColumn2());
}
}
FileOutputStream output_file = null;
try {
output_file = new FileOutputStream(new File(a));
} catch (FileNotFoundException ex) {
}
try {
new_workbook.write(output_file);
} catch (IOException ex) {
}
try {
output_file.close(); //close the file
} catch (IOException ex) {
}
FileInputStream fis = null;
try {
fis = new FileInputStream(a);
} catch (FileNotFoundException ex) {
}
InputStream is = fis;
String mimeType = "";
try {
mimeType = URLConnection.guessContentTypeFromStream(is);
} catch (IOException ex) {
}
return new DefaultStreamedContent(is, mimeType, fileName);
}
免责声明:未经测试,您需要使用自己的对象和实体来修改代码.
Disclaimer : Not tested, and you need to adapt the code with your own objects and entities.
这篇关于Primefaces p:dataExporter的问题,该数据使用SelectOneMenu导出的数据表与预期不符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!