public static void toLeadPicture(SXSSFWorkbook workbook, SXSSFSheet sheet, String fileUrl, int row, int col, double scaleX, double scaleY) {
try {
//防止URL地址有中文,解码
String head = fileUrl.substring(0, fileUrl.lastIndexOf("/")+1);
String suffix = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
String link = head + URLEncoder.encode(suffix,"UTF-8");
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>拼接URL为:[{}]", link);
// 构造URL
URL url = new URL(link);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(8 * 1000);
// 输入流
InputStream is = con.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
@SuppressWarnings("static-access")
int pictureIdx = workbook.addPicture(bytes, workbook.PICTURE_TYPE_PNG);
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
// 图片插入坐标
anchor.setCol1(col);
anchor.setRow1(row);
// 插入图片
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize(scaleX, scaleY);
// 设置宽度
sheet.setColumnWidth(row, 10 * 256);
// 设置高度
Row rowl = sheet.createRow(col);
rowl.setHeight((short) (30 * 20));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结合之前的导出教程使用该方法