Java:如何在Excel工作表中显示条形码?

标签 java jasper-reports apache-poi export-to-excel

我尝试使用 Apache POI 等 API 在 Excel 工作表中显示条形码,但没有找到任何解决方案。

我还尝试使用 JasperReports(因为我们可以在其中生成条形码),然后使用 JRXlsExporter 将编译后的报告转换为 Excel。

下面是我的用于 Excel 导出的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="customer_export" language="groovy" pageWidth="755" pageHeight="842" columnWidth="715" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="siteId" class="java.lang.String">
    <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<parameter name="companyId" class="java.lang.String">
    <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[SELECT c.NAME ,c.EXTERNAL_CODE ,a.ADDRESS,CASE  WHEN c.LOCAL_MAIL IS NOT NULL THEN c.LOCAL_MAIL when C.MAIL IS NOT NULL then c.MAIL when c.MAIL IS Null then NULL when c.LOCAL_MAIL is NULL then NULL ELSE NULL END as MAIL, ru.BARCODE, c.PHONE FROM CUSTOMER c, SITE csite, RACK_USER ru, ADDRESS a WHERE c.COMPANY_SITE_ID = csite.ID_SITE AND c.RACK_USER_ID = ru.ID_RACK_USER AND ru.ID_RACK_USER = a.RACK_USER_ID AND csite.ID_SITE=$P{companyId} AND c.SITE_ID=$P{siteId} AND a.MAIN_FOR_COMPANY_SITE=1 AND a.COMPANY_SITE_ID =$P{companyId} ORDER BY c.NAME ASC]]>
</queryString>
<field name="NAME" class="java.lang.String"/>
<field name="EXTERNAL_CODE" class="java.lang.String"/>
<field name="ADDRESS" class="java.lang.String"/>
<field name="MAIL" class="java.lang.String"/>
<field name="BARCODE" class="java.lang.String"/>
<field name="PHONE" class="java.lang.String"/>
<title>
    <band height="20">
        <staticText>
            <reportElement mode="Opaque" x="0" y="0" width="119" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Customer Name]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="119" y="0" width="112" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Customer Code]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="231" y="0" width="157" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Address]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="388" y="0" width="121" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Email]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="509" y="0" width="100" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Barcode]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="609" y="0" width="106" height="20" backcolor="#CCCCCC"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[Phone Number]]></text>
        </staticText>
    </band>
</title>
<detail>
    <band height="24" splitType="Stretch">
        <textField>
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="119" height="24"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="119" y="0" width="112" height="24"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{EXTERNAL_CODE}]]></textFieldExpression>
        </textField>
        <textField isStretchWithOverflow="true">
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="231" y="0" width="157" height="24"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{ADDRESS}]]></textFieldExpression>
        </textField>
        <textField isStretchWithOverflow="true">
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="388" y="0" width="121" height="24"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{MAIL}]]></textFieldExpression>
        </textField>
        <textField isBlankWhenNull="true">
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="609" y="0" width="106" height="24"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{PHONE}]]></textFieldExpression>
        </textField>
        <componentElement>
            <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="509" y="0" width="100" height="24"/>
            <jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="Code39" drawText="true" checksumRequired="false" evaluationTime="Report">
                <jr:codeExpression><![CDATA["1234"]]></jr:codeExpression>
            </jr:barbecue>
        </componentElement>
    </band>
</detail>

Java导出Excel代码:

JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jprint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, reportos);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET, 50000);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_FILE, filename);
exporterXLS.exportReport();

但是在条形码列中,当导出 Excel 时,我得到

我需要在 JasperReports 或 Java 或其他任何东西中应用任何属性吗?

或者还有其他 API,或者我们可以使用 Apache POI 显示条形码吗?

最佳答案

使用barcode4jbarbecue您可以在java中将条形码生成为图像,然后将其传递给jasper-reports

barcode4j 示例

Java代码

public class MyBarcodeGenerator {
  public static Image getGS1_128(String code) {
    if (code==null){
        return null;
    }
    EAN128Bean c = new EAN128Bean();
    c.setChecksumMode(ChecksumMode.CP_AUTO);
    c.setOmitBrackets(false);
    c.setCodeset(Code128Constants.CODESET_C);
    c.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
    c.doQuietZone(true);
    c.setQuietZone(5);
    c.setFontSize(2d);
    
    int dpi = 200;
    boolean antiAlias = false;
    int orientation = 0;
    BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);
    c.generateBarcode(canvas, code);
    return canvas.getBufferedImage();
  }
}

jrxml,显示图像

<image scaleImage="Clip" hAlign="Center" vAlign="Middle" isUsingCache="false" onErrorType="Blank">
    <reportElement positionType="Float" x="112" y="67" width="315" height="95" uuid="b90b1ec8-1483-4677-8db4-4556ecdad6b3">
        <printWhenExpression><![CDATA[new Boolean($F{GS1_128}!=null)]]></printWhenExpression>
    </reportElement>
    <imageExpression><![CDATA[my.package.MyBarcodeGenerator.getGS1_128($F{GS1_128})]]></imageExpression>
</image>

For correct image size change the dpi settings or use the batik libraries to generate svg images, it's a bit more complex but with much better resolution see barcode4j svg dom

关于Java:如何在Excel工作表中显示条形码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37567950/

相关文章:

java - 如何从 Excel 文件中逐行读取值并将其放入 Map 中?

java - Android:Java、C 还是 C++?

jasper-reports - 计算字段为特定值的行数

jasper-reports - 使用 jasperreports 时 apache Commons-javaflow 的安全性如何

java - 如何使用 apache poi 在同一个文件中写入多个工作表

java - 如何使用 apache POI 从 Excel 中删除图表?

java - 字段需要类型为 ... 的 bean,但无法找到

java - 凋零 vs 生成器 Lombok 库

java - POST 请求无法使用 ReSTLet 处理 JSON 输入

jasper-reports - 每次使用时如何增加变量?