excel转图片工具类
作者:互联网
添加依赖
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.9</version>
</dependency>
核心工具类
package com.cn.travel.web.test;
import com.aspose.cells.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* excel转图片工具类
*/
public class AsposeUtil {
/**
* excel转图片
* @param in 输入流
*/
// public static void toJPG(InputStream in) {
public static InputStream toJPG(InputStream in) throws Exception{
try {
Workbook book = new Workbook(in);
Worksheet sheet = book.getWorksheets().get(0);
// 设置图片的上下左右的白边宽度
sheet.getPageSetup().setLeftMargin(-10);
sheet.getPageSetup().setRightMargin(1);
sheet.getPageSetup().setBottomMargin(1);
sheet.getPageSetup().setTopMargin(1);
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setCellAutoFit(true);
imgOptions.setOnePagePerSheet(true);
SheetRender render = new SheetRender(sheet, imgOptions);
// 已经转好的图片流
ByteArrayOutputStream out = new ByteArrayOutputStream();
render.toImage(0, out);
// 截取图片去除水印
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
BufferedImage image = ImageIO.read(inputStream);
image = image.getSubimage(0, 20, image.getWidth(), image.getHeight()-20);
//生成于本地
// ImageIO.write(image, "png", new File("E:\\aa\\bb.jpg"));
//返回输入流
InputStream is = null;
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut;
imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "png", imOut);
is= new ByteArrayInputStream(bs.toByteArray());
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
controller
@RestController
//@RequestMapping("/test")
public class TestController {
@GetMapping("/exportPic")
public void exportPic(HttpServletResponse response) throws Exception {
TemplateExportParams params = new TemplateExportParams(
"templates/10仓zz.xlsx");
Map<String, Object> map = new HashMap<>();
map.put("purchasePlanNo", "123456");
map.put("boxesSum", 2);
map.put("torrentSum", "2");
List<Map<String, Object>> invoiceData = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Map<String, Object> lm = new HashMap<>();
lm.put("id", i);
lm.put("recordCommodityId", 44796);
lm.put("booksForReferenceNumber", 2);
lm.put("nsNo", "NS8070");
lm.put("customsRecordNumber", "10500051");
lm.put("commodityInspectionRecordNumber", "");
lm.put("hsCode", "3401199090");
lm.put("commodityBrand", "LION狮王");
lm.put("barcode", "202108270002");
lm.put("productName", "LION狮王 趣净除菌湿巾 无酒精 10片");
lm.put("quantity", 50);
lm.put("recordMeasurement", "袋");
lm.put("singlePrice", 100);
lm.put("totalPrice", 5000);
lm.put("originCountry", "日本");
lm.put("repeatRemark", "存在重复备案");
invoiceData.add(lm);
}
map.put("invoiceData", invoiceData);
map.put("year", 2021);
map.put("month", 9);
map.put("day", 13);
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream inputStream = new ByteArrayInputStream(content);
InputStream in = AsposeUtil.toJPG(inputStream);
// AsposeUtil.toJPG(in);
OutputStream out = null;
try {
//设置文件MIME类型
String fileName = "aa.jpg";
response.setContentType("application/octet-stream");
try {
fileName = new String((fileName).getBytes("gb2312"), "ISO8859-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.flush();
in.close();
out.close();
} finally {
try {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
标签:map,lm,excel,import,put,new,工具,out,图片 来源: https://blog.csdn.net/weixin_44854514/article/details/120450722