我的下载p:CommandButton:
有问题xhtml代码:
<p:commandButton id="downloadExcelFile" rendered="#{!login.connectedUserHasRole('Client')}" value="Exporter" actionListener="#{etudeManagedBean.exportChartBilan}" ajax="false" icon="ui-icon-arrowthichk-s" >
<p:fileDownload value="#{etudeManagedBean.bilanFile}"/>
</p:commandButton>
ManagedBean的代码:
@ManagedBean(name = "etudeManagedBean")
@ViewScoped
public class EtudeManagedBean implements Serializable {
private static final Logger LOGGER = Logger.getLogger(EtudeManagedBean.class);
@ManagedProperty("#{login}")
private Login login;
@EJB
private EtudeFacadeLocal etudeFacade;
@EJB
private PreRequisFacadeLocal preRequisFacade;
@EJB
private UtilisateurFacadeLocal utilisateurFacade;
@EJB
private Utilisateur selectedUtilisateur;
private EtudeModel etudeModel;
private StreamedContent bilanFile;
private StreamedContent templateFile;
private List<String> selectedJalonList;
private boolean skip, requestFromCbCs;
private List<Cb> listCb, listCbByEtude;
private List<PreRequis> listPreRequisByEtude;
private List<BilanLivraisonDto> listSemaineLivFinal;
private PieChartModel pieModel;
private PieChartModel pieModelBilanLiv;
@PostConstruct
public void init() {
etudeForFiltering = new Etude();
selectedEtude = new Etude();
initialisationUser();
recherche();
findListBilanLivraisonDto();
createPieModel();
createPieModelBIlanLiv();
}
public void exportChartBilan() {
try {
FacesContext fc = FacesContext.getCurrentInstance();
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("dd-MM-yyyy");
java.util.Date date = new java.util.Date();
String exportBilanDate = formater.format(date);
String nameBilanFile = "Bilan_" + exportBilanDate + ".xlsx";
FileInputStream chart_file_input = null;
if (!listSemaineLivFinal.isEmpty()) {
String listePath = "/pages/generatedFiles/BilanExport.xlsx";
String listePathCopie = "/pages/generatedFiles/BilanExportCopie.xlsx";
String tempBilanFileRealPath = fc.getExternalContext().getRealPath(listePath);
String tempCopieBilanFileRealPath = fc.getExternalContext().getRealPath(listePathCopie);
InputStream inputXlsFile = null;
ExcelFileHelper helper = new ExcelFileHelper();
helper.deleteAndCreateExcelFile(tempCopieBilanFileRealPath, tempBilanFileRealPath);
String tempFilePath = fc.getExternalContext().getRealPath(listePath);
System.out.println("tempFilePath: " + tempFilePath);
inputXlsFile = fc.getExternalContext().getResourceAsStream(listePath);
chart_file_input = new FileInputStream(new File(tempFilePath));
XSSFWorkbook my_workbook = null;
my_workbook = new XSSFWorkbook(chart_file_input);
XSSFSheet my_sheet = my_workbook.getSheetAt(0);
// helper.clearContentCell(tempFilePath);
inputXlsFile = fc.getExternalContext().getResourceAsStream(listePath);
List<BilanLivraisonDto> etudeList = new ArrayList<>();
for (BilanLivraisonDto etude : listSemaineLivFinal) {
etudeList.add(etude);
}
List<String> totalList = new ArrayList();
totalList.add(String.valueOf(getTotaleNbreLivTotale()));
totalList.add(String.valueOf(getTotaleNbreLivPartiel()));
totalList.add(String.valueOf(getTotaleNbreLivOptim()));
totalList.add(String.valueOf(getTotaleNbreLivPDHPDH()));
totalList.add(String.valueOf(getTotaleNbreLivPDHSDH()));
totalList.add(String.valueOf(getTotaleNbreLivSDHSDH()));
List<String> percentageList = new ArrayList();
percentageList.add(String.format("%.2f", getPourcentageForNbreLivTotale() * 100));
percentageList.add(String.format("%.2f", getPourcentageForNbreLivPartiel() * 100));
percentageList.add(String.format(" %.2f", getPourcentageForNbreLivOptim() * 100));
percentageList.add(String.format(" %.2f", getPourcentageForNbreLivPDHPDH() * 100));
percentageList.add(String.format("%.2f", getPourcentageForNbreLivPDHSDH() * 100));
percentageList.add(String.format(" %.2f", getPourcentageForNbreLivSDHSDH() * 100));
helper.editGeneratedExcelFoBilan(my_workbook, etudeList, totalList, percentageList);
/* Create JFreeChart object that will hold the Pie Chart Data */
DefaultPieDataset my_pie_chart_data = new DefaultPieDataset();
DefaultPieDataset my_pie_chart_data_liv = new DefaultPieDataset();
my_pie_chart_data.setValue("SDH-SDH", getPourcentageForNbreLivSDHSDH() * 100);
my_pie_chart_data.setValue("PDH-SDH", getPourcentageForNbreLivPDHSDH() * 100);
my_pie_chart_data.setValue("PDH-PDH", getPourcentageForNbreLivPDHPDH() * 100);
my_pie_chart_data.setValue("Optim", getPourcentageForNbreLivOptim() * 100);
my_pie_chart_data_liv.setValue("Livraison totale", getPourcentageForNbreLivTotale() * 100);
my_pie_chart_data_liv.setValue("Livraison partielle", getPourcentageForNbreLivPartiel() * 100);
final JFreeChart myPieChartLiv;
final JFreeChart myPieChartTypeConn;
myPieChartLiv = createChart(my_pie_chart_data_liv, "Bilan livraison");
myPieChartTypeConn = createChart(my_pie_chart_data, "Livraison par typologie");
int width = 320; /* Width of the chart */
int height = 300; /* Height of the chart */
float quality = 1; /* Quality factor */
ByteArrayOutputStream chart_out = new ByteArrayOutputStream();
ByteArrayOutputStream chart_out2 = new ByteArrayOutputStream();
ChartUtilities.writeChartAsJPEG(chart_out, quality, myPieChartTypeConn, width, height);
ChartUtilities.writeChartAsJPEG(chart_out2, quality, myPieChartLiv, width, height);
int my_picture_id = my_workbook.addPicture(chart_out.toByteArray(), Workbook.PICTURE_TYPE_JPEG);
chart_out.close();
chart_file_input.close();
FileOutputStream out = new FileOutputStream(new File(tempFilePath));
my_workbook.write(out);
out.close();
this.bilanFile = new DefaultStreamedContent(new FileInputStream(tempFilePath), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", nameBilanFile);
System.out.println("le bilan est " + this.bilanFile);
if (this.bilanFile == null) {
JsfUtil.addErrorMessage("Le fichier indisponible");
}
JsfUtil.addSuccessMessage("La génération du fichier est effectué avec succès.");
}
} catch (IOException ex) {
java.util.logging.Logger.getLogger(EtudeManagedBean.class.getName()).log(Level.SEVERE, null, ex);
JsfUtil.addErrorMessage("Le fichier indisponible");
}
}
/**
* @return the bilanFile
*/
public StreamedContent getBilanFile() {
return bilanFile;
}
/**
* @param bilanFile the bilanFile to set
*/
public void setBilanFile(StreamedContent bilanFile) {
this.bilanFile = bilanFile;
}
}
但在这种情况下,我无法在页面上显示错误。
答案 0 :(得分:0)
根据primefaces doc,当您在<p:fileDownlaod .../>
或<p:commandButton>
内使用<p:commandLink>
时,属性ajax="false"
是必需的,因为fileDownload需要整页刷新。您可以查找其他选项例如<p:dataExporter>
或更高级的<pe:exporter>
。您还可以根据需要自定义Excel文件。