add writeSimpleShape

This commit is contained in:
Looly 2024-08-11 14:09:12 +08:00
parent 191d6030ac
commit 87453459d8
33 changed files with 681 additions and 97 deletions

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.poi.excel;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellReferenceUtil;
import java.util.ArrayList;

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.poi.excel;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.io.file.FileTypeUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.multi.ListValueMap;
import org.dromara.hutool.core.text.StrUtil;
@ -32,6 +33,7 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import java.io.File;
import java.util.List;
/**
@ -40,11 +42,37 @@ import java.util.List;
* @author looly
* @since 4.0.7
*/
public class ExcelPicUtil {
public class ExcelImgUtil {
/**
* 获取图片类型
*
* @param imgFile 图片文件
* @return 图片类型默认PNG
* @since 6.0.0
*/
public static int getImgType(final File imgFile) {
final String type = FileTypeUtil.getType(imgFile);
if (StrUtil.equalsAnyIgnoreCase(type, "jpg", "jpeg")) {
return Workbook.PICTURE_TYPE_JPEG;
} else if (StrUtil.equalsAnyIgnoreCase(type, "emf")) {
return Workbook.PICTURE_TYPE_EMF;
} else if (StrUtil.equalsAnyIgnoreCase(type, "wmf")) {
return Workbook.PICTURE_TYPE_WMF;
} else if (StrUtil.equalsAnyIgnoreCase(type, "pict")) {
return Workbook.PICTURE_TYPE_PICT;
} else if (StrUtil.equalsAnyIgnoreCase(type, "dib")) {
return Workbook.PICTURE_TYPE_DIB;
}
// 默认格式
return Workbook.PICTURE_TYPE_PNG;
}
/**
* 获取工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射键格式行_列{@link PictureData}
*/
@ -64,10 +92,11 @@ public class ExcelPicUtil {
}
// -------------------------------------------------------------------------------------------------------------- Private method start
/**
* 获取XLS工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射键格式行_列{@link PictureData}
*/
@ -92,7 +121,7 @@ public class ExcelPicUtil {
/**
* 获取XLSX工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射键格式行_列{@link PictureData}
*/
@ -107,7 +136,7 @@ public class ExcelPicUtil {
XSSFPicture pic;
CTMarker ctMarker;
for (final XSSFShape shape : shapes) {
if(shape instanceof XSSFPicture){
if (shape instanceof XSSFPicture) {
pic = (XSSFPicture) shape;
ctMarker = pic.getPreferredSize().getFrom();
sheetIndexPicMap.putValue(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData());

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.poi.excel;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

View File

@ -0,0 +1,186 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel;
import org.apache.poi.ss.usermodel.ClientAnchor;
/**
* 简单的ClientAnchor实现此对象表示一个图形或绘图在Excel中的位置和大小参数表示
* <ul>
* <li>dx1和dy1表示在左上角单元格中的偏移量col1和row1表示左上角单元格</li>
* <li>dx2和dy2表示在右下角单元格中的偏移量col2和row2表示右下角单元格</li>
* </ul>
* 图示见https://www.cnblogs.com/sunyl/p/7527703.html
*/
public class SimpleClientAnchor implements ClientAnchor {
private int dx1;
private int dy1;
private int dy2;
private int dx2;
private int col1;
private int row1;
private int col2;
private int row2;
private AnchorType anchorType = AnchorType.MOVE_AND_RESIZE;
/**
* 构造<br>
* 通过定义左上角和右下角的单元格创建一个ClientAnchor对象<br>
* 默认偏移为0默认type为 {@link AnchorType#MOVE_AND_RESIZE}.
*
* @param col1 指定起始的列下标从0开始
* @param row1 指定起始的行下标从0开始
* @param col2 指定结束的列下标从0开始
* @param row2 指定结束的行下标从0开始
*/
public SimpleClientAnchor(final int col1, final int row1, final int col2, final int row2) {
this(0, 0, 0, 0, col1, row1, col2, row2);
}
/**
* 构造<br>
* 通过定义左上角和右下角的单元格以及单元格中的偏移量创建一个ClientAnchor对象<br>
* 默认type为 {@link AnchorType#MOVE_AND_RESIZE}.
*
* @param dx1 起始单元格中的x偏移像素
* @param dy1 起始单元格中的y偏移像素
* @param dx2 结束单元格中的x偏移像素
* @param dy2 结束单元格中的y偏移像素
* @param col1 指定起始的列下标从0开始
* @param row1 指定起始的行下标从0开始
* @param col2 指定结束的列下标从0开始
* @param row2 指定结束的行下标从0开始
*/
public SimpleClientAnchor(final int dx1, final int dy1, final int dx2, final int dy2, final int col1, final int row1, final int col2, final int row2) {
this.dx1 = dx1;
this.dy1 = dy1;
this.dx2 = dx2;
this.dy2 = dy2;
this.col1 = col1;
this.row1 = row1;
this.col2 = col2;
this.row2 = row2;
}
@Override
public int getDx1() {
return this.dx1;
}
@Override
public void setDx1(final int dx1) {
this.dx1 = dx1;
}
@Override
public int getDy1() {
return this.dy1;
}
@Override
public void setDy1(final int dy1) {
this.dy1 = dy1;
}
@Override
public int getDx2() {
return this.dx2;
}
@Override
public void setDx2(final int dx2) {
this.dx2 = dx2;
}
@Override
public int getDy2() {
return this.dy2;
}
@Override
public void setDy2(final int dy2) {
this.dy2 = dy2;
}
@Override
public short getCol1() {
return (short) this.col1;
}
@Override
public void setCol1(final int col1) {
this.col1 = col1;
}
@Override
public int getRow1() {
return this.row1;
}
@Override
public void setRow1(final int row1) {
this.row1 = row1;
}
@Override
public short getCol2() {
return (short) this.col2;
}
@Override
public void setCol2(final int col2) {
this.col2 = col2;
}
@Override
public int getRow2() {
return this.row2;
}
@Override
public void setRow2(final int row2) {
this.row2 = row2;
}
@Override
public AnchorType getAnchorType() {
return this.anchorType;
}
@Override
public void setAnchorType(final AnchorType anchorType) {
this.anchorType = anchorType;
}
/**
* 将当前对象中的值复制到目标对象中
*
* @param clientAnchor {@link ClientAnchor}
* @return 目标对象
*/
public ClientAnchor copyTo(final ClientAnchor clientAnchor) {
clientAnchor.setDx1(this.dx1);
clientAnchor.setDy1(this.dy1);
clientAnchor.setDx2(this.dx2);
clientAnchor.setDy2(this.dy2);
clientAnchor.setCol1(this.col1);
clientAnchor.setRow1(this.row1);
clientAnchor.setCol2(this.col2);
clientAnchor.setRow2(this.row2);
clientAnchor.setAnchorType(this.anchorType);
return clientAnchor;
}
}

View File

@ -18,7 +18,9 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.ss.util.SheetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.TrimEditor;
import org.dromara.hutool.poi.excel.cell.setters.CellSetter;
import org.dromara.hutool.poi.excel.cell.setters.CellSetterFactory;
import org.dromara.hutool.poi.excel.cell.values.CompositeCellValue;
import org.dromara.hutool.poi.excel.style.StyleSet;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 looly(loolly@aliyun.com)
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.cell;
package org.dromara.hutool.poi.excel.cell.editors;
import org.apache.poi.ss.usermodel.Cell;

View File

@ -14,8 +14,6 @@ package org.dromara.hutool.poi.excel.cell.editors;
import org.apache.poi.ss.usermodel.Cell;
import org.dromara.hutool.poi.excel.cell.CellEditor;
/**
* POI中NUMRIC类型的值默认返回的是Double类型此编辑器用于转换其为int型
*

View File

@ -15,7 +15,6 @@ package org.dromara.hutool.poi.excel.cell.editors;
import org.apache.poi.ss.usermodel.Cell;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
/**
* 去除String类型的单元格值两边的空格

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
/**

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import java.util.Calendar;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 looly(loolly@aliyun.com)
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.cell;
package org.dromara.hutool.poi.excel.cell.setters;
import org.apache.poi.ss.usermodel.Cell;

View File

@ -12,10 +12,10 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import java.io.File;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
@ -53,6 +53,11 @@ public class CellSetterFactory {
return new NumberCellSetter((Number) value);
}else if (value instanceof Hyperlink) {
return new HyperlinkCellSetter((Hyperlink) value);
}else if (value instanceof byte[]) {
// 二进制理解为图片
return new ImgCellSetter((byte[]) value);
}else if (value instanceof File) {
return new ImgCellSetter((File) value);
} else {
return new CharSequenceCellSetter(value.toString());
}

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
/**

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import java.util.Date;

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Hyperlink;

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.cell.setters;
import org.apache.poi.ss.usermodel.*;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.poi.excel.ExcelImgUtil;
import java.io.File;
/**
* 图片单元格值设置器
*
* @author Looly
* @since 6.0.0
*/
public class ImgCellSetter implements CellSetter {
private final byte[] pictureData;
private final int imgType;
// region ----- 构造
/**
* 构造默认PNG图片
*
* @param pictureData 图片数据
*/
public ImgCellSetter(final byte[] pictureData) {
this(pictureData, Workbook.PICTURE_TYPE_PNG);
}
/**
* 构造
*
* @param picturefile 图片数据
*/
public ImgCellSetter(final File picturefile) {
this(FileUtil.readBytes(picturefile), ExcelImgUtil.getImgType(picturefile));
}
/**
* 构造
*
* @param pictureData 图片数据
* @param imgType 图片类型
*/
public ImgCellSetter(final byte[] pictureData, final int imgType) {
this.pictureData = pictureData;
this.imgType = imgType;
}
// endregion
@Override
public void setValue(final Cell cell) {
final Sheet sheet = cell.getSheet();
final Workbook workbook = sheet.getWorkbook();
final Drawing<?> patriarch = sheet.createDrawingPatriarch();
final ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
final int columnIndex = cell.getColumnIndex();
final int rowIndex = cell.getRowIndex();
// 填充当前单元格
anchor.setCol1(columnIndex);
anchor.setRow1(rowIndex);
anchor.setCol2(columnIndex + 1);
anchor.setRow2(rowIndex + 1);
patriarch.createPicture(anchor, workbook.addPicture(this.pictureData, this.imgType));
}
}

View File

@ -13,17 +13,19 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
/**
* {@link Number} 值单元格设置器
* {@code null} 值单元格设置器
*
* @author looly
* @since 5.7.8
*/
public class NullCellSetter implements CellSetter {
/**
* 默认单例
*/
public static final NullCellSetter INSTANCE = new NullCellSetter();
@Override

View File

@ -13,7 +13,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
/**

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.RichTextString;

View File

@ -12,7 +12,6 @@
package org.dromara.hutool.poi.excel.cell.setters;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import java.time.Instant;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 looly(loolly@aliyun.com)
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.cell;
package org.dromara.hutool.poi.excel.cell.values;
/**
* 抽象的单元格值接口用于判断不同类型的单元格值<br>

View File

@ -15,9 +15,8 @@ package org.dromara.hutool.poi.excel.cell.values;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.dromara.hutool.poi.excel.cell.CellValue;
import org.dromara.hutool.poi.excel.cell.NullCell;
/**

View File

@ -13,7 +13,6 @@
package org.dromara.hutool.poi.excel.cell.values;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.cell.CellValue;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaError;

View File

@ -12,8 +12,7 @@
package org.dromara.hutool.poi.excel.cell.values;
import org.dromara.hutool.poi.excel.cell.CellSetter;
import org.dromara.hutool.poi.excel.cell.CellValue;
import org.dromara.hutool.poi.excel.cell.setters.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
/**

View File

@ -14,7 +14,6 @@ package org.dromara.hutool.poi.excel.cell.values;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.poi.excel.ExcelDateUtil;
import org.dromara.hutool.poi.excel.cell.CellValue;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.NumberToTextConverter;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.poi.excel.reader.sheet;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import java.util.ArrayList;

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.convert.Convert;
import org.apache.poi.ss.usermodel.Sheet;
import org.dromara.hutool.poi.excel.RowUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import java.util.ArrayList;
import java.util.List;

View File

@ -16,7 +16,7 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.dromara.hutool.core.func.SerBiConsumer;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellUtil;
/**

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.style;
import org.apache.poi.hssf.usermodel.HSSFShape;
/**
* SimpleShape中的线条风格枚举
*
* @author Looly
* @see HSSFShape
* @since 6.0.0
*/
public enum LineStyle {
/**
* Solid (continuous) pen
*/
SOLID(HSSFShape.LINESTYLE_SOLID),
/**
* PS_DASH system dash style
*/
DASHSYS(HSSFShape.LINESTYLE_DASHSYS),
/**
* PS_DOT system dash style
*/
DOTSYS(HSSFShape.LINESTYLE_DOTSYS),
/**
* PS_DASHDOT system dash style
*/
DASHDOTSYS(HSSFShape.LINESTYLE_DASHDOTSYS),
/**
* PS_DASHDOTDOT system dash style
*/
DASHDOTDOTSYS(HSSFShape.LINESTYLE_DASHDOTDOTSYS),
/**
* square dot style
*/
DOTGEL(HSSFShape.LINESTYLE_DOTGEL),
/**
* dash style
*/
DASHGEL(HSSFShape.LINESTYLE_DASHGEL),
/**
* long dash style
*/
LONGDASHGEL(HSSFShape.LINESTYLE_LONGDASHGEL),
/**
* dash short dash
*/
DASHDOTGEL(HSSFShape.LINESTYLE_DASHDOTGEL),
/**
* long dash short dash
*/
LONGDASHDOTGEL(HSSFShape.LINESTYLE_LONGDASHDOTGEL),
/**
* long dash short dash short dash
*/
LONGDASHDOTDOTGEL(HSSFShape.LINESTYLE_LONGDASHDOTDOTGEL),
/**
*
*/
NONE(HSSFShape.LINESTYLE_NONE);
private final int value;
/**
* 构造
*
* @param value 样式编码
*/
LineStyle(final int value) {
this.value = value;
}
/**
* 获取样式编码
*
* @return 样式编码
*/
public int getValue() {
return value;
}
}

View File

@ -0,0 +1,147 @@
package org.dromara.hutool.poi.excel.style;
import org.apache.poi.sl.usermodel.ShapeType;
import java.awt.Color;
import java.io.Serializable;
/**
* 形状配置
* 用于在Excel中定义形状的样式包括形状类型线条样式线条宽度线条颜色填充颜色等
*
* @author Looly
* @since 6.0.0
*/
public class ShapeConfig implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建一个形状配置
*
* @return ShapeConfig
*/
public static ShapeConfig of() {
return new ShapeConfig();
}
/**
* 形状类型如矩形圆形等默认直线
*/
private ShapeType shapeType = ShapeType.LINE;
/**
* 线条样式如实线虚线等默认实线
*/
private LineStyle lineStyle = LineStyle.SOLID;
/**
* 线条宽度以磅为单位
*/
private int lineWidth = 1;
/**
* 线条颜色
*/
private Color lineColor = Color.BLACK;
/**
* 填充颜色{@code null表示不填充}
*/
private Color fillColor;
/**
* 获取形状类型
*
* @return 形状类型
*/
public ShapeType getShapeType() {
return shapeType;
}
/**
* 设置形状类型
*
* @param shapeType 形状类型
* @return 当前形状配置对象用于链式调用
*/
public ShapeConfig setShapeType(final ShapeType shapeType) {
this.shapeType = shapeType;
return this;
}
/**
* 获取线条样式
*
* @return 线条样式
*/
public LineStyle getLineStyle() {
return lineStyle;
}
/**
* 设置线条样式
*
* @param lineStyle 线条样式
* @return 当前形状配置对象用于链式调用
*/
public ShapeConfig setLineStyle(final LineStyle lineStyle) {
this.lineStyle = lineStyle;
return this;
}
/**
* 获取线条宽度
*
* @return 线条宽度以磅为单位
*/
public int getLineWidth() {
return lineWidth;
}
/**
* 设置线条宽度
*
* @param lineWidth 线条宽度以磅为单位
* @return 当前形状配置对象用于链式调用
*/
public ShapeConfig setLineWidth(final int lineWidth) {
this.lineWidth = lineWidth;
return this;
}
/**
* 获取线条颜色
*
* @return 线条颜色
*/
public Color getLineColor() {
return lineColor;
}
/**
* 设置线条颜色
*
* @param lineColor 线条颜色
* @return 当前形状配置对象用于链式调用
*/
public ShapeConfig setLineColor(final Color lineColor) {
this.lineColor = lineColor;
return this;
}
/**
* 获取填充颜色{@code null表示不填充}
*
* @return 填充颜色{@code null表示不填充}
*/
public Color getFillColor() {
return fillColor;
}
/**
* 设置填充颜色{@code null表示不填充}
*
* @param fillColor 填充颜色{@code null表示不填充}
* @return 当前形状配置对象用于链式调用
*/
public ShapeConfig setFillColor(final Color fillColor) {
this.fillColor = fillColor;
return this;
}
}

View File

@ -13,13 +13,15 @@
package org.dromara.hutool.poi.excel.writer;
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.*;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.io.IORuntimeException;
@ -33,17 +35,13 @@ import org.dromara.hutool.core.map.multi.RowKeyTable;
import org.dromara.hutool.core.map.multi.Table;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.excel.SheetUtil;
import org.dromara.hutool.poi.excel.ExcelBase;
import org.dromara.hutool.poi.excel.RowUtil;
import org.dromara.hutool.poi.excel.WorkbookUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.*;
import org.dromara.hutool.poi.excel.cell.CellRangeUtil;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.dromara.hutool.poi.excel.style.Align;
import org.dromara.hutool.poi.excel.style.DefaultStyleSet;
import org.dromara.hutool.poi.excel.style.StyleSet;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.style.*;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
@ -761,89 +759,121 @@ public class ExcelWriter extends ExcelBase<ExcelWriter, ExcelWriteConfig> {
* @since 5.7.18
*/
public ExcelWriter writeImg(final File imgFile, final int col1, final int row1, final int col2, final int row2) {
return this.writeImg(imgFile, 0, 0, 0, 0, col1, row1, col2, row2);
return writeImg(imgFile, new SimpleClientAnchor(col1, row1, col2, row2));
}
/**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 添加图片到当前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开始
* @param imgFile 图片文件
* @param clientAnchor 图片的位置和大小信息
* @return this
* @author vhukze
* @since 5.7.18
* @since 6.0.0
*/
public ExcelWriter writeImg(final File imgFile, final int dx1, final int dy1, final int dx2, final int dy2, final int col1, final int row1,
final int col2, final int row2) {
return this.writeImg(imgFile, Workbook.PICTURE_TYPE_PNG, dx1, dy1, dx2, dy2, col1, row1, col2, row2);
public ExcelWriter writeImg(final File imgFile, final SimpleClientAnchor clientAnchor) {
return writeImg(imgFile, ExcelImgUtil.getImgType(imgFile), clientAnchor);
}
/**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 添加图片到当前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开始
* @param imgFile 图片文件
* @param imgType 图片类型对应poi中Workbook类中的图片类型2-7变量
* @param clientAnchor 图片的位置和大小信息
* @return this
* @author vhukze
* @since 5.7.18
* @since 6.0.0
*/
public ExcelWriter writeImg(final File imgFile, final int imgType, final int dx1, final int dy1, final int dx2,
final int dy2, final int col1, final int row1, final int col2, final int row2) {
return writeImg(FileUtil.readBytes(imgFile), imgType, dx1,
dy1, dx2, dy2, col1, row1, col2, row2);
public ExcelWriter writeImg(final File imgFile, final int imgType, final SimpleClientAnchor clientAnchor) {
return writeImg(FileUtil.readBytes(imgFile), imgType, clientAnchor);
}
/**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 添加图片到当前sheet中
*
* @param pictureData 数据bytes
* @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开始
* @param pictureData 数据bytes
* @param imgType 图片类型对应poi中Workbook类中的图片类型2-7变量
* @param clientAnchor 图片的位置和大小信息
* @return this
* @author vhukze
* @since 5.8.0
* @since 6.0.0
*/
public ExcelWriter writeImg(final byte[] pictureData, final int imgType, final int dx1, final int dy1, final int dx2,
final int dy2, final int col1, final int row1, final int col2, final int row2) {
public ExcelWriter writeImg(final byte[] pictureData, final int imgType, final SimpleClientAnchor clientAnchor) {
final Drawing<?> patriarch = this.sheet.createDrawingPatriarch();
final ClientAnchor anchor = this.workbook.getCreationHelper().createClientAnchor();
anchor.setDx1(dx1);
anchor.setDy1(dy1);
anchor.setDx2(dx2);
anchor.setDy2(dy2);
anchor.setCol1(col1);
anchor.setRow1(row1);
anchor.setCol2(col2);
anchor.setRow2(row2);
clientAnchor.copyTo(anchor);
patriarch.createPicture(anchor, this.workbook.addPicture(pictureData, imgType));
return this;
}
/**
* 绘制线条
*
* @param clientAnchor 绘制区域信息
* @return this
* @since 6.0.0
*/
public ExcelWriter writeLineShape(final SimpleClientAnchor clientAnchor) {
return writeSimpleShape(clientAnchor, ShapeConfig.of());
}
/**
* 绘制线条
*
* @param clientAnchor 绘制区域信息
* @param lineStyle 线条样式
* @param lineWidth 线条粗细
* @param lineColor 线条颜色
* @return this
* @since 6.0.0
*/
public ExcelWriter writeLineShape(final SimpleClientAnchor clientAnchor, final LineStyle lineStyle, final int lineWidth, final Color lineColor) {
return writeSimpleShape(clientAnchor, ShapeConfig.of().setLineStyle(lineStyle).setLineWidth(lineWidth).setLineColor(lineColor));
}
/**
* 绘制简单形状
*
* @param clientAnchor 绘制区域信息
* @param shapeConfig 形状配置包括形状类型线条样式线条宽度线条颜色填充颜色等
* @return this
* @since 6.0.0
*/
public ExcelWriter writeSimpleShape(final SimpleClientAnchor clientAnchor, ShapeConfig shapeConfig) {
final Drawing<?> patriarch = this.sheet.createDrawingPatriarch();
final ClientAnchor anchor = this.workbook.getCreationHelper().createClientAnchor();
clientAnchor.copyTo(anchor);
if (null == shapeConfig) {
shapeConfig = ShapeConfig.of();
}
final Color lineColor = shapeConfig.getLineColor();
if (patriarch instanceof HSSFPatriarch) {
final HSSFSimpleShape simpleShape = ((HSSFPatriarch) patriarch).createSimpleShape((HSSFClientAnchor) anchor);
simpleShape.setShapeType(shapeConfig.getShapeType().ooxmlId);
simpleShape.setLineStyle(shapeConfig.getLineStyle().getValue());
simpleShape.setLineWidth(shapeConfig.getLineWidth());
if (null != lineColor) {
simpleShape.setLineStyleColor(lineColor.getRed(), lineColor.getGreen(), lineColor.getBlue());
}
} else if (patriarch instanceof XSSFDrawing) {
final XSSFSimpleShape simpleShape = ((XSSFDrawing) patriarch).createSimpleShape((XSSFClientAnchor) anchor);
simpleShape.setShapeType(shapeConfig.getShapeType().ooxmlId);
simpleShape.setLineStyle(shapeConfig.getLineStyle().getValue());
simpleShape.setLineWidth(shapeConfig.getLineWidth());
if (null != lineColor) {
simpleShape.setLineStyleColor(lineColor.getRed(), lineColor.getGreen(), lineColor.getBlue());
}
} else {
throw new UnsupportedOperationException("Unsupported patriarch type: " + patriarch.getClass().getName());
}
return this;
}
// endregion
// region ----- writeRow

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.poi.excel.reader;
import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;

View File

@ -0,0 +1,24 @@
package org.dromara.hutool.poi.excel.writer;
import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.SimpleClientAnchor;
import org.dromara.hutool.poi.excel.style.LineStyle;
import org.junit.jupiter.api.Test;
import java.awt.Color;
public class WriteLineShapeTest {
@Test
void testWriteLineShape() {
// Setup
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/lineShape.xlsx");
final SimpleClientAnchor clientAnchor = new SimpleClientAnchor(0, 0, 1, 1);
final LineStyle lineStyle = LineStyle.SOLID;
final int lineWidth = 1;
// Execute
writer.writeLineShape(clientAnchor, lineStyle, lineWidth, Color.RED);
writer.close();
}
}