mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add toLocation method
This commit is contained in:
parent
545c20912a
commit
6dc12cf6fb
@ -1,5 +1,6 @@
|
||||
package cn.hutool.poi.excel;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
@ -9,8 +10,10 @@ import cn.hutool.core.exceptions.DependencyException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.poi.PoiChecker;
|
||||
import cn.hutool.poi.excel.cell.CellLocation;
|
||||
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
||||
import cn.hutool.poi.excel.sax.Excel07SaxReader;
|
||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||
@ -560,7 +563,7 @@ public class ExcelUtil {
|
||||
}
|
||||
int remainder = index % 26;
|
||||
colName.append((char) (remainder + 'A'));
|
||||
index = (int) ((index - remainder) / 26);
|
||||
index = (index - remainder) / 26;
|
||||
} while (index > 0);
|
||||
return colName.reverse().toString();
|
||||
}
|
||||
@ -585,4 +588,18 @@ public class ExcelUtil {
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Excel中地址标识符(例如A11,B5)等转换为行列表示<br>
|
||||
* 例如:A11 -》 x:0,y:10,B5-》x:1,y:4
|
||||
*
|
||||
* @param locationRef 单元格地址标识符
|
||||
* @return 坐标点,x表示行,从0开始,y表示列,从0开始
|
||||
* @since 5.1.4
|
||||
*/
|
||||
public static CellLocation toLocation(String locationRef){
|
||||
final int x = colNameToIndex(locationRef);
|
||||
final int y = ReUtil.getFirstNumber(locationRef) -1;
|
||||
return new CellLocation(x, y);
|
||||
}
|
||||
}
|
||||
|
@ -660,7 +660,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, boolean isSetHeaderStyle) {
|
||||
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
|
||||
|
||||
final CellStyle style = (isSetHeaderStyle && null != this.styleSet && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle;
|
||||
CellStyle style = null;
|
||||
if(null != this.styleSet){
|
||||
style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle;
|
||||
}
|
||||
CellUtil.mergingCells(this.sheet, firstRow, lastRow, firstColumn, lastColumn, style);
|
||||
|
||||
// 设置内容
|
||||
@ -1043,7 +1046,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
||||
Comparator<String> aliasComparator = this.aliasComparator;
|
||||
if (null == aliasComparator) {
|
||||
Set<String> keySet = this.headerAlias.keySet();
|
||||
aliasComparator = new IndexedComparator<>(keySet.toArray(new String[keySet.size()]));
|
||||
aliasComparator = new IndexedComparator<>(keySet.toArray(new String[0]));
|
||||
this.aliasComparator = aliasComparator;
|
||||
}
|
||||
return aliasComparator;
|
||||
|
@ -0,0 +1,69 @@
|
||||
package cn.hutool.poi.excel.cell;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 单元格位置
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.1.4
|
||||
*/
|
||||
public class CellLocation implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param x 列号,从0开始
|
||||
* @param y 行号,从0开始
|
||||
*/
|
||||
public CellLocation(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final CellLocation that = (CellLocation) o;
|
||||
return x == that.x && y == that.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CellLocation{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package cn.hutool.poi.excel.test;
|
||||
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.hutool.poi.excel.cell.CellLocation;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
|
||||
public class ExcelUtilTest {
|
||||
|
||||
@Test
|
||||
@ -36,4 +36,11 @@ public class ExcelUtilTest {
|
||||
Assert.assertEquals(1, ExcelUtil.colNameToIndex("B"));
|
||||
Assert.assertEquals(0, ExcelUtil.colNameToIndex("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLocationTest(){
|
||||
final CellLocation a11 = ExcelUtil.toLocation("A11");
|
||||
Assert.assertEquals(0, a11.getX());
|
||||
Assert.assertEquals(10, a11.getY());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user