forked from Appendium/flatpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bb9d02
commit 0363320
Showing
6 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>net.sf.flatpack</groupId> | ||
<artifactId>flatpack-parent</artifactId> | ||
<version>4.0.4-SNAPSHOT</version> | ||
</parent> | ||
|
||
|
||
<artifactId>flatpack-poi</artifactId> | ||
<packaging>bundle</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<name>FlatPack POI</name> | ||
<description>Use poi to write to XML</description> | ||
|
||
<properties> | ||
<org.apache.poi.version>3.17</org.apache.poi.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>net.sf.flatpack</groupId> | ||
<artifactId>flatpack</artifactId> | ||
<version>4.0.4-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.poi</groupId> | ||
<artifactId>poi-ooxml</artifactId> | ||
<version>${org.apache.poi.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>${slf4j.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
<version>3.2.0</version> | ||
<extensions>true</extensions> | ||
<configuration> | ||
<instructions> | ||
<Export-Package>net.sf.flatpack.poi.*;version="${project.version}"</Export-Package> | ||
<Private-Package/> | ||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> | ||
<Bundle-Version>${project.version}</Bundle-Version> | ||
<Bundle-RequiredExecutionEnvironment>J2SE-1.4</Bundle-RequiredExecutionEnvironment> | ||
</instructions> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Automatic-Module-Name>net.sf.flatpack.poi</Automatic-Module-Name> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>test-jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
88 changes: 88 additions & 0 deletions
88
flatpack-poi/src/main/java/net/sf/flatpack/poi/writer/PoiWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package net.sf.flatpack.poi.writer; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.poi.ss.usermodel.*; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
||
import net.sf.flatpack.writer.Writer; | ||
|
||
public class PoiWriter implements Writer { | ||
private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(PoiWriter.class); | ||
|
||
private OutputStream out; | ||
|
||
private CreationHelper createHelper; | ||
private Workbook workbook; | ||
private Sheet sheet; | ||
|
||
private List<String> columns = new ArrayList<>(); | ||
private Map<String, Object> currentRow = new HashMap<>(); | ||
|
||
|
||
public PoiWriter(OutputStream out) { | ||
this.out = out; | ||
|
||
workbook = new XSSFWorkbook(); | ||
createHelper = workbook.getCreationHelper(); | ||
sheet = workbook.createSheet("Sheet1"); | ||
} | ||
|
||
|
||
@Override | ||
public Writer printHeader() { | ||
// Print the HEADER columns if it is filled | ||
if (this.columns.isEmpty()) { | ||
LOGGER.debug("PrintHeader :: No columns defined skipping"); | ||
} else { | ||
Row headerRow = sheet.createRow(0); | ||
for (int colIndex = 0; colIndex < this.columns.size(); colIndex++) { | ||
headerRow.createCell(colIndex).setCellValue(this.columns.get(colIndex)); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public Writer printFooter() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Writer addRecordEntry(String columnName, Object value) { | ||
this.currentRow.put(columnName, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Writer nextRecord() throws IOException { | ||
if (columns.isEmpty()) { | ||
// Create columns based on this row | ||
this.currentRow.forEach((name, object) -> this.columns.add(name)); | ||
this.printHeader(); | ||
} | ||
|
||
Row row = sheet.createRow(sheet.getLastRowNum()+1); | ||
for (int colIndex = 0; colIndex < this.columns.size(); colIndex++) { | ||
row.createCell(colIndex).setCellValue(currentRow.get(this.columns.get(colIndex)).toString()); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public Writer flush() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
workbook.write(out); | ||
workbook.close(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
flatpack-poi/src/main/java/net/sf/flatpack/poi/writer/PoiWriterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.sf.flatpack.poi.writer; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import net.sf.flatpack.writer.Writer; | ||
import net.sf.flatpack.writer.WriterFactory; | ||
|
||
public class PoiWriterFactory implements WriterFactory { | ||
|
||
OutputStream outputStream; | ||
|
||
@Override | ||
public Writer createWriter(java.io.Writer out) throws IOException { | ||
return new PoiWriter(this.outputStream); | ||
} | ||
|
||
public PoiWriterFactory setOutputStream(OutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
return this; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
flatpack-poi/src/test/java/net/sf/flatpack/poi/writer/PoiWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.sf.flatpack.poi.writer; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.math.BigDecimal; | ||
|
||
import org.junit.Test; | ||
|
||
import net.sf.flatpack.writer.Writer; | ||
|
||
public class PoiWriterTest { | ||
|
||
@Test | ||
public void printHeader() throws IOException { | ||
OutputStream outputStream = new FileOutputStream("test.xlsx"); | ||
final Writer writer = new PoiWriterFactory().setOutputStream(outputStream).createWriter(null); | ||
|
||
writer | ||
.printHeader() | ||
.addRecordEntry("LASTNAME", "DOE") // New fluent | ||
.addRecordEntry("ADDRESS", "1234 CIRCLE CT") // | ||
.addRecordEntry("STATE", "OH") // | ||
.addRecordEntry("ZIP", "44035") // | ||
.addRecordEntry("FIRSTNAME", "JOHN") // | ||
.addRecordEntry("CITY", "ELYRIA") // | ||
.addRecordEntry("REVENUE", BigDecimal.ZERO) // | ||
.nextRecord() // | ||
.printFooter() | ||
.flush() | ||
.close(); | ||
outputStream.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters