barcode4j+Apache POI,批量导出条形码图片到excel

客户在业务过程中需要打印条形码,以便追踪每个单品。为了符合客户的条码打印机格式,采用了barcode4j+Apache POI的模式。

一开始用bufferImg读图再转换成byteArray的方法写,发现无法批量插入,十几张条形码只能显示为一张。后面采用了不在本地存储图片,直接用byteArray写入,成功。下面是代码。

public int getExcel(HttpServletRequest request) throws IOException, IllegalArgumentException, IllegalAccessException {
        List<BarCode> list = getBarCodeByStatus(0);
        if (list != null) {
            FileOutputStream fileOut = null;
            HSSFClientAnchor anchor = null;
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet1 = wb.createSheet("条码打印");
            HSSFFont hssfFont = wb.createFont();
            hssfFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
            hssfFont.setFontHeightInPoints((short) 12);
            hssfFont.setFontName("宋体");
            sheet1.setDefaultRowHeightInPoints((short) 15);
            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
            //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
            fileOut = new FileOutputStream(AppConstants.getUploadedPath(request) + "封装袋编码表.xls");
            try {
                int row1 = 8;
                int row2 = 12;
                for (BarCode b : list) {
                    //anchor主要用于设置图片的属性
                    anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, row1, (short) 3, row2);
                    anchor.setAnchorType(3);
                    //插入图片
                    patriarch.createPicture(anchor, wb.addPicture(BarCodeUtil.generateByte(b.getCode()), HSSFWorkbook.PICTURE_TYPE_PNG));
//符合客户打印机格式,间隔13行
                    row1 += 13;
                    row2 += 13;
                    b.setPrtStatus(1);
                }
                // 写入excel文件
                wb.write(fileOut);
                System.out.println("----Excel文件已生成------");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fileOut != null) {
                    try {
                        fileOut.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return 0;
        } else {
            return 1;
        }
    }

需要导入一个

import org.apache.commons.codec.digest.DigestUtils;

不然会报class not found

barcodeUtil代码

package com.xxt.utils;

import org.apache.fop.util.UnitConv;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

import java.awt.image.BufferedImage;
import java.io.*;

public class BarCodeUtil {
    public static File generateFile(String msg, String path) {
        File file = new File(path);
        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return file;
    }
    /**
     * 生成字节
     *
     * @param msg
     * @return
     */
    public static byte[] generateByte(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }

    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */
    public static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }

        Code39Bean bean = new Code39Bean();

        // 精细度
        final int dpi = 150;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);

        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);

        String format = "image/png";
        try {

            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);

            // 生成条形码
            bean.generateBarcode(canvas, msg);

            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //导出

}

猜你喜欢

转载自blog.csdn.net/qq_34137382/article/details/81168600