From 01b0e35f01d12ee3564000b1334ad3bc5314cb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B0=91=E6=B3=BD?= Date: Sat, 18 Dec 2021 20:34:34 +0800 Subject: [PATCH] =?UTF-8?q?excel=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/poi/excel/ExcelWriter.java | 96 ++++++++++++++++--- .../cn/hutool/poi/excel/ExcelWriteTest.java | 14 +++ 2 files changed, 97 insertions(+), 13 deletions(-) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index 68f8d7c7f..402bf81f2 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -16,22 +16,13 @@ import cn.hutool.poi.excel.cell.CellLocation; import cn.hutool.poi.excel.cell.CellUtil; import cn.hutool.poi.excel.style.Align; import org.apache.poi.common.usermodel.Hyperlink; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.DataValidation; -import org.apache.poi.ss.usermodel.DataValidationConstraint; -import org.apache.poi.ss.usermodel.DataValidationHelper; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.HeaderFooter; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddressList; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDataValidation; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.nio.charset.Charset; import java.util.Comparator; import java.util.Iterator; @@ -858,6 +849,85 @@ public class ExcelWriter extends ExcelBase { return this; } + /** + * 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 添加图片到当前sheet中 / 默认图片类型png / 默认的起始坐标和结束坐标都为0 + * + * @param imgFile 图片文件 + * @param col1 指定起始的单元格,下标从0开始 + * @param row1 指定起始的单元格,下标从0开始 + * @param col2 指定结束的单元格,下标从0开始 + * @param row2 指定结束的单元格,下标从0开始 + * @return this + * @author vhukze + */ + public ExcelWriter writeImg(File imgFile, int col1, int row1, int col2, int row2) { + return this.writeImg(imgFile, 0, 0, 0, 0, col1, row1, col2, row2); + } + + /** + * 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 添加图片到当前sheet中 / 默认图片类型png + * + * @param imgFile 图片文件 + * @param dx1 起始单元格中的x坐标 + * @param dy1 起始单元格中的y坐标 + * @param dx2 结束单元格中的x坐标 + * @param dy2 结束单元格中的y坐标 + * @param col1 指定起始的单元格,下标从0开始 + * @param row1 指定起始的单元格,下标从0开始 + * @param col2 指定结束的单元格,下标从0开始 + * @param row2 指定结束的单元格,下标从0开始 + * @return this + * @author vhukze + */ + public ExcelWriter writeImg(File imgFile, int dx1, int dy1, int dx2, int dy2, int col1, int row1, + int col2, int row2) { + return this.writeImg(imgFile, Workbook.PICTURE_TYPE_PNG, dx1, dy1, dx2, dy2, col1, row1, col2, row2); + } + + /** + * 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 添加图片到当前sheet中 + * + * @param imgFile 图片文件 + * @param imgType 图片类型,对应poi中Workbook类中的图片类型2-7变量 + * @param dx1 起始单元格中的x坐标 + * @param dy1 起始单元格中的y坐标 + * @param dx2 结束单元格中的x坐标 + * @param dy2 结束单元格中的y坐标 + * @param col1 指定起始的单元格,下标从0开始 + * @param row1 指定起始的单元格,下标从0开始 + * @param col2 指定结束的单元格,下标从0开始 + * @param row2 指定结束的单元格,下标从0开始 + * @return this + * @author vhukze + */ + public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2, + int dy2, int col1, int row1, int col2, int row2) { + Drawing patriarch = this.sheet.createDrawingPatriarch(); + ClientAnchor anchor; + if (this.isXlsx()) { + anchor = new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); + } else { + anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short) col1, row1, (short) col2, row2); + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + FileInputStream is = new FileInputStream(imgFile); + int b; + while ((b = is.read()) != -1) { + bos.write(b); + } + } catch (IOException e) { + e.printStackTrace(); + } + + patriarch.createPicture(anchor, this.workbook.addPicture(bos.toByteArray(), imgType)); + return this; + } + /** * 写出一行标题数据
* 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java index 0dc63eade..5fd07ebc6 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java @@ -807,4 +807,18 @@ public class ExcelWriteTest { writer.merge(0,1,2,2,3.99,false); writer.close(); } + + @Test + public void writeImgTest() { + ExcelWriter writer = ExcelUtil.getWriter(true); + + File file = new File("C:\\Users\\zsz\\Desktop\\1.jpg"); + + writer.writeImg(file, 0, 0, 5, 10); + + writer.flush(new File("C:\\Users\\zsz\\Desktop\\2.xlsx")); + + writer.close(); + } + }