/**
* 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);
}
* 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);
}