add locationRef

This commit is contained in:
Looly 2020-02-21 12:39:22 +08:00
parent 6dc12cf6fb
commit a8037833f2
4 changed files with 150 additions and 51 deletions

View File

@ -6,6 +6,8 @@
## 5.1.4 ## 5.1.4
### 新特性 ### 新特性
* 【poi 】 增加单元格位置引用例如A11等方式获取单元格
### Bug修复 ### Bug修复
* 【core 】 修复CombinationAnnotationElement数组判断问题issue#752@Github * 【core 】 修复CombinationAnnotationElement数组判断问题issue#752@Github

View File

@ -4,6 +4,7 @@ import java.io.Closeable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cn.hutool.poi.excel.cell.CellLocation;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -18,22 +19,28 @@ import cn.hutool.poi.excel.style.StyleUtil;
/** /**
* Excel基础类用于抽象ExcelWriter和ExcelReader中共用部分的对象和方法 * Excel基础类用于抽象ExcelWriter和ExcelReader中共用部分的对象和方法
* *
* @param <T> 子类类型用于返回this * @param <T> 子类类型用于返回this
* @author looly * @author looly
* @since 4.1.4 * @since 4.1.4
*/ */
public class ExcelBase<T extends ExcelBase<T>> implements Closeable { public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** 是否被关闭 */ /**
* 是否被关闭
*/
protected boolean isClosed; protected boolean isClosed;
/** 工作簿 */ /**
* 工作簿
*/
protected Workbook workbook; protected Workbook workbook;
/** Excel中对应的Sheet */ /**
* Excel中对应的Sheet
*/
protected Sheet sheet; protected Sheet sheet;
/** /**
* 构造 * 构造
* *
* @param sheet Excel中的sheet * @param sheet Excel中的sheet
*/ */
public ExcelBase(Sheet sheet) { public ExcelBase(Sheet sheet) {
@ -44,7 +51,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取Workbook * 获取Workbook
* *
* @return Workbook * @return Workbook
*/ */
public Workbook getWorkbook() { public Workbook getWorkbook() {
@ -53,7 +60,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 返回工作簿表格数 * 返回工作簿表格数
* *
* @return 工作簿表格数 * @return 工作簿表格数
* @since 4.0.10 * @since 4.0.10
*/ */
@ -63,7 +70,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取此工作簿所有Sheet表 * 获取此工作簿所有Sheet表
* *
* @return sheet表列表 * @return sheet表列表
* @since 4.0.3 * @since 4.0.3
*/ */
@ -78,7 +85,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取表名列表 * 获取表名列表
* *
* @return 表名列表 * @return 表名列表
* @since 4.0.3 * @since 4.0.3
*/ */
@ -93,7 +100,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取当前Sheet * 获取当前Sheet
* *
* @return {@link Sheet} * @return {@link Sheet}
*/ */
public Sheet getSheet() { public Sheet getSheet() {
@ -103,7 +110,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 自定义需要读取或写出的Sheet如果给定的sheet不存在创建之<br> * 自定义需要读取或写出的Sheet如果给定的sheet不存在创建之<br>
* 在读取中此方法用于切换读取的sheet在写出时此方法用于新建或者切换sheet * 在读取中此方法用于切换读取的sheet在写出时此方法用于新建或者切换sheet
* *
* @param sheetName sheet名 * @param sheetName sheet名
* @return this * @return this
* @since 4.0.10 * @since 4.0.10
@ -120,7 +127,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 自定义需要读取或写出的Sheet如果给定的sheet不存在创建之命名为默认<br> * 自定义需要读取或写出的Sheet如果给定的sheet不存在创建之命名为默认<br>
* 在读取中此方法用于切换读取的sheet在写出时此方法用于新建或者切换sheet * 在读取中此方法用于切换读取的sheet在写出时此方法用于新建或者切换sheet
* *
* @param sheetIndex sheet序号从0开始计数 * @param sheetIndex sheet序号从0开始计数
* @return this * @return this
* @since 4.0.10 * @since 4.0.10
@ -140,7 +147,19 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取指定坐标单元格单元格不存在时返回<code>null</code> * 获取指定坐标单元格单元格不存在时返回<code>null</code>
* *
* @param locationRef 单元格地址标识符例如A11B5
* @return {@link Cell}
* @since 5.1.4
*/
public Cell getCell(String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return getCell(cellLocation.getX(), cellLocation.getY());
}
/**
* 获取指定坐标单元格单元格不存在时返回<code>null</code>
*
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link Cell} * @return {@link Cell}
@ -152,7 +171,19 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取或创建指定坐标单元格 * 获取或创建指定坐标单元格
* *
* @param locationRef 单元格地址标识符例如A11B5
* @return {@link Cell}
* @since 5.1.4
*/
public Cell getOrCreateCell(String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return getOrCreateCell(cellLocation.getX(), cellLocation.getY());
}
/**
* 获取或创建指定坐标单元格
*
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link Cell} * @return {@link Cell}
@ -164,9 +195,22 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取指定坐标单元格如果isCreateIfNotExist为false则在单元格不存在时返回<code>null</code> * 获取指定坐标单元格如果isCreateIfNotExist为false则在单元格不存在时返回<code>null</code>
* *
* @param x X坐标从0计数即列号 * @param locationRef 单元格地址标识符例如A11B5
* @param y Y坐标从0计数即行号 * @param isCreateIfNotExist 单元格不存在时是否创建
* @return {@link Cell}
* @since 5.1.4
*/
public Cell getCell(String locationRef, boolean isCreateIfNotExist) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return getCell(cellLocation.getX(), cellLocation.getY(), isCreateIfNotExist);
}
/**
* 获取指定坐标单元格如果isCreateIfNotExist为false则在单元格不存在时返回<code>null</code>
*
* @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号
* @param isCreateIfNotExist 单元格不存在时是否创建 * @param isCreateIfNotExist 单元格不存在时是否创建
* @return {@link Cell} * @return {@link Cell}
* @since 4.0.6 * @since 4.0.6
@ -179,9 +223,10 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return null; return null;
} }
/** /**
* 获取或者创建行 * 获取或者创建行
* *
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link Row} * @return {@link Row}
* @since 4.1.4 * @since 4.1.4
@ -192,7 +237,19 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 为指定单元格获取或者创建样式返回样式后可以设置样式内容 * 为指定单元格获取或者创建样式返回样式后可以设置样式内容
* *
* @param locationRef 单元格地址标识符例如A11B5
* @return {@link CellStyle}
* @since 5.1.4
*/
public CellStyle getOrCreateCellStyle(String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return getOrCreateCellStyle(cellLocation.getX(), cellLocation.getY());
}
/**
* 为指定单元格获取或者创建样式返回样式后可以设置样式内容
*
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link CellStyle} * @return {@link CellStyle}
@ -202,10 +259,22 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
final CellStyle cellStyle = getOrCreateCell(x, y).getCellStyle(); final CellStyle cellStyle = getOrCreateCell(x, y).getCellStyle();
return StyleUtil.isNullOrDefaultStyle(this.workbook, cellStyle) ? createCellStyle(x, y) : cellStyle; return StyleUtil.isNullOrDefaultStyle(this.workbook, cellStyle) ? createCellStyle(x, y) : cellStyle;
} }
/** /**
* 为指定单元格创建样式返回样式后可以设置样式内容 * 为指定单元格创建样式返回样式后可以设置样式内容
* *
* @param locationRef 单元格地址标识符例如A11B5
* @return {@link CellStyle}
* @since 5.1.4
*/
public CellStyle createCellStyle(String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return createCellStyle(cellLocation.getX(), cellLocation.getY());
}
/**
* 为指定单元格创建样式返回样式后可以设置样式内容
*
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link CellStyle} * @return {@link CellStyle}
@ -221,7 +290,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取或创建某一行的样式返回样式后可以设置样式内容<br> * 获取或创建某一行的样式返回样式后可以设置样式内容<br>
* 需要注意此方法返回行样式设置背景色在单元格设置值后会被覆盖需要单独设置其单元格的样式 * 需要注意此方法返回行样式设置背景色在单元格设置值后会被覆盖需要单独设置其单元格的样式
* *
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link CellStyle} * @return {@link CellStyle}
* @since 4.1.4 * @since 4.1.4
@ -230,10 +299,10 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
CellStyle rowStyle = getOrCreateRow(y).getRowStyle(); CellStyle rowStyle = getOrCreateRow(y).getRowStyle();
return StyleUtil.isNullOrDefaultStyle(this.workbook, rowStyle) ? createRowStyle(y) : rowStyle; return StyleUtil.isNullOrDefaultStyle(this.workbook, rowStyle) ? createRowStyle(y) : rowStyle;
} }
/** /**
* 创建某一行的样式返回样式后可以设置样式内容 * 创建某一行的样式返回样式后可以设置样式内容
* *
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @return {@link CellStyle} * @return {@link CellStyle}
* @since 4.6.3 * @since 4.6.3
@ -247,7 +316,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取或创建某一行的样式返回样式后可以设置样式内容<br> * 获取或创建某一行的样式返回样式后可以设置样式内容<br>
* 需要注意此方法返回行样式设置背景色在单元格设置值后会被覆盖需要单独设置其单元格的样式 * 需要注意此方法返回行样式设置背景色在单元格设置值后会被覆盖需要单独设置其单元格的样式
* *
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @return {@link CellStyle} * @return {@link CellStyle}
* @since 4.1.4 * @since 4.1.4
@ -256,10 +325,10 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
final CellStyle columnStyle = this.sheet.getColumnStyle(x); final CellStyle columnStyle = this.sheet.getColumnStyle(x);
return StyleUtil.isNullOrDefaultStyle(this.workbook, columnStyle) ? createColumnStyle(x) : columnStyle; return StyleUtil.isNullOrDefaultStyle(this.workbook, columnStyle) ? createColumnStyle(x) : columnStyle;
} }
/** /**
* 创建某一行的样式返回样式后可以设置样式内容 * 创建某一行的样式返回样式后可以设置样式内容
* *
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @return {@link CellStyle} * @return {@link CellStyle}
* @since 4.6.3 * @since 4.6.3
@ -272,11 +341,11 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取总行数计算方法为 * 获取总行数计算方法为
* *
* <pre> * <pre>
* 最后一行序号 + 1 * 最后一行序号 + 1
* </pre> * </pre>
* *
* @return 行数 * @return 行数
* @since 4.5.4 * @since 4.5.4
*/ */
@ -286,53 +355,53 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** /**
* 获取有记录的行数计算方法为 * 获取有记录的行数计算方法为
* *
* <pre> * <pre>
* 最后一行序号 - 第一行序号 + 1 * 最后一行序号 - 第一行序号 + 1
* </pre> * </pre>
* *
* @return 行数 * @return 行数
* @since 4.5.4 * @since 4.5.4
*/ */
public int getPhysicalRowCount() { public int getPhysicalRowCount() {
return this.sheet.getPhysicalNumberOfRows(); return this.sheet.getPhysicalNumberOfRows();
} }
/** /**
* 获取第一行总列数计算方法为 * 获取第一行总列数计算方法为
* *
* <pre> * <pre>
* 最后一列序号 + 1 * 最后一列序号 + 1
* </pre> * </pre>
* *
* @return 列数 * @return 列数
*/ */
public int getColumnCount() { public int getColumnCount() {
return getColumnCount(0); return getColumnCount(0);
} }
/** /**
* 获取总列数计算方法为 * 获取总列数计算方法为
* *
* <pre> * <pre>
* 最后一列序号 + 1 * 最后一列序号 + 1
* </pre> * </pre>
* *
* @param rowNum 行号 * @param rowNum 行号
* @return 列数-1表示获取失败 * @return 列数-1表示获取失败
*/ */
public int getColumnCount(int rowNum) { public int getColumnCount(int rowNum) {
final Row row = this.sheet.getRow(rowNum); final Row row = this.sheet.getRow(rowNum);
if(null != row) { if (null != row) {
// getLastCellNum方法返回序号+1的值 // getLastCellNum方法返回序号+1的值
return row.getLastCellNum(); return row.getLastCellNum();
} }
return -1; return -1;
} }
/** /**
* 判断是否为xlsx格式的Excel表Excel07格式 * 判断是否为xlsx格式的Excel表Excel07格式
* *
* @return 是否为xlsx格式的Excel表Excel07格式 * @return 是否为xlsx格式的Excel表Excel07格式
* @since 4.6.2 * @since 4.6.2
*/ */

View File

@ -1,11 +1,5 @@
package cn.hutool.poi.excel; package cn.hutool.poi.excel;
import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import cn.hutool.core.exceptions.DependencyException; import cn.hutool.core.exceptions.DependencyException;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
@ -18,6 +12,11 @@ import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.Excel07SaxReader; import cn.hutool.poi.excel.sax.Excel07SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler; import cn.hutool.poi.excel.sax.handler.RowHandler;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
/** /**
* Excel工具类 * Excel工具类
* *
@ -593,7 +592,7 @@ public class ExcelUtil {
* 将Excel中地址标识符例如A11B5等转换为行列表示<br> * 将Excel中地址标识符例如A11B5等转换为行列表示<br>
* 例如A11 - x:0,y:10B5-x:1,y:4 * 例如A11 - x:0,y:10B5-x:1,y:4
* *
* @param locationRef 单元格地址标识符 * @param locationRef 单元格地址标识符例如A11B5
* @return 坐标点x表示行从0开始y表示列从0开始 * @return 坐标点x表示行从0开始y表示列从0开始
* @since 5.1.4 * @since 5.1.4
*/ */

View File

@ -12,6 +12,7 @@ import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import cn.hutool.poi.excel.cell.CellLocation;
import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
@ -860,6 +861,19 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this; return this;
} }
/**
* 给指定单元格赋值使用默认单元格样式
*
* @param locationRef 单元格地址标识符例如A11B5
* @param value
* @return this
* @since 5.1.4
*/
public ExcelWriter writeCellValue(String locationRef, Object value) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return writeCellValue(cellLocation.getX(), cellLocation.getY(), value);
}
/** /**
* 给指定单元格赋值使用默认单元格样式 * 给指定单元格赋值使用默认单元格样式
* *
@ -886,10 +900,25 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
*/ */
@Deprecated @Deprecated
public CellStyle createStyleForCell(int x, int y) { public CellStyle createStyleForCell(int x, int y) {
final Cell cell = getOrCreateCell(x, y); return createCellStyle(x, y);
final CellStyle cellStyle = this.workbook.createCellStyle(); }
cell.setCellStyle(cellStyle);
return cellStyle; /**
* 设置某个单元格的样式<br>
* 此方法用于多个单元格共享样式的情况<br>
* 可以调用{@link #getOrCreateCellStyle(int, int)} 方法创建或取得一个样式对象
*
* <p>
* 需要注意的是共享样式会共享同一个{@link CellStyle}一个单元格样式改变全部改变
*
* @param style 单元格样式
* @param locationRef 单元格地址标识符例如A11B5
* @return this
* @since 5.1.4
*/
public ExcelWriter setStyle(CellStyle style, String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return setStyle(style, cellLocation.getX(), cellLocation.getY());
} }
/** /**