POI模板导出EXCEL并且插入图片

/**
* Asponse Cells
* 1:准备模板
* 2:准备数据  一种Map 一种实体类\
* 3: 填充数据
* 4: <a href="xxxxxx.do?method=downloadMulu">导出</a>
*/
@RequestMapping(params="method=downloadMulu", method=RequestMethod.GET)
public ResponseEntity<byte[]> downloadMulu(@Json Map<String,Object> searchParams,HttpServletRequest request) throws JsonParseException, JsonMappingException, IOException {
//设置Http头
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

List<ComponentstockExtend> dataList = new ArrayList<ComponentstockExtend>();

dataList = componentstockMapper.selectPagination(new PageBounds(), searchParams);


//报表的名字
String fileName = "查询表.xls";

//模板的路径
String tplPath = "/WEB-INF/designer/componentqrcodeQuery.xls";

//设置http头,设置文件下载名称
responseHeaders.add("content-disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO8859-1" ));

//模板设计器
WorkbookDesigner designer = new WorkbookDesigner();
String path = request.getServletContext().getRealPath(tplPath);
//内存流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
        FileOutputStream fileOut = null;     
        BufferedImage bufferImg = null; 
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        
        
try {
//创建工作簿 等于excel 
Workbook wb = new Workbook(path);
designer.setWorkbook(wb);


//fileOut.close();
byteArrayOut.close();
//填充数据
designer.setDataSource("Data", dataList);

designer.setDataSource("title", "查询表");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date=formatter.format(new Date());
designer.setDataSource("date",date);


//绑定数据
designer.process();

wb.getWorksheets().get(0);
//输出到内存流
wb.save(outputStream, wb.getFileFormat());
  org.apache.poi.ss.usermodel.Workbook poiWorkbook = new HSSFWorkbook(new ByteArrayInputStream(outputStream.toByteArray()));
int sheetcount = poiWorkbook.getNumberOfSheets();
poiWorkbook.removeSheetAt(sheetcount-1);
poiWorkbook.setActiveSheet(0);
Drawing patriarch = poiWorkbook.getSheetAt(0).createDrawingPatriarch();
int row=3;
for(ComponentstockExtend c:dataList){
int width = 100;   
       int height = 100;  
       int col=12;
       String format = "jpg";   
       Hashtable hints= new Hashtable();   
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
       BitMatrix bitMatrix = new MultiFormatWriter().encode(c.getComponentqrcode(), BarcodeFormat.QR_CODE, width, height,hints);   
       File outputFile = new File("D:\\MyNewCode.jpg"); 
       MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
//POI API 目的删掉最后一个sheet
bufferImg = ImageIO.read(new File("D:\\MyNewCode.jpg"));
        ImageIO.write(bufferImg,"jpg",byteArrayOut);     
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,0,0,(short) col, row, (short)++col, ++row);     
       anchor.setAnchorType(3);                
           patriarch.createPicture(anchor, poiWorkbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
}
poiWorkbook.write(outputStream2);
} catch (Exception e) {
throw new RuntimeException(e);
}
//返回一个下载的流
return new ResponseEntity<byte[]>(outputStream2.toByteArray(), responseHeaders, HttpStatus.OK);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值