Java 生成/导出Excel,同时按内容生成二维码和条形码列

maven 增加依赖 google zxing

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.5.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.5.0</version>
</dependency>


代码如下:

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;

public class ExportQr {
    public static void main(String ags[]) {
        String excelName = "D://nowjava-excel.xls";
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        sheet.setColumnWidth(2, 4800);

        CellStyle headerStyle = workBook.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workBook.createFont();
        headerFont.setFontName("Arial");
        headerFont.setFontHeightInPoints((short) 10);
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerStyle.setFont(headerFont);

        HSSFRow titleRow = sheet.createRow(0);
        setCell(titleRow.createCell(0), headerStyle, "内容");
        setCell(titleRow.createCell(1), headerStyle, "二维码");
        setCell(titleRow.createCell(2), headerStyle, "条形码");
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        List<String> contentList = Arrays.asList("aaaa333aa", "abasdfasdfasdfasd", "333333333333333", "oaflazfoqnfaaldfoq333", "29asdkf9ksfjsf");
        try {
            for (int index = 0; index < contentList.size(); index++) {
                //内容
                String content = contentList.get(index);
                HSSFRow contentRowNext = sheet.createRow(index + 1);
                contentRowNext.createCell(0).setCellValue(content);

                //二维码
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
                hints.put(EncodeHintType.MARGIN, 0);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                MatrixToImageWriter.writeToStream(
                        new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints), "PNG",
                        outputStream);
                contentRowNext.setHeight((short) 1000);

                HSSFClientAnchor anchor = new HSSFClientAnchor(20, 10, 1003, 245, (short) 1, index + 1, (short) 1,
                        index + 1);
                patriarch.createPicture(anchor,
                        workBook.addPicture(outputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));

                //条形码
                outputStream = new ByteArrayOutputStream();
                hints = new HashMap<EncodeHintType, Object>();
                hints.put(EncodeHintType.MARGIN, 0);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                MatrixToImageWriter.writeToStream(
                        new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 300, 120, hints), "PNG",
                        outputStream);
                contentRowNext.setHeight((short) 1000);

                anchor = new HSSFClientAnchor(20, 10, 1003, 245, (short) 2, index + 1, (short) 2,
                        index + 1);
                patriarch.createPicture(anchor,
                        workBook.addPicture(outputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
            }

            FileOutputStream outputStream = new FileOutputStream(excelName);
            workBook.write(outputStream);
            outputStream.close();
            System.out.println("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void setCell(HSSFCell hSSFCell, CellStyle style, String content) {
        hSSFCell.setCellStyle(style);
        hSSFCell.setCellValue(content);
        return;
    }
}


生成效果如下:

展开阅读全文

本文系作者在时代Java发表,未经许可,不得转载。

如有侵权,请联系nowjava@qq.com删除。

编辑于

关注时代Java

关注时代Java