Excel导出
来源:互联网 发布:自学编程需要多久 编辑:程序博客网 时间:2024/06/08 17:33
/** * 根据批次号导出溯源码excel * 2003 * * @param pbCode * @return */ @RequestMapping("exportTrQrcode") public synchronized void exportTrQrcode(String pbCode, HttpServletResponse response) throws IOException { //参数初始化 startRow = -1; startCol = 0; patriarch = null; totalSheet = 0; sheet = null; ResultBase resultBase = null; BufferedImage bufferImg = null; String outFileName = null; HSSFWorkbook wb = null; try { if (StringUtils.isEmpty(pbCode)) { resultBase = new ResultBase("1014"); } else if (batchService.getProBatchByPbCode(pbCode) == null) { resultBase = new ResultBase("1015"); } else { List<TbTraceQrcode> qrcodeList = qrcodeService.getListByPbCode(pbCode); File model = new File(FilePathConsts.rootPath + "file/excel/export_trace_qrcode.xls"); FileInputStream fis = new FileInputStream(model); POIFSFileSystem fs = new POIFSFileSystem(fis); wb = new HSSFWorkbook(fs); //HSSFSheet sheet = wb.createSheet("qrcode picture"); sheet = wb.getSheetAt(0); //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点) patriarch = sheet.createDrawingPatriarch(); int width = sheet.getColumnWidth(0); int height = sheet.getRow(0).getHeight(); totalSheet = 1; //溯源码图片生成 顺序为:溯源码-其对应的活动码们-下一个溯源码... for (TbTraceQrcode qrcode : qrcodeList) { ByteArrayOutputStream byteArrayOut = QrCodeUtils.qrCode(FilePathConsts.qrcode_prefix + qrcode.getTqQrcode()); insertPicture(width, height, wb, byteArrayOut); //活动码 List<TbDrawQrcode> drawQrcodeList = drawQrcodeService.getDrQrcodeListByTqQrcode(qrcode.getTqQrcode()); //活动码图片生成 for (TbDrawQrcode tbDrawQrcode : drawQrcodeList) { ByteArrayOutputStream byteArrayOut2 = QrCodeUtils.qrCode(FilePathConsts.qrcode_prefix + tbDrawQrcode.getDqId()); insertPicture(width, height, wb, byteArrayOut2); } } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } finally { response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + UUIDUtils.getUniquekey() + ".xls"); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); } } /** * EXCEL插入图片 * * @param width * @param height * @param wb * @param byteArrayOut */ private void insertPicture(int width, int height, HSSFWorkbook wb, ByteArrayOutputStream byteArrayOut) { try { HSSFClientAnchor anchor; //anchor主要用于设置图片的属性 if (startCol != 0 && startCol < 5) { anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) startCol, startRow, (short) startCol, startRow); sheet.setColumnWidth(startCol, width); //sheet.getRow(startRow).setHeight((short)height); startCol++; } else { startCol = 0; startRow++; anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) startCol, startRow, (short) startCol, startRow); sheet.setColumnWidth(startCol, width); if (sheet.getRow(startRow) == null) { sheet.createRow(startRow); } sheet.getRow(startRow).setHeight((short) height); startCol++; } anchor.setAnchorType(3); //插入图片 patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); //每页五行五列 if (startRow == 5 && startCol == 5) { sheet = wb.createSheet("Sheet" + (++totalSheet)); patriarch = sheet.createDrawingPatriarch(); startCol = 0; startRow = -1; } } catch (Exception e) { throw e; } }
阅读全文
1 0
- 导出EXCEL
- Excel导出
- 导出Excel
- 导出EXCEL
- 导出Excel
- EXCEL导出
- 导出Excel
- 导出excel
- 导出excel
- EXCEL导出
- 导出excel
- 导出Excel
- Excel导出。
- 导出excel
- Excel 导出
- 导出excel
- 导出excel
- 导出excel
- hdu5984 Pocky(猜数学期望公式)
- java集合总结(二)-各类关系与注意事项
- Coursera 机器学习(by Andrew Ng)课程学习笔记(四)——神经网络(一)
- 初识阿里云2017年10月10日090449
- FullyGridLayoutManager
- Excel导出
- 安卓使用腾讯Bugly实现应用升级功能
- mysql语句
- 1.2.Qt添加程序图标
- Coursera 机器学习(by Andrew Ng)课程学习笔记(五)——神经网络(二)
- 解决ios下的微信打开的页面背景音乐无法自动播放
- bootstrap-datepicker.js
- 浅析Android的setOnTouchListener的返回值是false和true的区别
- 支付宝微信免签约 免手续费 支付接口API