From 6dc12cf6fb61c91f91e5f87bb7b133ee9e1b6a06 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 21 Feb 2020 12:05:52 +0800 Subject: [PATCH] add toLocation method --- .../java/cn/hutool/poi/excel/ExcelUtil.java | 19 ++++- .../java/cn/hutool/poi/excel/ExcelWriter.java | 7 +- .../hutool/poi/excel/cell/CellLocation.java | 69 +++++++++++++++++++ .../hutool/poi/excel/test/ExcelUtilTest.java | 11 ++- 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellLocation.java diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java index 01d845841..a2e5bdaeb 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java @@ -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)等转换为行列表示
+ * 例如: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); + } } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index d8d54c090..3c421acf3 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -660,7 +660,10 @@ public class ExcelWriter extends ExcelBase { 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 { Comparator aliasComparator = this.aliasComparator; if (null == aliasComparator) { Set 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; diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellLocation.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellLocation.java new file mode 100644 index 000000000..8fd3c30c9 --- /dev/null +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellLocation.java @@ -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 + + '}'; + } +} diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelUtilTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelUtilTest.java index 882f1289d..8d723f9bf 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelUtilTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelUtilTest.java @@ -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()); + } }