首页 文章

使用标记时,为什么在XLSX中忽略了样式?

提问于
浏览
2

我对文本的一部分使用粗体样式,所以它看起来像:

"<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 Markup property - styled

它适用于PDF和HTML . 但是,我在使用 XLSX 时遇到问题 .

不幸的是,即使直接将字体大小设置为14(我之前尝试从样式设置它)我得到整个标签的字体 11 callibri (这是默认字体),它使用标签 <style isBold='true'> .

我尝试了同样的 <b> text </b> 和markup = HTML - 结果没有改变 .

Conclusion: Any styled text in XLSX is insensitive for fonts (sets it to default), how can this be resolved?

EDIT:

我发现问题是我之前申请的样式,但是问题仍然存在于excel中 . <style> 标记只是将其覆盖为默认字体和字体大小 .

1 回答

  • 1

    我可以在导出到 xlsx 时确认相同的错误,样式被忽略,它似乎与为 XSSFSheet 中的单元格创建 RichTextString 有关 . (不正确/没有字体设置为 RichTextString ?)

    编辑:我创建了一个bug问题,标记为下一个版本已解决(当前版本为v6.3.0)

    重现bug的简单示例

    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>
    

    java code to export to xls and xlsx

    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();
    

    Output xls (left), xlsx (right)

    result

    xlsx中的单元格A1未应用样式

    解决

    textField 上执行 not use style ,因此将样式直接应用于 textField .

    在示例中,我们将 forecolor="#CC0000"fontSize="14" 添加到 textField 并删除 style 属性

    <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>
    

相关问题