今天整理笔记的时候看到之前的一段代码,是有关于下载含有网络图片的excle,把代码贴出来大家让大家指导一下
下载下来的excel类似这种效果,我这边处理的是网络图片哟
1.部分代码展示
@RequestMapping(value = "downReadilyPager")
@ResponseBody
public void downReadilyPager(HttpServletRequest request, HttpServletResponse response) {
try {
ReadilyRequest readilyRequest = new ReadilyRequest();
List<Readily> list = new ArrayList<Readily>();
String ids = request.getParameter("ids");
if (!StringUtils.isBlank(ids)) {
String[] idArray = ids.split(",");
for (String id : idArray) {
Readily temp = readilyService.get(Integer.parseInt(id));
Account account = accountService.get(temp.getAccountId());
temp.setAccount(account);
list.add(temp);
}
}
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
HSSFSheet sheet = hssfWorkbook.createSheet("报表管理");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
//设置表格样式
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(4, 8000);
sheet.setColumnWidth(5, 8000);
sheet.setColumnWidth(6, 8000);
sheet.setColumnWidth(7, 8000);
sheet.setColumnWidth(8, 8000);
sheet.setColumnWidth(9, 8000);
sheet.setColumnWidth(10, 8000);
//一定要放在循环外,只能声明一次
String fileName = "报表-" + simpledate.format(new Date());
cell.setCellValue(fileName);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("主题");
cell = row.createCell(1);
cell.setCellValue("内容");
cell = row.createCell(2);
cell.setCellValue("地址");
cell = row.createCell(3);
cell.setCellValue("状态");
cell = row.createCell(4);
cell.setCellValue("拍摄时间");
cell = row.createCell(5);
cell.setCellValue("处理时间");
cell = row.createCell(6);
cell.setCellValue("描述");
cell = row.createCell(7);
cell.setCellValue("昵称");
cell = row.createCell(8);
cell.setCellValue("电话");
cell = row.createCell(9);
cell.setCellValue("图片");
int line = 2;
//循环 开始赋值
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Readily s = list.get(i);
row = sheet.createRow(line++);
if (s.getTitle() != null) {
cell = row.createCell(0);
row.setHeight((short) (6 * 200));
cell.setCellValue(s.getTitle());
}
if (s.getContent() != null) {
cell = row.createCell(1);
cell.setCellValue(s.getContent());
}
if (s.getAddress() != null) {
cell = row.createCell(2);
cell.setCellValue(s.getAddress());
}
if (s.getState() != null) {
cell = row.createCell(3);
if (s.getState() == 0) {
cell.setCellValue("未处理");
} else if (s.getState() == 1) {
cell.setCellValue("已经处理");
}
}
if (s.getTime() != null) {
cell = row.createCell(4);
cell.setCellValue(simpledate.format(s.getTime()));
}
if (s.getUpdateTime() != null) {
cell = row.createCell(5);
cell.setCellValue(simpledate.format(s.getUpdateTime()));
}
if (s.getRemark() != null) {
cell = row.createCell(6);
cell.setCellValue(s.getRemark());
}
if (s.getAccount().getNickName() != null) {
cell = row.createCell(7);
cell.setCellValue(s.getAccount().getNickName());
}
if (s.getAccount().getPhone() != null) {
cell = row.createCell(8);
cell.setCellValue(s.getAccount().getPhone());
}
if (!StringUtils.isBlank(s.getImgUrl())) {
String[] imgUrls = s.getImgUrl().split(",");
int length = imgUrls.length;
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//for循环中处理图片的关键代码
for (int j = 0; j < length; j++) {
BufferedImage bufferImg = null;
//网络图片用下面的方法处理不适合本地图片
URL url = new URL(imgUrls[j]);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(url);
ImageIO.write(bufferImg, "jpg", byteArrayOut);
//***(short) (9 + j), (2 + i), 图片起点的位置 (short) (10 + j), (3 + i) 图片终点的位置***
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (9 + j), (2 + i), (short) (10 + j), (3 + i));
patriarch.createPicture(anchor, hssfWorkbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
}
}
}
}
OutputStream fout = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
//表格写出
hssfWorkbook.write(fout);
fout.flush();
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.jar包下载地址
https://download.csdn.net/download/weixin_38638883/10652650