Search code examples
javanetbeansjasper-reportsexport

Jasper report error when exporting report to docx


I'm trying to export the jasper report to .docx file; but I'm getting this error

java.lang.ClassCastException: java.lang.String cannot be cast to net.sf.jasperreports.engine.JasperPrint

I'm using java to develop this.

Here is the code I used

public void generateReport2() throws PrinterException {

try {  
String sourceFileName = "src/POS_bill/esFourReport.jasper";
String printFileName = null;
DataBeanFactory2 DataBean = new DataBeanFactory2();
JRBeanCollectionDataSource beanColDataSource = new      JRBeanCollectionDataSource(DataBean.generateCollection());
Map parameters = new HashMap();
printFileName = JasperFillManager.fillReportToFile(
     sourceFileName,
     parameters,
     beanColDataSource);


JRDocxExporter exp=new JRDocxExporter();   
exp.setParameter(JRExporterParameter.JASPER_PRINT,printFileName);
exp.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"src/POS_bill/sample_report2.docx");
exp.exportReport();


} catch (Exception e) {

System.out.println(e);
} 
}

Solution

  • Finally I found the answer!

    I removed these two lines

    exp.setParameter(JRExporterParameter.JASPER_PRINT,printFileName);
    exp.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"src/POS_bill/sample_report2.docx");
    

    and replaced it with these

    exporter.setExporterInput(new SimpleExporterInput(printFileName));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("src/POS_bill/samplereport2.docx"));
    

    and now it works finely!