Skip to content

Commit e61c090

Browse files
authored
Merge pull request #3 from spacious-team/develop
Релиз 2021.4
2 parents ef36d5d + 46fb389 commit e61c090

File tree

13 files changed

+230
-156
lines changed

13 files changed

+230
-156
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target/
2020
*.iml
2121
*.ipr
2222
!.idea/runConfigurations
23+
!.idea/codeStyles
2324
!.idea/copyright
2425

2526
### NetBeans ###

.idea/codeStyles/Project.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/copyright/GNU_AGPLv3.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
Предоставляет реализацию `Table Wrapper API` для удобного доступа к табличным данным, сохраненным в файлах формата
66
Microsoft Office Excel (xls) и [Office Open XML](https://ru.wikipedia.org/wiki/Office_Open_XML) (xlsx).
77

8-
Пример создания фабрики таблиц
8+
Пример создания таблиц с первого листа файла `1.xlsx`
99
```java
1010
Workbook book = new XSSFWorkbook(Files.newInputStream(Path.of("1.xlsx")));
1111
ReportPage reportPage = new ExcelSheet(book.getSheetAt(0));
12-
TableFactory tableFactory = new ExcelTableFactory();
1312

14-
Table table1 = tableFactory.create(reportPage, "Table 1 description", ...);
13+
Table table1 = reportPage.create("Table 1 description", ...);
1514
...
16-
Table tableN = tableFactory.create(reportPage, "Table N description", ...);
15+
Table tableN = reportPage.create("Table N description", ...);
1716
```
1817
Объекты `table`...`tableN` используются для удобного доступа к строкам и к значениям ячеек.
1918

@@ -24,6 +23,14 @@ Table tableN = tableFactory.create(reportPage, "Table N description", ...);
2423
например для Apache Maven проекта
2524
```xml
2625
<repositories>
26+
<repository>
27+
<id>central</id>
28+
<name>Central Repository</name>
29+
<url>https://repo.maven.apache.org/maven2</url>
30+
<snapshots>
31+
<enabled>false</enabled>
32+
</snapshots>
33+
</repository>
2734
<repository>
2835
<id>jitpack.io</id>
2936
<url>https://jitpack.io</url>

pom.xml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<groupId>org.spacious-team</groupId>
2626
<artifactId>table-wrapper-excel-impl</artifactId>
27-
<version>2020.12</version>
27+
<version>2021.4</version>
2828
<packaging>jar</packaging>
2929

3030
<name>Table Wrapper API Excel Implementation</name>
@@ -53,6 +53,14 @@
5353
</properties>
5454

5555
<repositories>
56+
<repository>
57+
<id>central</id>
58+
<name>Central Repository</name>
59+
<url>https://repo.maven.apache.org/maven2</url>
60+
<snapshots>
61+
<enabled>false</enabled>
62+
</snapshots>
63+
</repository>
5664
<repository>
5765
<id>jitpack.io</id>
5866
<url>https://jitpack.io</url>
@@ -63,7 +71,7 @@
6371
<dependency>
6472
<groupId>com.github.spacious-team</groupId>
6573
<artifactId>table-wrapper-api</artifactId>
66-
<version>2020.12</version>
74+
<version>2021.4</version>
6775
</dependency>
6876
<dependency>
6977
<groupId>org.apache.poi</groupId>
@@ -83,4 +91,22 @@
8391
<optional>true</optional>
8492
</dependency>
8593
</dependencies>
94+
95+
<build>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-source-plugin</artifactId>
100+
<version>3.2.1</version>
101+
<executions>
102+
<execution>
103+
<id>attach-sources</id>
104+
<goals>
105+
<goal>jar</goal>
106+
</goals>
107+
</execution>
108+
</executions>
109+
</plugin>
110+
</plugins>
111+
</build>
86112
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Table Wrapper Excel Impl
3+
* Copyright (C) 2021 Vitalii Ananev <an-vitek@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.excel;
20+
21+
import org.apache.poi.ss.usermodel.Cell;
22+
import org.apache.poi.ss.usermodel.FormulaError;
23+
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
24+
25+
import java.time.Instant;
26+
import java.time.LocalDateTime;
27+
28+
public class ExcelCellDataAccessObject implements CellDataAccessObject<Cell, ExcelTableRow> {
29+
public static final ExcelCellDataAccessObject INSTANCE = new ExcelCellDataAccessObject();
30+
31+
@Override
32+
public Cell getCell(ExcelTableRow row, Integer cellIndex) {
33+
return row.getRow().getCell(cellIndex);
34+
}
35+
36+
@Override
37+
public Object getValue(Cell cell) {
38+
return switch (cell.getCellType()) {
39+
case STRING -> cell.getStringCellValue();
40+
case NUMERIC -> cell.getNumericCellValue(); // return double
41+
case BLANK -> null;
42+
case BOOLEAN -> cell.getBooleanCellValue();
43+
case FORMULA -> getCachedFormulaValue(cell);
44+
case ERROR -> throw new RuntimeException("Ячейка содержит ошибку вычисления формулы: " +
45+
FormulaError.forInt(cell.getErrorCellValue()));
46+
case _NONE -> null;
47+
};
48+
}
49+
50+
@Override
51+
public Instant getInstantValue(Cell cell) {
52+
return cell.getDateCellValue().toInstant();
53+
}
54+
55+
@Override
56+
public LocalDateTime getLocalDateTimeValue(Cell cell) {
57+
return cell.getLocalDateTimeCellValue();
58+
}
59+
60+
private static Object getCachedFormulaValue(Cell cell) {
61+
return switch (cell.getCachedFormulaResultType()) {
62+
case BOOLEAN -> cell.getBooleanCellValue();
63+
case NUMERIC -> cell.getNumericCellValue();
64+
case STRING -> cell.getRichStringCellValue();
65+
case ERROR -> throw new RuntimeException("Ячейка не содержит кешированный результат формулы: " +
66+
FormulaError.forInt(cell.getErrorCellValue()));
67+
default -> null; //never should occur
68+
};
69+
}
70+
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelSheet.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
import lombok.Getter;
2222
import lombok.RequiredArgsConstructor;
23+
import org.apache.poi.ss.usermodel.Cell;
2324
import org.apache.poi.ss.usermodel.Row;
2425
import org.apache.poi.ss.usermodel.Sheet;
25-
import org.spacious_team.table_wrapper.api.ReportPage;
26+
import org.spacious_team.table_wrapper.api.AbstractReportPage;
2627
import org.spacious_team.table_wrapper.api.TableCellAddress;
27-
import org.spacious_team.table_wrapper.api.TableRow;
2828

2929
import java.util.function.BiPredicate;
3030

3131
@RequiredArgsConstructor
32-
public class ExcelSheet implements ReportPage {
32+
public class ExcelSheet extends AbstractReportPage<ExcelTableRow> {
3333

3434
@Getter
3535
private final Sheet sheet;
@@ -41,7 +41,7 @@ public TableCellAddress find(Object value, int startRow, int endRow, int startCo
4141
}
4242

4343
@Override
44-
public TableRow getRow(int i) {
44+
public ExcelTableRow getRow(int i) {
4545
Row row = sheet.getRow(i);
4646
return (row == null) ? null : new ExcelTableRow(row);
4747
}
@@ -50,4 +50,31 @@ public TableRow getRow(int i) {
5050
public int getLastRowNum() {
5151
return sheet.getLastRowNum();
5252
}
53+
54+
/**
55+
* @param startRow first row for check
56+
* @return index of first empty row or -1 if not found
57+
*/
58+
@Override
59+
public int findEmptyRow(int startRow) {
60+
int lastRowNum = startRow;
61+
LAST_ROW:
62+
for (int n = getLastRowNum(); lastRowNum <= n; lastRowNum++) {
63+
Row row = sheet.getRow(lastRowNum);
64+
if (row == null || row.getLastCellNum() == -1) {
65+
return lastRowNum; // all row cells blank
66+
}
67+
for (Cell cell : row) {
68+
Object value;
69+
if (!(cell == null
70+
|| ((value = ExcelCellDataAccessObject.INSTANCE.getValue(cell)) == null)
71+
|| (value instanceof String) && (value.toString().isEmpty()))) {
72+
// not empty
73+
continue LAST_ROW;
74+
}
75+
}
76+
return lastRowNum; // all row cells blank
77+
}
78+
return -1;
79+
}
5380
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelTable.java

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,36 @@
1818

1919
package org.spacious_team.table_wrapper.excel;
2020

21+
import lombok.AccessLevel;
22+
import lombok.Getter;
2123
import lombok.ToString;
22-
import org.apache.poi.ss.usermodel.Cell;
23-
import org.spacious_team.table_wrapper.api.*;
24-
25-
import java.math.BigDecimal;
26-
import java.time.LocalDateTime;
27-
import java.util.Date;
24+
import org.spacious_team.table_wrapper.api.AbstractReportPage;
25+
import org.spacious_team.table_wrapper.api.AbstractTable;
26+
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
27+
import org.spacious_team.table_wrapper.api.Table;
28+
import org.spacious_team.table_wrapper.api.TableCellRange;
29+
import org.spacious_team.table_wrapper.api.TableColumnDescription;
2830

2931
@ToString(callSuper = true)
30-
public class ExcelTable extends AbstractTable {
32+
public class ExcelTable extends AbstractTable<ExcelTableRow> {
33+
34+
@Getter(AccessLevel.PROTECTED)
35+
private final CellDataAccessObject<?, ExcelTableRow> cellDataAccessObject = ExcelCellDataAccessObject.INSTANCE;
3136

32-
ExcelTable(ReportPage reportPage,
37+
ExcelTable(AbstractReportPage<ExcelTableRow> reportPage,
3338
String tableName,
3439
TableCellRange tableRange,
3540
Class<? extends TableColumnDescription> headerDescription,
3641
int headersRowCount) {
3742
super(reportPage, tableName, tableRange, headerDescription, headersRowCount);
3843
}
3944

40-
@Override
41-
public Object getCellValue(TableRow row, TableColumnDescription columnDescription) {
42-
return ExcelTableHelper.getCellValue(getRawCell(row, columnDescription));
43-
}
44-
45-
@Override
46-
public int getIntCellValue(TableRow row, TableColumnDescription columnDescription) {
47-
return (int) getLongCellValue(row, columnDescription);
48-
}
49-
50-
@Override
51-
public long getLongCellValue(TableRow row, TableColumnDescription columnDescription) {
52-
return ExcelTableHelper.getLongCellValue(getRawCell(row, columnDescription));
53-
}
54-
55-
@Override
56-
public BigDecimal getCurrencyCellValue(TableRow row, TableColumnDescription columnDescription) {
57-
return ExcelTableHelper.getCurrencyCellValue(getRawCell(row, columnDescription));
45+
ExcelTable(AbstractTable<ExcelTableRow> table, int appendDataRowsToTop, int appendDataRowsToBottom) {
46+
super(table, appendDataRowsToTop, appendDataRowsToBottom);
5847
}
5948

6049
@Override
61-
public String getStringCellValue(TableRow row, TableColumnDescription columnDescription) {
62-
return ExcelTableHelper.getStringCellValue(getRawCell(row, columnDescription));
63-
}
64-
65-
public Date getDateCellValue(TableRow row, TableColumnDescription columnDescription) {
66-
return getRawCell(row, columnDescription).getDateCellValue();
67-
}
68-
69-
public LocalDateTime getLocalDateTimeCellValue(TableRow row, TableColumnDescription columnDescription) {
70-
return getRawCell(row, columnDescription).getLocalDateTimeCellValue();
71-
}
72-
73-
private Cell getRawCell(TableRow row, TableColumnDescription columnDescription) {
74-
return ((ExcelTableRow) row).getRow().getCell(columnIndices.get(columnDescription.getColumn()));
50+
public Table subTable(int topRows, int bottomRows) {
51+
return new ExcelTable(this, topRows, bottomRows);
7552
}
7653
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelTableCell.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,19 @@
1818

1919
package org.spacious_team.table_wrapper.excel;
2020

21-
import lombok.Getter;
22-
import lombok.RequiredArgsConstructor;
2321
import org.apache.poi.ss.usermodel.Cell;
24-
import org.spacious_team.table_wrapper.api.TableCell;
22+
import org.spacious_team.table_wrapper.api.AbstractTableCell;
2523

26-
@RequiredArgsConstructor
27-
public class ExcelTableCell implements TableCell {
24+
import static org.spacious_team.table_wrapper.excel.ExcelCellDataAccessObject.INSTANCE;
2825

29-
@Getter
30-
private final Cell cell;
26+
public class ExcelTableCell extends AbstractTableCell<Cell> {
3127

32-
@Override
33-
public int getColumnIndex() {
34-
return cell.getColumnIndex();
35-
}
36-
37-
@Override
38-
public Object getValue() {
39-
return ExcelTableHelper.getCellValue(cell);
28+
public ExcelTableCell(Cell cell) {
29+
super(cell, INSTANCE);
4030
}
4131

4232
@Override
43-
public String getStringCellValue() {
44-
return ExcelTableHelper.getStringCellValue(cell);
33+
public int getColumnIndex() {
34+
return getCell().getColumnIndex();
4535
}
4636
}

0 commit comments

Comments
 (0)