Skip to content

Commit

Permalink
Appendium#44 FlatPack poi example
Browse files Browse the repository at this point in the history
  • Loading branch information
martindiphoorn committed Nov 22, 2018
1 parent 8bb9d02 commit 0363320
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 1 deletion.
1 change: 0 additions & 1 deletion flatpack-excel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<version>4.0.4-SNAPSHOT</version>
</parent>


<artifactId>flatpack-excel</artifactId>
<name>FlatPack Excel</name>
<packaging>bundle</packaging> <!-- (1) OSGi -->
Expand Down
95 changes: 95 additions & 0 deletions flatpack-poi/pom.xml
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>
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();
}
}
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;
}
}
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();
}
}
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<modules>
<module>flatpack</module>
<module>flatpack-excel</module>
<module>flatpack-poi</module>
<module>flatpack-samples</module>
</modules>

Expand Down Expand Up @@ -201,6 +202,15 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 0363320

Please sign in to comment.