简体   繁体   English

Jasper生成Excel文件错误“ Excel在..中发现了不可读的内容”

[英]Jasper generated Excel file error “Excel found unreadable content in .. ”

I'm using Java and Jasper to generate reports in .xlsx format. 我正在使用JavaJasper生成.xlsx格式的报告。 And I was successful and I have components in my application which create actual reports with data. 而且我很成功,我的应用程序中有一些组件可以使用数据创建实际的报告。

recently I was trying to add another component and the report generation was successful , but the generated .xslx file wont open with MS Excel or any other. 最近,我试图添加另一个组件,并且报告生成成功,但是生成的.xslx文件无法用MS Excel或任何其他格式打开。

the code parts are below. 代码部分如下。

    ArrayList<ReportBean> reportBean =  new ArrayList<ReportBean>();

    response.setHeader("Content-Disposition",  "attachment;filename=BudgetaryQuote.xlsx");
    response.setContentType("application/xlsx");
    OutputStream outStream = response.getOutputStream();

    Report report = new Report(template, "xlsx", reportBean, outStream);
    //Report(String template, String format, ArrayList dataList,OutputStream outputStream) 
    report.generateReport();

in generateReport() it has , 在generateReport()中,

        InputStream inputStream = new FileInputStream(new File(this.template));  
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(this.dataList); 
        JasperDesign jasperDesign = JRXmlLoader.load(inputStream); 
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

        if(this.format.equalsIgnoreCase(XLSX)) {

            JRXlsxExporter Xlsxexporter = new JRXlsxExporter();
            Xlsxexporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            Xlsxexporter.setParameter(JRExporterParameter.OUTPUT_STREAM,  outputStream);
            Xlsxexporter.exportReport();            

        }

the .jrxml file (It's pretty much blank) .jrxml文件(几乎为空白)

     <?xml version="1.0" encoding="UTF-8"?>
     <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="timeSheetDetailedReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ffef809-ea2d-4533-a0b9-bdd2d055afbe">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch"/>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>

There is an error like in the image. 图像中有错误。 "Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."

How can I check what is the wrong with the file ? 如何检查文件有什么问题?

在此处输入图片说明

I just solved the problem. 我刚刚解决了这个问题。 But it's not clear. 但是还不清楚。

my code had the below code , 我的代码具有以下代码,

ArrayList<ReportBean> reportBean =  new ArrayList<ReportBean>();
Report report = new Report(template, "xlsx", reportBean, outStream);
report.generateReport();

The problem was the ArrayList reportBean was empty. 问题是ArrayList reportBean为空。 The I add an dummy ReportBean to the list and the problem was solved. 我将虚拟ReportBean添加到列表中,问题已解决。

But I have no idea how it solved the problem and why 但是我不知道它是如何解决问题的,为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM