Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To install manually, please check the [releases](https://github.com/millij/poi-o

#### Dependencies

The current implementation uses **POI version 3.17**.
The current implementation uses **POI version 4.0.1**.


## Usage
Expand Down
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

group = 'io.github.millij'
version = '1.0.0'
version = '2.0.0-SNAPSHOT'


dependencies {
Expand All @@ -27,7 +27,8 @@ dependencies {
compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'

// Apache POI
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
compile group: 'org.apache.poi', name: 'poi', version: '4.0.1'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'


// Test compile
Expand All @@ -51,8 +52,8 @@ test {
// ----------------------------------------------------------------------------

tasks.withType(JavaCompile) {
sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

task javadocJar(type: Jar) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/io/github/millij/poi/ss/reader/XlsxReader.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.github.millij.poi.ss.reader;

import static io.github.millij.poi.util.Beans.isInstantiableType;
import io.github.millij.poi.SpreadsheetReadException;
import io.github.millij.poi.ss.handler.RowContentsHandler;
import io.github.millij.poi.ss.handler.RowListener;

import java.io.InputStream;

import org.apache.poi.ooxml.util.SAXHelper;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.SAXHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
Expand All @@ -21,6 +18,10 @@
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import io.github.millij.poi.SpreadsheetReadException;
import io.github.millij.poi.ss.handler.RowContentsHandler;
import io.github.millij.poi.ss.handler.RowListener;


/**
* Reader impletementation of {@link Workbook} for an OOXML .xlsx file. This implementation is
Expand Down
204 changes: 108 additions & 96 deletions src/main/java/io/github/millij/poi/ss/writer/SpreadsheetWriter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.github.millij.poi.ss.writer;

import io.github.millij.poi.ss.model.annotations.Sheet;
import io.github.millij.poi.util.Spreadsheet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -23,6 +25,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.millij.poi.ss.model.annotations.Sheet;
import io.github.millij.poi.util.Spreadsheet;

@Deprecated
public class SpreadsheetWriter {
Expand All @@ -32,145 +36,153 @@ public class SpreadsheetWriter {
private final XSSFWorkbook workbook;
private final OutputStream outputStrem;


// Constructors
// ------------------------------------------------------------------------

public SpreadsheetWriter(String filepath) throws FileNotFoundException {
this(new File(filepath));
this(new File(filepath));
}

public SpreadsheetWriter(File file) throws FileNotFoundException {
this(new FileOutputStream(file));
this(new FileOutputStream(file));
}

public SpreadsheetWriter(OutputStream outputStream) {
super();
super();

this.workbook = new XSSFWorkbook();
this.outputStrem = outputStream;
this.workbook = new XSSFWorkbook();
this.outputStrem = outputStream;
}


// Methods
// ------------------------------------------------------------------------


// Sheet :: Add

public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects) {
// Sheet Headers
List<String> headers = Spreadsheet.getColumnNames(beanType);
// Sheet Headers
Map<String, String> headerMap = Spreadsheet.getPropertyToColumnNameMap(beanType);

this.addSheet(beanType, rowObjects, headerMap);
}

this.addSheet(beanType, rowObjects, headers);
public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, Map<String, String> headerMap) {
// SheetName
Sheet sheet = beanType.getAnnotation(Sheet.class);
String sheetName = sheet != null ? sheet.value() : null;

this.addSheet(beanType, rowObjects, headerMap, sheetName);
}

public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, List<String> headers) {
// SheetName
Sheet sheet = beanType.getAnnotation(Sheet.class);
String sheetName = sheet != null ? sheet.value() : null;
public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, Set<String> headers) {
// SheetName
Sheet sheet = beanType.getAnnotation(Sheet.class);
String sheetName = sheet != null ? sheet.value() : null;

// Sheet Headers
Map<String, String> headerMap = Spreadsheet.getPropertyToColumnNameMap(beanType);
Map<String, String> headerMapCopy = headerMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
headerMapCopy.values().retainAll(headers);

this.addSheet(beanType, rowObjects, headers, sheetName);
this.addSheet(beanType, rowObjects, Collections.unmodifiableMap(headerMapCopy), sheetName);
}

public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, String sheetName) {
// Sheet Headers
List<String> headers = Spreadsheet.getColumnNames(beanType);
Map<String, String> headerMap = Spreadsheet.getPropertyToColumnNameMap(beanType);

this.addSheet(beanType, rowObjects, headers, sheetName);
this.addSheet(beanType, rowObjects, headerMap, sheetName);
}

public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, List<String> headers,
String sheetName) {
// Sanity checks
if (beanType == null) {
throw new IllegalArgumentException("GenericExcelWriter :: ExcelBean type should not be null");
}

if (CollectionUtils.isEmpty(rowObjects)) {
LOGGER.error("Skipping excel sheet writing as the ExcelBean collection is empty");
return;
}

if (CollectionUtils.isEmpty(headers)) {
LOGGER.error("Skipping excel sheet writing as the headers collection is empty");
return;
}

try {
XSSFSheet exSheet = workbook.getSheet(sheetName);
if (exSheet != null) {
String errMsg = String.format("A Sheet with the passed name already exists : %s", sheetName);
throw new IllegalArgumentException(errMsg);
}

XSSFSheet sheet = StringUtils.isEmpty(sheetName) ? workbook.createSheet() : workbook.createSheet(sheetName);
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName());

// Header
XSSFRow headerRow = sheet.createRow(0);
for (int i = 0; i < headers.size(); i++) {
XSSFCell cell = headerRow.createCell(i);
cell.setCellValue(headers.get(i));
}

// Data Rows
Map<String, List<String>> rowsData = this.prepareSheetRowsData(headers, rowObjects);
for (int i = 0, rowNum = 1; i < rowObjects.size(); i++, rowNum++) {
final XSSFRow row = sheet.createRow(rowNum);

int cellNo = 0;
for (String key : rowsData.keySet()) {
Cell cell = row.createCell(cellNo);
String value = rowsData.get(key).get(i);
cell.setCellValue(value);
cellNo++;
}
}

} catch (Exception ex) {
String errMsg = String.format("Error while preparing sheet with passed row objects : %s", ex.getMessage());
LOGGER.error(errMsg, ex);
}
public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, Map<String, String> headerMap,
String sheetName) {
// Sanity checks
if (beanType == null) {
throw new IllegalArgumentException("GenericExcelWriter :: ExcelBean type should not be null");
}

if (CollectionUtils.isEmpty(rowObjects)) {
LOGGER.error("Skipping excel sheet writing as the ExcelBean collection is empty");
return;
}

if (headerMap == null | headerMap.isEmpty()) {
LOGGER.error("Skipping excel sheet writing as the headers collection is empty");
return;
}

try {
XSSFSheet exSheet = workbook.getSheet(sheetName);
if (exSheet != null) {
String errMsg = String.format("A Sheet with the passed name already exists : %s", sheetName);
throw new IllegalArgumentException(errMsg);
}

XSSFSheet sheet = StringUtils.isEmpty(sheetName) ? workbook.createSheet() : workbook.createSheet(sheetName);
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName());

// Header
XSSFRow headerRow = sheet.createRow(0);
Iterator<Entry<String, String>> iterator = headerMap.entrySet().iterator();
for (int i = 0; iterator.hasNext(); i++) {
XSSFCell cell = headerRow.createCell(i);
cell.setCellValue(headerMap.get(iterator.next().getKey()));
}

// Data Rows
Map<String, List<String>> rowsData = this.prepareSheetRowsData(headerMap, rowObjects);
for (int i = 0, rowNum = 1; i < rowObjects.size(); i++, rowNum++) {
final XSSFRow row = sheet.createRow(rowNum);

int cellNo = 0;
for (String key : rowsData.keySet()) {
Cell cell = row.createCell(cellNo);
String value = rowsData.get(key).get(i);
cell.setCellValue(value);
cellNo++;
}
}

} catch (Exception ex) {
String errMsg = String.format("Error while preparing sheet with passed row objects : %s", ex.getMessage());
LOGGER.error(errMsg, ex);
}
}


// Sheet :: Append to existing



// Write

public void write() throws IOException {
workbook.write(outputStrem);
workbook.close();
workbook.write(outputStrem);
workbook.close();
}


// Private Methods
// ------------------------------------------------------------------------

private <EB> Map<String, List<String>> prepareSheetRowsData(List<String> headers,
List<EB> rowObjects) throws Exception {
private <EB> Map<String, List<String>> prepareSheetRowsData(Map<String, String> headerMap, List<EB> rowObjects)
throws Exception {

final Map<String, List<String>> sheetData = new LinkedHashMap<String, List<String>>();
final Map<String, List<String>> sheetData = new LinkedHashMap<String, List<String>>();

// Iterate over Objects
for (EB excelBean : rowObjects) {
Map<String, String> row = Spreadsheet.asRowDataMap(excelBean, headers);
// Iterate over Objects
for (EB excelBean : rowObjects) {
Map<String, String> row = Spreadsheet.asRowDataMap(excelBean, headerMap);

for (String header : headers) {
List<String> data = sheetData.containsKey(header) ? sheetData.get(header) : new ArrayList<String>();
String value = row.get(header) != null ? row.get(header) : "";
data.add(value);
for (String header : headerMap.values()) {
List<String> data = sheetData.containsKey(header) ? sheetData.get(header) : new ArrayList<String>();
String value = row.get(header) != null ? row.get(header) : "";
data.add(value);

sheetData.put(header, data);
}
}
sheetData.put(header, data);
}
}

return sheetData;
return sheetData;
}



}
Loading