public ResponseEntity chartExport(HttpServletRequest request, String picinfo, HttpServletResponse response) {
//类型
String type = request.getParameter("type");
try {
//decodeBase64();
List dataList = JsonUtils.json2List(request.getParameter("mockTestNetTrendList"), MockTestNetTrend.class);
List 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;
}