POI导出实现以及导出所有图片都是第一次插入图片解决办法
导出代码
@ResponseBody
@ApiOperation(value = "导出", httpMethod = "GET")
public Result export(HttpServletResponse response, @RequestParam("list") List<EquipmentContrastDTO> list) {
List<String> nameList; // 这里是我业务需要动态表头
if (CollectionUtils.isEmpty(list)) { //list 是导出数据
return Result.success("导出成功");
}
//导出的标题
List<String> title = new ArrayList<>();
title.add("名称");
title.add("图片");
if (CollectionUtils.isNotEmpty(list.get(0).getFdEquipmentParameterList())) {
nameList = list.get(0).getFdEquipmentParameterList().stream().map(name -> name.getParameterName()).collect(Collectors.toList());
title.addAll(nameList);
}
//excel名称
String fileName = "导出.xlsx";
//sheet名称
String sheetName = "sheet名称";
//调用导出方法
HSSFWorkbook wb = getHSSFWorkbook(sheetName, title, list, null);
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return Result.success("导出成功");
}
导出方法
public HSSFWorkbook getHSSFWorkbook(String sheetName, List<String> title, List<EquipmentContrastDTO> list, HSSFWorkbook wb) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
if (wb == null) {
wb = new HSSFWorkbook();
}
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 650);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFCell cell = null;
for (int i = 0; i < title.size(); i++) {
sheet.setColumnWidth(i, 6000);
cell = row.createCell(i);
cell.setCellValue(title.get(i));
HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 15);
style.setFont(font);
cell.setCellStyle(style);
}
try {
HSSFCellStyle styleCon = wb.createCellStyle();
styleCon.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleCon.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
for (int i = 0; i < list.size(); i++) {
BufferedImage bufferImg = null;
row = sheet.createRow(i + 1);
row.setHeight((short) 550);
EquipmentContrastDTO keeSpecimen = list.get(i);
List<FdEquipmentParameterVo> parameterList = keeSpecimen.getFdEquipmentParameterList();
String imagePath = list.get(i).getShowPicture();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
if (StrUtil.isNotEmpty(imagePath)) {
URL url = new URL(imagePath);
String formatName = imagePath.substring(imagePath.lastIndexOf(".") + 1);
bufferImg = ImageIO.read(url);
byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferImg, formatName, byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(480, 30, 700, 250,
(short) 1, i + 1, (short) 1, i + 1);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
}
cell = row.createCell(0);
cell.setCellValue(keeSpecimen.getEquipmentName());
cell.setCellStyle(styleCon);
int j = 1;
for (int i1 = 0; i1 < parameterList.size(); i1++) {
j++;
cell = row.createCell(j);
cell.setCellValue(parameterList.get(i1).getParameterValue());
cell.setCellStyle(styleCon);
}
}
return wb;
} catch (Exception e) {
System.err.println(e.getMessage());
}
return wb;
}
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
