Skip to content

Commit

Permalink
adding ReportContentIOKit
Browse files Browse the repository at this point in the history
  • Loading branch information
itsoulltd committed Aug 9, 2022
1 parent 49941af commit b520155
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ReportContentIOKit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>web-component-kit</artifactId>
<groupId>com.infoworks.lab</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>report-content-io-kit</artifactId>
<packaging>jar</packaging>
<version>1.10.7-RELEASE</version>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>[2.9.10.5,)</jackson.version>
<javax.json>1.1.4</javax.json>
<poi-version>4.1.2</poi-version>
</properties>

<dependencies>
<dependency>
<groupId>com.github.itsoulltd</groupId>
<artifactId>JSQLEditor</artifactId>
<version>1.1.4.2-RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.infoworks.lab</groupId>
<artifactId>http-rest-client</artifactId>
<version>1.10.7-RELEASE</version>
<scope>provided</scope>
</dependency>
<!--Apache POI-excel-parser -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-version}</version>
</dependency>
<!-- Apache POI Streaming api -->
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>2.1.0</version>
</dependency>
<!-- -->
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.infoworks.lab.definition;

import java.util.Map;

public interface ContentWriter<T> extends AutoCloseable{
default void write(Map<Integer, T> data, boolean skipZeroIndex) { write("", data, skipZeroIndex); }
void write(String sheetName, Map<Integer, T> data, boolean skipZeroIndex);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.infoworks.lab.definition;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public interface ReadingService {

void readAsync(InputStream inputStream
, Integer bufferSize
, Integer sheetAt
, Integer beginIndex
, Integer endIndex
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException;

void readAsync(File file
, Integer bufferSize
, Integer sheetAt
, Integer beginIndex
, Integer endIndex
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException;

void read(InputStream inputStream
, Integer sheetAt
, Integer startAt
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException;

void read(File file
, Integer sheetAt
, Integer startAt
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException;

Map<Integer, List<String>> read(InputStream inputStream, Integer sheetAt, Integer start, Integer end) throws IOException;

Map<Integer, List<String>> read(File file, Integer sheetAt, Integer start, Integer end) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.infoworks.lab.definition;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public interface WritingService {
void write(OutputStream outputStream, String sheetName, Map<Integer, List<String>> data) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package com.infoworks.lab.services.excel;

import com.infoworks.lab.definition.ReadingService;
import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.NumberToTextConverter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ExcelReadingService implements ReadingService {

private static Logger LOG = Logger.getLogger(ExcelReadingService.class.getSimpleName());

public void readAsync(InputStream inputStream
, Integer bufferSize
, Integer sheetAt
, Integer beginIndex
, Integer endIndex
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Workbook workbook = StreamingReader.builder()
.rowCacheSize(pageSize)
.bufferSize(bufferSize)
.open(inputStream);
configureWorkbook(workbook);
readBuffered(workbook, sheetAt, beginIndex, endIndex, pageSize, consumer);
workbook.close();
}

public void readAsync(File file
, Integer bufferSize
, Integer sheetAt
, Integer beginIndex
, Integer endIndex
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Workbook workbook = StreamingReader.builder()
.rowCacheSize(pageSize)
.bufferSize(bufferSize)
.open(file);
configureWorkbook(workbook);
readBuffered(workbook, sheetAt, beginIndex, endIndex, pageSize, consumer);
workbook.close();
}

/**
*
* @param workbook
* @param sheetAt
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
* @param pageSize
* @param consumer
* @throws IOException
*/
private void readBuffered(Workbook workbook
, Integer sheetAt
, Integer beginIndex
, Integer endIndex
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Sheet sheet = workbook.getSheetAt(sheetAt);
int maxCount = sheet.getLastRowNum() + 1;
pageSize = (pageSize > maxCount) ? maxCount : pageSize;
if (endIndex <= 0 || endIndex == Integer.MAX_VALUE) endIndex = maxCount;
//
int idx = -1;
Map<Integer, List<String>> data = new HashMap<>();
for (Row row : sheet){
if (++idx < beginIndex) {continue;}
if (idx >= endIndex) {break;}
//
data.put(idx, new ArrayList<>());
for (Cell cell : row){
addInto(data, idx, cell);
}
if (consumer != null && data.size() == pageSize ){
Map xData = new HashMap(data);
data.clear();
consumer.accept(xData);
}
}
//left-over
if (consumer != null && data.size() > 0 ){
Map xData = new HashMap(data);
data.clear();
consumer.accept(xData);
}
}

public void read(InputStream inputStream
, Integer sheetAt
, Integer startAt
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Workbook workbook = WorkbookFactory.create(inputStream);
configureWorkbook(workbook);
readAsync(workbook, sheetAt, startAt, pageSize, consumer);
workbook.close();
}

public void read(File file
, Integer sheetAt
, Integer startAt
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Workbook workbook = WorkbookFactory.create(file);
configureWorkbook(workbook);
readAsync(workbook, sheetAt, startAt, pageSize, consumer);
workbook.close();
}

private void readAsync(Workbook workbook
, Integer sheetAt
, Integer startAt
, Integer pageSize
, Consumer<Map<Integer, List<String>>> consumer) throws IOException {
//
Sheet sheet = workbook.getSheetAt(sheetAt);
int maxCount = sheet.getLastRowNum() + 1;
int loopCount = (pageSize == maxCount) ? 1 : (maxCount / pageSize) + 1;
pageSize = (pageSize > maxCount) ? maxCount : pageSize;
int index = 0;
int start = (startAt < 0 || startAt >= maxCount) ? 0 : startAt;
while (index < loopCount){
int end = start + pageSize;
if (end >= maxCount) end = maxCount;
Map res = parseContent(workbook, sheetAt, start, end);
if (consumer != null && res.size() > 0){
consumer.accept(res);
}
//
start += pageSize;
index++;
}
}

public Map<Integer, List<String>> read(InputStream inputStream, Integer sheetAt, Integer start, Integer end) throws IOException {
Workbook workbook = WorkbookFactory.create(inputStream);
configureWorkbook(workbook);
Map res = parseContent(workbook, sheetAt, start, end);
workbook.close();
return res;
}

public Map<Integer, List<String>> readXls(InputStream inputStream, Integer sheetAt, Integer start, Integer end) throws IOException {
Workbook workbook = new HSSFWorkbook(inputStream);
configureWorkbook(workbook);
Map res = parseContent(workbook, sheetAt, start, end);
workbook.close();
return res;
}

public Map<Integer, List<String>> read(File file, Integer sheetAt, Integer start, Integer end) throws IOException {
Workbook workbook = WorkbookFactory.create(file);
configureWorkbook(workbook);
Map res = parseContent(workbook, sheetAt, start, end);
workbook.close();
return res;
}

private void configureWorkbook(Workbook workbook) {
if (workbook != null){
try {
//Add All kind of setting for workbook:
workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
}catch (UnsupportedOperationException e){
LOG.log(Level.WARNING, e.getMessage());
}catch (Exception e){
LOG.log(Level.WARNING, e.getMessage());
}
}
}

private Map<Integer, List<String>> parseContent(Workbook workbook, Integer sheetAt, Integer start, Integer end) throws IOException {
//DoTheMath:
Sheet sheet = workbook.getSheetAt(sheetAt);
Map<Integer, List<String>> data = new HashMap<>();
//
if (end <= 0 || end == Integer.MAX_VALUE){
end = sheet.getLastRowNum() + 1;
}
int idx = (start < 0) ? 0 : start;
while (idx < end) {
data.put(idx, new ArrayList<>());
for (Cell cell : sheet.getRow(idx)) {
addInto(data, idx, cell);
}
idx++;
}
return data;
}

private void addInto(Map<Integer, List<String>> data, int idx, Cell cell) {
switch (cell.getCellType()) {
case STRING:
data.get(idx).add(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
data.get(idx).add(cell.getDateCellValue() + "");
} else {
data.get(idx).add(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
break;
case BOOLEAN:
data.get(idx).add(cell.getBooleanCellValue() + "");
break;
case FORMULA:
data.get(idx).add(cell.getStringCellValue() + "");
break;
default:
data.get(idx).add(" ");
}
}

}
Loading

0 comments on commit b520155

Please sign in to comment.