add add pic

This commit is contained in:
looly 2021-12-20 17:35:47 +08:00
parent 76014cbbe2
commit f9bd66c4e3
2 changed files with 41 additions and 34 deletions

View File

@ -16,13 +16,24 @@ import cn.hutool.poi.excel.cell.CellLocation;
import cn.hutool.poi.excel.cell.CellUtil; import cn.hutool.poi.excel.cell.CellUtil;
import cn.hutool.poi.excel.style.Align; import cn.hutool.poi.excel.style.Align;
import org.apache.poi.common.usermodel.Hyperlink; import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
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.Drawing;
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.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation; import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import java.io.*; import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
@ -854,12 +865,13 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* 添加图片到当前sheet中 / 默认图片类型png / 默认的起始坐标和结束坐标都为0 * 添加图片到当前sheet中 / 默认图片类型png / 默认的起始坐标和结束坐标都为0
* *
* @param imgFile 图片文件 * @param imgFile 图片文件
* @param col1 指定起始的单元格下标从0开始 * @param col1 指定起始的下标从0开始
* @param row1 指定起始的单元格下标从0开始 * @param row1 指定起始的下标从0开始
* @param col2 指定结束的单元格下标从0开始 * @param col2 指定结束的下标从0开始
* @param row2 指定结束的单元格下标从0开始 * @param row2 指定结束的下标从0开始
* @return this * @return this
* @author vhukze * @author vhukze
* @since 5.7.18
*/ */
public ExcelWriter writeImg(File imgFile, int col1, int row1, int col2, int row2) { 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); return this.writeImg(imgFile, 0, 0, 0, 0, col1, row1, col2, row2);
@ -874,12 +886,13 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param dy1 起始单元格中的y坐标 * @param dy1 起始单元格中的y坐标
* @param dx2 结束单元格中的x坐标 * @param dx2 结束单元格中的x坐标
* @param dy2 结束单元格中的y坐标 * @param dy2 结束单元格中的y坐标
* @param col1 指定起始的单元格下标从0开始 * @param col1 指定起始的下标从0开始
* @param row1 指定起始的单元格下标从0开始 * @param row1 指定起始的下标从0开始
* @param col2 指定结束的单元格下标从0开始 * @param col2 指定结束的下标从0开始
* @param row2 指定结束的单元格下标从0开始 * @param row2 指定结束的下标从0开始
* @return this * @return this
* @author vhukze * @author vhukze
* @since 5.7.18
*/ */
public ExcelWriter writeImg(File imgFile, int dx1, int dy1, int dx2, int dy2, int col1, int row1, public ExcelWriter writeImg(File imgFile, int dx1, int dy1, int dx2, int dy2, int col1, int row1,
int col2, int row2) { int col2, int row2) {
@ -896,35 +909,28 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param dy1 起始单元格中的y坐标 * @param dy1 起始单元格中的y坐标
* @param dx2 结束单元格中的x坐标 * @param dx2 结束单元格中的x坐标
* @param dy2 结束单元格中的y坐标 * @param dy2 结束单元格中的y坐标
* @param col1 指定起始的单元格下标从0开始 * @param col1 指定起始的下标从0开始
* @param row1 指定起始的单元格下标从0开始 * @param row1 指定起始的下标从0开始
* @param col2 指定结束的单元格下标从0开始 * @param col2 指定结束的下标从0开始
* @param row2 指定结束的单元格下标从0开始 * @param row2 指定结束的下标从0开始
* @return this * @return this
* @author vhukze * @author vhukze
* @since 5.7.18
*/ */
public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2, public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2,
int dy2, int col1, int row1, int col2, int row2) { int dy2, int col1, int row1, int col2, int row2) {
Drawing<?> patriarch = this.sheet.createDrawingPatriarch(); Drawing<?> patriarch = this.sheet.createDrawingPatriarch();
ClientAnchor anchor; ClientAnchor anchor = this.workbook.getCreationHelper().createClientAnchor();
if (this.isXlsx()) { anchor.setDx1(dx1);
anchor = new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); anchor.setDy1(dy1);
} else { anchor.setDx2(dx2);
anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short) col1, row1, (short) col2, row2); anchor.setDy2(dy2);
} anchor.setCol1(col1);
anchor.setRow1(row1);
anchor.setCol2(col2);
anchor.setRow2(row2);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); patriarch.createPicture(anchor, this.workbook.addPicture(FileUtil.readBytes(imgFile), imgType));
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; return this;
} }

View File

@ -809,6 +809,7 @@ public class ExcelWriteTest {
} }
@Test @Test
@Ignore
public void writeImgTest() { public void writeImgTest() {
ExcelWriter writer = ExcelUtil.getWriter(true); ExcelWriter writer = ExcelUtil.getWriter(true);