gpt4 book ai didi

java - 将 jasper 报告导出为 XLS 格式时,未发生单元格扩展

转载 作者:行者123 更新时间:2023-11-30 07:38:23 26 4
gpt4 key购买 nike

我有一个带有静态字段和文本字段的ireport。数据库由大部分数据组成,对于数据库中的单行,有多行数据。所以当我通过java代码将jasper报告导出为PDF格式时正在拉伸(stretch)、阐述和打印整个数据,没有任何丢失的行,但是当我导出到 XLS 时,它丢失了数据,而且单元格的拉伸(stretch)没有发生。我将显示一个图像以便更好地理解。 enter image description here

正如您在上面所看到的,数据在 PDF 中打印得非常清晰。 enter image description here

在上面,即 XLS 格式数据被隐藏或在某种意义上丢失。所以我的问题是当报表导出为 XLS 格式时如何在单元格中显示整个数据。我在下面发布 java 代码。

    ServletContext context = request.getServletContext(); 
String fullPath = context.getRealPath("/WEB-INF/reports/report5.jrxml");
InputStream input = new FileInputStream(new File(fullPath));
JasperDesign jasperDesign = JRXmlLoader.load(input);

System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport,map,conn);
byte bytes[] = new byte[10000];
//String result = JasperRunManager.runReportToHtmlFile("./usertemplates/test.jasper" , parameters, conn);



JRXlsxExporter exporter = new JRXlsxExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setWhitePageBackground(true);
configuration.setDetectCellType(true);
configuration.setFontSizeFixEnabled(true);
exporter.setConfiguration(configuration);
exporter.exportReport();
//System.out.println("Size of byte array:"+xlsReport.size());
//bytes = xlsReport.toByteArray();
String fileName = "InvoiceReport.xls";
response.setHeader("Content-Disposition", "inline; filename="
+ fileName);
response.setContentType("application/vnd.ms-excel");
//System.out.println("After JasperPrint = 1");
response.setContentLength(xlsReport.size());
//System.out.println("After JasperPrint = 2");
//xlsReport.close();
//System.out.println("After JasperPrint = 3");

OutputStream outputStream = response.getOutputStream();
System.out.println("After JasperPrint = 4");
xlsReport.writeTo(outputStream);
outputStream.flush();
//outputStream.close();
}
catch(Exception e)
{e.printStackTrace();}

%>

jrxml 代码如下。

report5.jrxml

<detail>
<band height="20" splitType="Prevent">
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="13" height="20" isPrintWhenDetailOverflows="true" uuid="24b49d3f-5c78-4a19-a4d2-e2cbff95a40d">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{serial number}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="13" y="0" width="242" height="20" isPrintWhenDetailOverflows="true" uuid="4332ae75-6371-4879-b805-9d07254f0784">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{DescriptionOfGoods}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="309" y="0" width="86" height="20" isPrintWhenDetailOverflows="true" uuid="54828e8d-bae0-4669-873e-36de8bf54446">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{PerUnitPrice}]]></textFieldExpression>
</textField>
<staticText>
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="395" y="0" width="64" height="20" isPrintWhenDetailOverflows="true" uuid="e02408a1-2bcd-4fb9-94d3-ed0c0c1d8ff6">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[Nos]]></text>
</staticText>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="459" y="0" width="96" height="20" isPrintWhenDetailOverflows="true" uuid="00ba44d4-b287-4a29-8bf3-1ad07da760e8">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{Quantity} *$F{PerUnitPrice}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="255" y="0" width="54" height="20" isPrintWhenDetailOverflows="true" uuid="d74b9bd4-9608-44a7-b46e-55f39ed0c852">
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<box>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" pdfFontName="Times-Roman" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{Quantity}]]></textFieldExpression>
</textField>
</band>
</detail>

最佳答案

首先,您在 jasper 报告中使用已弃用方法,请考虑更新您的代码:

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setWhitePageBackground(true);
configuration.setDetectCellType(true);
\\... set all your configuration like above
exporter.setConfiguration(configuration);
exporter.exportReport();

您需要在包含需要位匹配的文本的 textField 中设置 reportElement 的属性。

<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>

如果这不能按预期工作,您的解决方案是:

  1. 使 Excel 更改字体大小(以适合)

    configuration.setFontSizeFixEnabled(true)

Flag for decreasing font size so that texts fit into the specified cell height.

  • 手动设置 Excel 中的列宽
  • net.sf.jasperreports.export.xls.column.width,具有以像素为单位测量的整数值的属性

    如果您仍然不满意,请转至 ,加载工作簿并根据需要进行修改......

    关于java - 将 jasper 报告导出为 XLS 格式时,未发生单元格扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35054440/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com