public ResponseEntity<byte[]> chartExport(HttpServletRequest request, String picinfo, HttpServletResponse response) {
//類型
String type = request.getParameter("type");
try {
//decodeBase64();
List<MockTestNetTrend> dataList = JsonUtils.json2List(request.getParameter("mockTestNetTrendList"), MockTestNetTrend.class);
List<String> cellList = JsonUtils.json2List(request.getParameter("cellList"), String.class);
// 生成圖片
byte[] picInfoByte = base64TObyte(request, picinfo);
/* String picPath = "C:\\Users\\Administrator\\Desktop\\pic" + ".png";
File picFile = new File(picPath);//圖片文件
OutputStream picStream = new FileOutputStream(picFile);//圖片輸出流
picStream.write(picInfoByte);
picStream.flush();
picStream.close();*/
String outFileName = FileNameUtil.createFileName("xls");
String exportFileName = type + ".xls";
//導出路徑
String exportFilePath = CacheUtil.getSysParam(GlobalConstant.COM_PROP_ACCESS_EXPORTPATH);
// 模板輸入流
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int index = 1;
//創建表頭
createHeader(wb, sheet,cellList);
//寫入數據
if (!StringUtil.isEmptyList(dataList)) {
writeData(dataList, wb, sheet, index, type);
}
Drawing patri = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 0, dataList.size() + 2, (short) 5, 25);
//圖通過流的形式插入到excel模板中
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // 將圖片寫入流中
ByteArrayInputStream in = new ByteArrayInputStream(picInfoByte); //將picInfoByte作為輸入流;
BufferedImage bufferImg = ImageIO.read(in); //將in作為輸入流,讀取圖片存入image中,而這里in可以為ByteArrayInputStream();
ImageIO.write(bufferImg, "png", outStream); // 利用HSSFPatriarch將圖片寫入EXCEL
patri.createPicture(anchor, wb.addPicture(
outStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
File outFile = new File(exportFilePath + File.separator + outFileName);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
wb.write(new FileOutputStream(outFile));
outStream.flush();
outStream.close();
/* if (picFile.exists()){
picFile.delete();
}*/
return ExportUtil.export(exportFileName, outFile, true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}