最近做的项目中涉及到了利用poi进行excel导出,本身导出没有什么难度的,但是遇到一个需要导出图片的需求,在实现上有一点阻力,现在解决了,记录一下。
<!-- start POI Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能 start -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<!-- end POI end -->
这个代码是我写的一个简单版的,工具类可以自行编码
/**
* 合同下主成品信息成品条形码 excel导出
*
* @param fileName 下载文件的名称
* @param list 导出excel数据
* @throws IOException
*/
public void saveContractMainFpCodes(HttpServletResponse response, String fileName, List<FinishedProductDto> list) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet页名称");
//标识位,用于标识sheet的行号
int rowIndex = 1;
Row rowTitle = sheet.createRow(0);
//自定义行头标题的内容
String[] titles = new String[]{};
for (int i = 0; i < titles.length; i++) {
rowTitle.createCell(i).setCellValue(titles[i]);
}
try {
//循环写入主表数据
for (Iterator<FinishedProductDto> iter = list.iterator(); iter.hasNext(); ) {
FinishedProductDto product = iter.next();
Row row = sheet.createRow(rowIndex);
Cell cell0 = row.createCell(2);
cell0.setCellValue(product.getPactId());
Cell cell2 = row.createCell(3);
cell2.setCellValue(product.getMaTally());
Cell cell3 = row.createCell(4);
cell3.setCellValue(product.getMaName());
Cell cell5 = row.createCell(5);
cell5.setCellValue(product.getDeliveryTime().toString());
Cell cell6 = row.createCell(6);
cell6.setCellValue(product.getBreakTime().toString());
//调用Drawing对象进行绘画操作
XSSFDrawing drawingPatriarch = (XSSFDrawing) sheet.createDrawingPatriarch();
//使用Anchor进行图片位置等方面的调节
XSSFClientAnchor anchor = new XSSFClientAnchor(300, 75, 700, 250, (short) 0, rowIndex, (short) 1, rowIndex + 1);
//product.getBarcodeImg()是一个byte[]
//根据anchor和图片的byte[]来进行创建
drawingPatriarch.createPicture(anchor, workbook.addPicture(product.getBarcodeImg(), XSSFWorkbook.PICTURE_TYPE_JPEG));
//y = (x + 0.72)*256
//x是excel表格宽度
//x是代码宽度
int width = (int) ((15 + 0.72) * 256);
if (rowIndex == 1) {
width = (int) ((32 + 0.72) * 256);
}
if (rowIndex == 3) {
width = (int) ((30 + 0.72) * 256);
}
//这里对每一个单元格的宽高进行了设置,具体的宽高以图片宽高为核心
sheet.setColumnWidth(rowIndex - 1, width);
row.setHeight((short) 900);
//显示网格
sheet.setDisplayGridlines(true);
Cell cell1 = row.createCell(1);
cell1.setCellValue(product.getMainFpBarcode());
rowIndex++;
}
//设置reponse一些响应属性,返回文件流给前端
response.setContentType("APPLICATION/OCTET-STREAM");
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream fos = response.getOutputStream();
workbook.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
附:POI中文文档
本文地址:https://blog.csdn.net/weixin_43398895/article/details/107140818
如对本文有疑问, 点击进行留言回复!!
利用python将Mysql信息以Excel文件并作为邮件附件发送
springmvc+mybaits+mysql上传表情Incorrect string value: ‘\xF0\x9F\xA4\xB4\xF0\x9F...‘ for
SpringCloud Greenwich集成Seata1.2.0详解说明(原创by ulwfcyvi)
mybatis generator生成代码库 与指定的库不一致 为其他库的同名表
网友评论