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
### 新特性
* 【poi 】 增加单元格位置引用例如A11等方式获取单元格
### Bug修复
* 【core 】 修复CombinationAnnotationElement数组判断问题issue#752@Github

View File

@ -4,6 +4,7 @@ import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.poi.excel.cell.CellLocation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
@ -24,11 +25,17 @@ import cn.hutool.poi.excel.style.StyleUtil;
* @since 4.1.4
*/
public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/** 是否被关闭 */
/**
* 是否被关闭
*/
protected boolean isClosed;
/** 工作簿 */
/**
* 工作簿
*/
protected Workbook workbook;
/** Excel中对应的Sheet */
/**
* Excel中对应的Sheet
*/
protected Sheet sheet;
/**
@ -138,6 +145,18 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return (T) this;
}
/**
* 获取指定坐标单元格单元格不存在时返回<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>
*
@ -150,6 +169,18 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return getCell(x, y, false);
}
/**
* 获取或创建指定坐标单元格
*
* @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());
}
/**
* 获取或创建指定坐标单元格
*
@ -165,8 +196,21 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
/**
* 获取指定坐标单元格如果isCreateIfNotExist为false则在单元格不存在时返回<code>null</code>
*
* @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号
* @param locationRef 单元格地址标识符例如A11B5
* @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 单元格不存在时是否创建
* @return {@link Cell}
* @since 4.0.6
@ -179,6 +223,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return null;
}
/**
* 获取或者创建行
*
@ -190,6 +235,18 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return RowUtil.getOrCreateRow(this.sheet, y);
}
/**
* 为指定单元格获取或者创建样式返回样式后可以设置样式内容
*
* @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());
}
/**
* 为指定单元格获取或者创建样式返回样式后可以设置样式内容
*
@ -203,6 +260,18 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
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());
}
/**
* 为指定单元格创建样式返回样式后可以设置样式内容
*
@ -323,7 +392,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
*/
public int getColumnCount(int rowNum) {
final Row row = this.sheet.getRow(rowNum);
if(null != row) {
if (null != row) {
// getLastCellNum方法返回序号+1的值
return row.getLastCellNum();
}

View File

@ -1,11 +1,5 @@
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.io.FileUtil;
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.handler.RowHandler;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Excel工具类
*
@ -593,7 +592,7 @@ public class ExcelUtil {
* 将Excel中地址标识符例如A11B5等转换为行列表示<br>
* 例如A11 - x:0,y:10B5-x:1,y:4
*
* @param locationRef 单元格地址标识符
* @param locationRef 单元格地址标识符例如A11B5
* @return 坐标点x表示行从0开始y表示列从0开始
* @since 5.1.4
*/

View File

@ -12,6 +12,7 @@ import java.util.Set;
import java.util.TreeMap;
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.HSSFDataValidation;
import org.apache.poi.ss.usermodel.Cell;
@ -860,6 +861,19 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
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
public CellStyle createStyleForCell(int x, int y) {
final Cell cell = getOrCreateCell(x, y);
final CellStyle cellStyle = this.workbook.createCellStyle();
cell.setCellStyle(cellStyle);
return cellStyle;
return createCellStyle(x, y);
}
/**
* 设置某个单元格的样式<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());
}
/**