mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
添加writeCellValue的重载,以支持isHeader
This commit is contained in:
parent
dd3ea66a95
commit
2e01dd0183
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -919,7 +919,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
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);
|
||||
dy1, dx2, dy2, col1, row1, col2, row2);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -927,15 +927,15 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
* 添加图片到当前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 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开始
|
||||
* @return this
|
||||
* @author vhukze
|
||||
* @since 5.8.0
|
||||
@ -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) {
|
||||
// 别名键值对加入
|
||||
|
@ -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;
|
||||
}
|
||||
@ -74,8 +92,8 @@ public class CellLocation implements Serializable {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CellLocation{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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中地址标识符(例如A11,B5)等转换为行列表示<br>
|
||||
* 例如:A11 -》 x:0,y:10,B5-》x:1,y:4
|
||||
*
|
||||
* @param locationRef 单元格地址标识符,例如A11,B5
|
||||
* @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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user