问题
I want to Export the <p:dataTable>
which has a SelectOneMenu as shown in this Example JSF Primefaces SelectOneMenu
So, i used the primefaces <p:dataExporter type="xls" target="datatbleId" fileName="cars"/>
which generated a xls file
as the below format:
------------------------------------
Name | Car
----------------------------------------
ABC | 1
DDD | 2
What should i do ? So, that i can generate a xls
in the below format :
------------------------------------
Name | Car
----------------------------------------
ABC | Toyota
DDD | Ford
My DataTable Code Looks like this:
<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>
回答1:
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.
Sample :
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 >
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.
来源:https://stackoverflow.com/questions/24889066/issue-with-primefaces-pdataexporter-which-exports-a-datatable-with-selectoneme