我试图将jasper报告导出到.docx文件;但是我收到了这个错误
java.lang.ClassCastException: java.lang.String cannot be cast to net.sf.jasperreports.engine.JasperPrint
我使用java来开发它。
以下是我使用的代码
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);
}
}
答案 0 :(得分:0)
最后我找到了答案!
我删除了这两行
exp.setParameter(JRExporterParameter.JASPER_PRINT,printFileName);
exp.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"src/POS_bill/sample_report2.docx");
并将其替换为
exporter.setExporterInput(new SimpleExporterInput(printFileName));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("src/POS_bill/samplereport2.docx"));
现在它运作得很好!