我对部分文本使用了粗体,所以它看起来像:
"<style isBold = 'true'>" + $P{REPORT_RESOURCE_BUNDLE}.getString("report.label.foo") +": "+"</style>"+$F{foo}
在 jrxml 中这个 textField
看起来像:
<textField>
<reportElement style="moduleBorderColumnStyle" mode="Opaque" x="0" y="0" width="555" height="20" uuid="6adbbfa7-e549-4378-903c-04095c2f34c4"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<style isBold = 'true'>" +
$P{REPORT_RESOURCE_BUNDLE}
.getString("report.label.foo")
+": "+"</style>"+$F{foo}]]></textFieldExpression>
</textField>
TextField 标记属性 - 样式化
它非常适合 PDF 和 HTML。但是,我对 XLSX 使用相同的方法时遇到问题。
不幸的是,即使在直接将字体大小设置为 14 之后(我之前尝试从样式设置它)我正在为整个标签获取字体 11 callibri(这是默认字体),它正在使用标签 <style isBold='true'>
.
我对 <b> text </b>
进行了同样的尝试和标记 = HTML - 结果没有改变。
结论:XLSX 中的任何样式文本对字体不敏感(将其设置为默认值),如何解决?
编辑:
我发现问题出在我申请的风格上 textField
之前,但是问题仍然存在于 excel 中。 <style>
标记只是将其覆盖为默认字体和字体大小。
最佳答案
我可以在导出到 xlsx
时确认相同的错误,样式被忽略,这似乎与为 XSSFSheet
中的单元格创建 RichTextString
有关>。 (不正确/没有字体设置为 RichTextString
?)
EDIT: I have created a bug issue, that is marked as solved for next release (current release was v6.3.0)
重现错误的简单示例
jrxml (SimpleTest.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="SimpleTest" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="e4188d8a-c7f9-4f7d-8f0f-ada07b89d42f">
<style name="test" mode="Transparent" forecolor="#CC0000" fontSize="14"/>
<detail>
<band height="20">
<textField isStretchWithOverflow="true">
<reportElement style="test" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="200" height="20" uuid="6d5644bf-480e-4ed2-831b-3ed043f38f70"/>
<textElement verticalAlignment="Middle" markup="html">
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA["<b>TEST</b> TEXT"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement style="test" positionType="Float" stretchType="RelativeToTallestObject" x="200" y="0" width="200" height="20" uuid="f876d0a3-136b-468c-b3bd-bd9cd5475ca9"/>
<textElement verticalAlignment="Middle" markup="html">
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA["TEXT2"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
导出到 xls 和 xlsx 的 java 代码
JasperReport report = JasperCompileManager.compileReport("SimpleTest.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(report,new HashMap<String, Object>(), new JREmptyDataSource(1));
//Export to excel xls
JRXlsExporter exporterXls = new JRXlsExporter();
File outputFile = new File("excelTest.xls");
exporterXls.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterXls.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
SimpleXlsReportConfiguration configXls = new SimpleXlsReportConfiguration();
configXls.setDetectCellType(true);
configXls.setRemoveEmptySpaceBetweenColumns(true);
configXls.setRemoveEmptySpaceBetweenRows(true);
configXls.setCollapseRowSpan(true);
configXls.setWhitePageBackground(false);
exporterXls.setConfiguration(configXls);
exporterXls.exportReport();
//Export to excel xlsx
JRXlsxExporter exporterXlsx = new JRXlsxExporter();
File output = new File("excelTest.xlsx");
exporterXlsx.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterXlsx.setExporterOutput(new SimpleOutputStreamExporterOutput(output));
SimpleXlsxReportConfiguration configXlsx = new SimpleXlsxReportConfiguration();
configXlsx.setDetectCellType(true);
configXlsx.setRemoveEmptySpaceBetweenColumns(true);
configXlsx.setRemoveEmptySpaceBetweenRows(true);
configXlsx.setCollapseRowSpan(true);
configXlsx.setWhitePageBackground(false);
exporterXlsx.setConfiguration(configXlsx);
exporterXlsx.exportReport();
输出 xls(左)、xlsx(右)
cell A1 in xlsx the style is not applied
解决方法
不要在 textField
上使用样式,因此将样式直接应用于 textField
。
在示例中,我们将 forecolor="#CC0000"
和 fontSize="14"
添加到 textField
并删除 样式
属性
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="200" height="20" forecolor="#CC0000" uuid="6d5644bf-480e-4ed2-831b-3ed043f38f70"/>
<textElement verticalAlignment="Middle" markup="html">
<font size="14"/>
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA["<b>TEST</b> TEXT"]]></textFieldExpression>
</textField>
关于jasper-reports - 为什么在使用标记时在 XLSX 中忽略样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38844417/