POI导出EXCEL附带二维码以及条形码

本文介绍如何利用Apache POI库在Java中创建Excel文件,并且在Excel中添加二维码和条形码。详细步骤包括添加相关依赖、实现代码以及定义单元格样式。
摘要由CSDN通过智能技术生成

效果图
在这里插入图片描述
在这里插入图片描述

实现代码如下:

添加依赖


        <!--条形码/二维码-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

实现代码


import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class CaseMarkServiceImpl implements CaseMarkService {
   
    @Autowired
    private CaseMarkRepository repository;

@Override
    public HttpEntity<InputStreamSource> exportCaseMark(List<String> list) throws IOException, WriterException {
   
        // 查询数据库获取生成caseMark数据
        List<CaseMarkOutDto> resultList = repository.findVehicleInfo(list);
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        if (!CollectionUtils.isEmpty(resultList)) {
   
            for (CaseMarkOutDto dto : resultList) {
   
                SXSSFSheet sheet = workbook.createSheet(dto.getVin());
                // 设置列宽
                sheet.setColumnWidth(0, 20 * 50);
                sheet.setColumnWidth(1, 20 * 270);
                sheet.setColumnWidth(2, 20 * 100);
                sheet.setColumnWidth(3, 20 * 230);
                sheet.setColumnWidth(4, 20 * 200);
                sheet.setColumnWidth(5, 20 * 70);
                sheet.setColumnWidth(6, 20 * 150);
                int index = 0;
                // 首行添加个空白行
                SXSSFRow row = sheet.createRow(index);
                row.setHeight((short) 700);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                }
                CellRangeAddress cellAddresses = new CellRangeAddress(index, index, 0, 6);
                sheet.addMergedRegion(cellAddresses);
                index++;
                // 获取pcNo
                String pcNoStr = dto.getPcNo();
                String pc1 = pcNoStr.substring(0, 3);
                String pc2 = pcNoStr.substring(3);
                String pcNo = pc1 + "-" + pc2;
                String pcNos = "32F-" + pcNo;
                // 第2行
                SXSSFRow row2 = sheet.createRow(index);
                row2.setHeight((short) 1000);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row2.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle5(workbook));
                    if (i == 1) {
   
                        cell.setCellValue(pcNos);
                        cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                    } else if (i == 4) {
   
                        cell.setCellValue(dto.getVesselCode());
                        cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                    }
                }
                CellRangeAddress cal1 = new CellRangeAddress(index, index, 1, 3);
                sheet.addMergedRegion(cal1);
                CellRangeAddress cal2 = new CellRangeAddress(index, index, 4, 6);
                sheet.addMergedRegion(cal2);
                index++;
                // 第3行
                SXSSFRow row3 = sheet.createRow(index);
                row3.setHeight((short) 700);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row3.createCell(i);
                    if (i == 1) {
   
                        cell.setCellValue("HDM");
                        cell.setCellStyle(ExcelStyleUtils.getStyle22(workbook));
                    } else if (i == 3) {
   
                        cell.setCellValue("* " + dto.getVin() + " *");
                        cell.setCellStyle(ExcelStyleUtils.getStyle23(workbook));
                    } else {
   
                        cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                    }
                }
                CellRangeAddress cal3 = new CellRangeAddress(index, index, 3, 5);
                sheet.addMergedRegion(cal3);
                index++;
                // 第4行  300
                SXSSFRow row4 = sheet.createRow(index);
                row4.setHeight((short) 300);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row4.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                }
                CellRangeAddress cal4 = new CellRangeAddress(index, index, 0, 5);
                sheet.addMergedRegion(cal4);
                index++;
                // 第5行
                SXSSFRow row5 = sheet.createRow(index);
                row5.setHeight((short) 600);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row5.createCell(i);
                    if (i == 1) {
   
                        cell.setCellValue(pcNo);
                        cell.setCellStyle(ExcelStyleUtils.getStyle22(workbook));
                    }
                }
                index++;
                // 第6行
                SXSSFRow row6 = sheet.createRow(index);
                row6.setHeight((short) 300);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row6.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                }
                index++;
                // 第7行
                SXSSFRow row7 = sheet.createRow(index);
                row7.setHeight((short) 400);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row7.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle21(workbook));
                }
                CellRangeAddress cal7 = new CellRangeAddress(index - 2, index, 1, 2);
                sheet.addMergedRegion(cal7);
                index++;
                // 第8行
                SXSSFRow row8 = sheet.createRow(index);
                row8.setHeight((short) 700);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row8.createCell(i);
                    cell.setCellStyle(ExcelStyleUtils.getStyle23(workbook));
                    if (i == 1) {
   
                        cell.setCellValue(dto.getDistOfficialName());
                    }
                }
                CellRangeAddress cal8 = new CellRangeAddress(index, index, 1, 2);
                sheet.addMergedRegion(cal8);
                index++;

                String pc_case = dto.getPcNo() + this.appendCaseNo(dto);
                // 第9行
                SXSSFRow row9 = sheet.createRow(index);
                row9.setHeight((short) 500);
                for (int i = 0; i < 7; i++) {
   
                    SXSSFCell cell = row9.createCell(i);
                    cell
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该工具为个人整理修改的常用工具类,maven结构,Java语言编写详细依赖间pom文件,如有冲突自行修改, 包括条形码操作工具、二维码操作、图片验证码工具、动态密码工具、雪花算法id工具、签名工具、poi导出、json转换、文件操作、分页、日期处理、jdbc、大数运算及格式化、gzip压缩、http 结构说明如下: datasourceconfig:多数据源使用配置包 对数据源配置.txt:配置说明文件 function:功能代码及工具类 delayqueue:java自带延迟对了使用包 DelayMsg:延迟消息实体 DelayMsgHandler:延迟消息处理工具 paramvalid:空参检查包 CommonUtil:空参检查工具类 ParamNotNull:参数注解 RegistParam:参数实体 RequestMsg:公共参数实体 pdf:pdf导出工具包,包含4个导出工具类,具体使用根据导出结果确定 websocket:服务端websockt配置包,比较简单,具体使用需自行扩充 ActiveMqUtil:ActiveMq操作工具类 BarCodeUtil:条形码操作工具类,包括生成和读取 CorsConfig:服务端防止跨域请求公共设置 DateUtil:时间处理工具类 DBConnection:jdbc工具类 FileOperater:文件处理工具类 包括 读取文本文件,写出文本文件, 大文件切分,文件下载,文件或文件夹比较,文件或文件夹遍历筛选 ...... HttpClientCard:http工具类 HttpUtil:http工具类 Identification:id生成工具类,包括 UUID luhn算法id(适用于有序的流水号码) ImgUtil:图片相似度计算工具 ItvJsonUtil:基于fastjson的json工具类 JsonUtility:基于jackjson的接送工具类 MailUtil:邮件工具类,包括发送纯文本邮件,带(1个/多个)附件的邮件, 群发邮件 NumberUtil:数字操作工具 包括精准数字运算,数据格式化 PageUtil:分页工具类, POIUtil:poi工具类,excel导出 QrCodeUtil:二维码操作工具, 包括生成和读取 ShellUtil:shell命令操作工具,包括linux登陆,命令执行...... 较为简单,具体需要自行扩充 SignUtil:签名工具,包括MD5 位运算 AES BASE64 SnowflakeIdWorker:ID生成工具 雪花算法 SocketUtil:socket测试工具 TotpAuthUtil:动态密码工具 包括生成和校验 VerifyCodeUtil:图片验证码生成工具 ZipUtils:gzip压缩工具
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值