添加writeCellValue的重载,以支持isHeader

This commit is contained in:
Looly 2023-05-26 20:39:33 +08:00
parent dd3ea66a95
commit 2e01dd0183
4 changed files with 134 additions and 49 deletions

View File

@ -13,13 +13,12 @@
package org.dromara.hutool.poi.excel;
import org.dromara.hutool.core.exception.DependencyException;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.poi.PoiChecker;
import org.dromara.hutool.poi.excel.cell.CellLocation;
import org.dromara.hutool.poi.excel.cell.CellLocationUtil;
import org.dromara.hutool.poi.excel.sax.ExcelSaxReader;
import org.dromara.hutool.poi.excel.sax.ExcelSaxUtil;
import org.dromara.hutool.poi.excel.sax.handler.RowHandler;
@ -464,19 +463,7 @@ public class ExcelUtil {
* @since 4.1.20
*/
public static String indexToColName(int index) {
if (index < 0) {
return null;
}
final StringBuilder colName = StrUtil.builder();
do {
if (colName.length() > 0) {
index--;
}
final int remainder = index % 26;
colName.append((char) (remainder + 'A'));
index = (index - remainder) / 26;
} while (index > 0);
return colName.reverse().toString();
return CellLocationUtil.indexToColName(index);
}
/**
@ -487,17 +474,7 @@ public class ExcelUtil {
* @since 4.1.20
*/
public static int colNameToIndex(final String colName) {
final int length = colName.length();
char c;
int index = -1;
for (int i = 0; i < length; i++) {
c = Character.toUpperCase(colName.charAt(i));
if (Character.isDigit(c)) {
break;// 确定指定的char值是否为数字
}
index = (index + 1) * 26 + (int) c - 'A';
}
return index;
return CellLocationUtil.colNameToIndex(colName);
}
/**
@ -509,8 +486,6 @@ public class ExcelUtil {
* @since 5.1.4
*/
public static CellLocation toLocation(final String locationRef) {
final int x = colNameToIndex(locationRef);
final int y = ReUtil.getFirstNumber(locationRef) - 1;
return new CellLocation(x, y);
return CellLocationUtil.toLocation(locationRef);
}
}

View File

@ -1042,7 +1042,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} else {
rowMap = (Map) rowBean;
}
}else if(rowBean instanceof Iterable){
} else if (rowBean instanceof Iterable) {
// issue#2398@Github
// MapWrapper由于实现了Iterable接口应该优先按照Map处理
return writeRow((Iterable<?>) rowBean);
@ -1097,7 +1097,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
for (final Table.Cell<?, ?, ?> cell : aliasTable) {
// 首先查找原名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getRowKey()));
if(null == location){
if (null == location) {
// 未找到则查找别名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getColumnKey()));
}
@ -1149,8 +1149,21 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @since 4.0.2
*/
public ExcelWriter writeCellValue(final int x, final int y, final Object value) {
return writeCellValue(x, y, value, false);
}
/**
* 给指定单元格赋值使用默认单元格样式
*
* @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号
* @param isHeader 是否为Header
* @param value
* @return this
*/
public ExcelWriter writeCellValue(final int x, final int y, final Object value, final boolean isHeader) {
final Cell cell = getOrCreateCell(x, y);
CellUtil.setCellValue(cell, value, this.styleSet, false, this.cellEditor);
CellUtil.setCellValue(cell, value, this.styleSet, isHeader, this.cellEditor);
return this;
}
@ -1375,9 +1388,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
private Table<?, ?, ?> aliasTable(final Map<?, ?> rowMap) {
final Table<Object, Object, Object> filteredTable = new RowKeyTable<>(new LinkedHashMap<>(), TableMap::new);
if (MapUtil.isEmpty(this.headerAlias)) {
rowMap.forEach((key, value)-> filteredTable.put(key, key, value));
}else{
rowMap.forEach((key, value)->{
rowMap.forEach((key, value) -> filteredTable.put(key, key, value));
} else {
rowMap.forEach((key, value) -> {
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
if (null != aliasName) {
// 别名键值对加入

View File

@ -38,18 +38,36 @@ public class CellLocation implements Serializable {
this.y = y;
}
/**
* 获取x
*
* @return x
*/
public int getX() {
return x;
}
/**
* 设置x
*
* @param x x
*/
public void setX(final int x) {
this.x = x;
}
/**
* 获取y
* @return y
*/
public int getY() {
return y;
}
/**
* 设置y
* @param y y
*/
public void setY(final int y) {
this.y = y;
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2023 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:
* http://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;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
/**
* 单元格位置工具类提供包括行号转行名称列号转列名称等功能
*
* @author looly
* @since 6.0.0
*/
public class CellLocationUtil {
/**
* 将Sheet列号变为列名
*
* @param index 列号, 从0开始
* @return 0-A; 1-B...26-AA
*/
public static String indexToColName(int index) {
if (index < 0) {
return null;
}
final StringBuilder colName = StrUtil.builder();
do {
if (colName.length() > 0) {
index--;
}
final int remainder = index % 26;
colName.append((char) (remainder + 'A'));
index = (index - remainder) / 26;
} while (index > 0);
return colName.reverse().toString();
}
/**
* 根据表元的列名转换为列号
*
* @param colName 列名, 从A开始
* @return A1-0; B1-1...AA1-26
*/
public static int colNameToIndex(final String colName) {
final int length = colName.length();
char c;
int index = -1;
for (int i = 0; i < length; i++) {
c = Character.toUpperCase(colName.charAt(i));
if (Character.isDigit(c)) {
break;// 确定指定的char值是否为数字
}
index = (index + 1) * 26 + (int) c - 'A';
}
return index;
}
/**
* 将Excel中地址标识符例如A11B5等转换为行列表示<br>
* 例如A11 - x:0,y:10B5-x:1,y:4
*
* @param locationRef 单元格地址标识符例如A11B5
* @return 坐标点x表示行从0开始y表示列从0开始
*/
public static CellLocation toLocation(final String locationRef) {
final int x = colNameToIndex(locationRef);
final int y = ReUtil.getFirstNumber(locationRef) - 1;
return new CellLocation(x, y);
}
}