From 24393dd2020124b4e2d714ca08ac508c07cf6d8a Mon Sep 17 00:00:00 2001 From: liyawei Date: Fri, 29 Jul 2022 18:25:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A4=E8=AF=81-=E4=B8=8A=E6=8A=A5=E5=BA=93-?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AF=BC=E5=87=BAexcel=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF-=E5=9B=BE=E7=89=87=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/project/util/CellReference.java | 112 ++++++++++++++++++ .../jero/modules/project/util/XlsxUtils.java | 56 +++++++++ 2 files changed, 168 insertions(+) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/CellReference.java create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/XlsxUtils.java diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/CellReference.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/CellReference.java new file mode 100644 index 000000000..fc67eb2d9 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/CellReference.java @@ -0,0 +1,112 @@ +/* + * Copyright 2013 Haulmont + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.jero.modules.project.util; + +import org.apache.commons.lang3.ObjectUtils; +import org.xlsx4j.sml.Cell; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CellReference implements Comparable { + public static final Pattern CELL_COORDINATES_PATTERN = Pattern.compile("([A-z]+)([0-9]+)"); + private int column; + private int row; + private String sheet; + + public CellReference(String sheet, int row, int column) { + this.column = column; + this.row = row; + this.sheet = sheet; + } + + public CellReference(String sheet, String cellRef) { + Matcher matcher = CELL_COORDINATES_PATTERN.matcher(cellRef); + if (matcher.find()) { + column = XlsxUtils.getNumberFromColumnReference(matcher.group(1)); + row = Integer.valueOf(matcher.group(2)); + this.sheet = sheet; + } else { + throw new RuntimeException(String.format("Wrong cell %s", cellRef)); + } + } + + public CellReference(String sheet, Cell cell) { + this(sheet, cell.getR()); + } + + public CellReference shift(int downShift, int rightShift) { + row += downShift; + column += rightShift; + return this; + } + + public CellReference move(int row, int col) { + this.row = row; + this.column = col; + return this; + } + + public String toReference() { + return XlsxUtils.getColumnReferenceFromNumber(getColumn()) + getRow(); + } + + public int getColumn() { + return column; + } + + public int getRow() { + return row; + } + + public String getSheet() { + return sheet; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + CellReference that = (CellReference) o; + + if (column != that.column) return false; + if (row != that.row) return false; + if (sheet != null ? !sheet.equals(that.sheet) : that.sheet != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = column; + result = 31 * result + row; + result = 31 * result + (sheet != null ? sheet.hashCode() : 0); + return result; + } + + @Override + public int compareTo(Object o) { + if (o instanceof CellReference) { + int rows = ObjectUtils.compare(row, ((CellReference) o).row); + int columns = ObjectUtils.compare(column, ((CellReference) o).column); + return rows != 0 ? rows : columns; + + } else { + throw new IllegalArgumentException("Could not compare with " + o); + } + } +} \ No newline at end of file diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/XlsxUtils.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/XlsxUtils.java new file mode 100644 index 000000000..49a72d3bf --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/XlsxUtils.java @@ -0,0 +1,56 @@ +/* + * Copyright 2013 Haulmont + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.jero.modules.project.util; + +public final class XlsxUtils { + + private static final long PX_PER_INCH = 96; + private static final long EMU_PER_INCH = 914400; + + private XlsxUtils() { + } + + public static int getNumberFromColumnReference(String columnReference) { + int sum = 0; + + for (int i = 0; i < columnReference.length(); i++) { + char c = columnReference.charAt(i); + int number = ((int) c) - 64 - 1; + + int pow = columnReference.length() - i - 1; + sum += Math.pow(26, pow) * (number + 1); + } + return sum; + } + + public static String getColumnReferenceFromNumber(int number) { + int remain = 0; + StringBuilder ref = new StringBuilder(); + do { + + remain = (number - 1) % 26; + number = (number - 1) / 26; + + ref.append((char) (remain + 64 + 1)); + } while (number > 0); + + return ref.reverse().toString(); + } + + public static long convertPxToEmu(long px) { + return px * EMU_PER_INCH / PX_PER_INCH; + } +}