HelloWorld2.0
2021-07-15 14:45
采纳率: 100%
浏览 303
已采纳

使用ZXing生成二维码,将二维码流形式输入到execl(poi)中 出现覆盖叠加?

在execl单元格中一直覆盖叠加 , 一个execl中会带一个二维码,如果下载两份,第二份就会在同一个位置出现两个二维码,新的二维码会覆盖第一份的二维码。

    String content ="123cs";

    //获取一个二维码图片
    BitMatrix bitMatrix =null;

    bitMatrix = QRCodeUtils.createCode(content);

    System.out.println(bitMatrix);


    ByteArrayOutputStream pngOutputStream =null;
    pngOutputStream = new ByteArrayOutputStream();

    //以流的形式输出
    MatrixToImageWriter.writeToStream(bitMatrix , "png" , pngOutputStream);

    /**
     * 该构造函数有8个参数
     * 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离
     * 后四个参数,前连个表示图片左上角所在的cellNum和 rowNum,个参数对应的表示图片右下角所在的cellNum和 rowNum,
     * excel中的cellNum和rowNum的index都是从0开始的
     *
     */

    //图片一导出到单元格B5中
    XSSFClientAnchor anchor = null;

    anchor = new XSSFClientAnchor(10000, 10000, 0, 0,
            (short) 8, 18, (short) 9, 20);


    // 插入图片
    st1.createDrawingPatriarch().createPicture(anchor, wb.addPicture(pngOutputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));

    try {

        pngOutputStream.reset();
        pngOutputStream.flush();
        pngOutputStream.close();
        wb.write(out);
        wb.close();


    } catch (IOException e) {
        e.printStackTrace();
    }

public class QRCodeUtils {

/**
 *  生成二维码
 * @param content 二维码的内容
 * @return BitMatrix对象
 * */
public static BitMatrix createCode(String content) throws IOException {
    //二维码的宽高
    int width = 200;
    int height = 200;

    //其他参数,如字符集编码
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    //容错级别为H
    hints.put(EncodeHintType.ERROR_CORRECTION , ErrorCorrectionLevel.H);
    //白边的宽度,可取0~4
    hints.put(EncodeHintType.MARGIN , 0);

    BitMatrix bitMatrix = null;
    try {
        //生成矩阵,因为我的业务场景传来的是编码之后的URL,所以先解码
        bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);

        //bitMatrix = deleteWhite(bitMatrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bitMatrix;
}

/**
 *  删除生成的二维码周围的白边,根据审美决定是否删除
 * @param matrix BitMatrix对象
 * @return BitMatrix对象
 * */
private static BitMatrix deleteWhite(BitMatrix matrix) {
    int[] rec = matrix.getEnclosingRectangle();
    int resWidth = rec[2] + 1;
    int resHeight = rec[3] + 1;

    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
    resMatrix.clear();
    for (int i = 0; i < resWidth; i++) {
        for (int j = 0; j < resHeight; j++) {
            if (matrix.get(i + rec[0], j + rec[1]))
                resMatrix.set(i, j);
        }
    }
    return resMatrix;
}

}

// 收前台传入的ID 重复调用 赋值下载代码生成Execl表格    

@RequestMapping(value = "/slId.do", name = "获取ID 重复调用赋值下载功能")
public void slId(String ids) throws Exception {


    String[] id = ids.split(",");


    //导出zip
    ByteOutputStream outXlsx = new ByteOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(outXlsx);

    Calendar now = Calendar.getInstance();

    for (String s : id) {

        // 拿到明细单ID 根据ID 查询出数据

        ltfmMxd companyMxdById = mxdService.findCompanyMxdById(Integer.parseInt(s));


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        csprintExcel(companyMxdById, baos);
        // 将baos 变成图片
        compressFileToZipStream(zipOutputStream, baos, companyMxdById.getReportNumHead() + "-" + companyMxdById.getReportNumMain() + "-" + now.get(Calendar.YEAR) + "-" + companyMxdById.getReportNum() + ".xlsx");
        baos.flush();
        baos.reset();
        baos.close();


    }

    zipOutputStream.flush();
    zipOutputStream.close();

    String fileNameTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String filename = fileNameTime + ".zip";
    // the response variable is just a standard HttpServletResponse
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    response.setContentType("application/zip");

    try {
        response.getOutputStream().write(outXlsx.toByteArray());
        response.flushBuffer();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        outXlsx.flush();
        outXlsx.reset();
        outXlsx.close();

    }

}


/**
 * 压缩单个excel文件的输出流 到zip输出流,注意zipOutputStream未关闭,需要交由调用者关闭之
 *
 * @param zipOutputStream   zip文件的输出流
 * @param excelOutputStream excel文件的输出流
 * @param excelFilename     文件名可以带目录,例如 TestDir/test1.xlsx
 */
public static void compressFileToZipStream(ZipOutputStream zipOutputStream,
                                           ByteArrayOutputStream excelOutputStream, String excelFilename) {
    byte[] buf = new byte[1024];
    try {
        // Compress the files
        byte[] content = excelOutputStream.toByteArray();
        ByteArrayInputStream is = new ByteArrayInputStream(content);
        BufferedInputStream bis = new BufferedInputStream(is);
        // Add ZIP entry to output stream.
        zipOutputStream.putNextEntry(new ZipEntry(excelFilename));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = bis.read(buf)) > 0) {
            zipOutputStream.write(buf, 0, len);
        }
        // Complete the entry
        excelOutputStream.close();//关闭excel输出流
        zipOutputStream.closeEntry();
        bis.close();
        is.close();
        // Complete the ZIP file
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • 曦陽惜夏 2021-07-20 10:34
    已采纳

    HttpServletResponse response 这个对象使用局部的 public void slId(String ids,HttpServletResponse response) throws Exception,改成这样试试

    已采纳该答案
    打赏 评论
  • CSDN专家-Time 2021-07-21 10:32

    先不插二维码,先从插图片开始做。
    问题出在createDrawingPatriarch().createPicture
    每次来每次插入,当然会插入N次图片.
    你需要对外部函数调用进行分析.将代码逻辑写成,即使下载N次,也只调用1次。

    打赏 评论
  • one.lin 2021-07-15 17:56

    anchor = new XSSFClientAnchor(10000, 10000, 0, 0,
    (short) 8, 18, (short) 9, 20);
    这里用死值肯定会一张盖一张啊

    打赏 评论
  • 雨夹雪 2021-07-16 08:07

    你在新加二维码前,要把之前的删掉

    Workbook workbook = new Workbook();
    workbook.LoadFromFile("你的Excel");
    
    Worksheet sheet = workbook.Worksheets[0];
    sheet.Pictures[0].Remove();
    
    打赏 评论
  • 王大师王文峰 2021-07-19 16:25

    ZXing生成二维码并导出到excel表
    如果是这个的话,可以看看这篇博客
    https://blog.csdn.net/qq_25333005/article/details/79296061
    如果有帮助的话,望采纳,谢谢!

    打赏 评论
  • HelloWorld2.0 2021-07-21 09:24

    有大哥能解决嘛

    打赏 评论

相关推荐 更多相似问题