在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();
}
}