Java便捷生成二维码并使用Excel


1、思路

第一步生成图片字节数组输出流
第二步字节数组输出流存入excel

2、详细过程

1.引入依赖

使用的是easyexcel和hutool工具便捷快速开发

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.15</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--阿里巴巴easyexcel工具-->

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel-core</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>

2.导出数据

导出类信息@ContentRowHeight(100) @ColumnWidth(100/6)这两个注解是条件excel行列大小,可以自行调整

@Getter
@Setter
@EqualsAndHashCode
@ContentRowHeight(100)
@ColumnWidth(100/6)
public class ImageDemoData{


    private byte[] byteArray;


    private String shortDesc;
    /**
     * 根据文件导出 并设置导出的位置。
     */
    @ExcelIgnore
    private WriteCellData<Void> writeCellDataFile;
}

EasyExcel会根据这个实体类来生成表格,可以去官网查看注解注释掉其中的某个属性,每一个属性代表每列的信息。

@Test
public void imageWrite() throws Exception {
    String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
       List<ImageDemoData> list = ListUtils.newArrayList();
        ImageDemoData imageDemoData = new ImageDemoData();
        list.add(imageDemoData);
		// 创建字节数组输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		// 调用 QrCodeUtil.generate() 生成二维码,并将结果写入输出流
        QrCodeUtil.generate("helloworld", 300, 300, ImgUtil.IMAGE_TYPE_PNG, outputStream);
		// 从输出流中获取生成的二维码图像数据
        byte[] qrCodeData = outputStream.toByteArray();
        imageDemoData.setByteArray(qrCodeData);
        imageDemoData.setShortDesc("helloworld");
		// 关闭输出流
        outputStream.close();
        // 写入数据
        EasyExcel.write(fileName, ImageDemoData.class).sheet().doWrite(list);
    }

利用 hutools 工具简化开发,生成字符串对应的字节数组输出流,传入easyexcel完成二维码的生成。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是Java生成二维码并下载的代码示例: ```java // 导入相关包 import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Controller @RequestMapping("/qrcode") public class QRCodeController { @GetMapping("/download") @ResponseBody public void downloadQRCode(@RequestParam("content") String content, HttpServletResponse response) throws IOException, WriterException { // 设置二维码参数 int width = 300; // 二维码宽度 int height = 300; // 二维码高度 String format = "png"; // 二维码图片格式 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码格式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错等级 hints.put(EncodeHintType.MARGIN, 1); // 边距 // 生成二维码 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); // 将二维码图片写入文件 File file = new File("qrcode.png"); ImageIO.write(image, format, file); // 设置响应头,使浏览器能够下载文件 response.setHeader("Content-Disposition", "attachment;filename=qrcode.png"); response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); // 将文件写入响应流 FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } in.close(); out.close(); } } ``` 以上代码使用Spring Boot框架,通过调用`/qrcode/download`接口,传入二维码内容`content`,即可生成二维码并下载到本地。其中,二维码参数可以根据需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夸父号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值