Skip to content

Commit 152e35b

Browse files
commit
1 parent 7cd98b6 commit 152e35b

9 files changed

Lines changed: 538 additions & 0 deletions

File tree

.gitignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
### Eclipse ###
2+
*.class
3+
/build
4+
/.classpath
5+
.metadata
6+
bin/**
7+
tmp/**
8+
tmp/**/*
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*~.nib
13+
local.properties
14+
.settings/
15+
.loadpath
16+
*.pydevproject
17+
.project
18+
19+
# Package Files #
20+
*.jar
21+
*.war
22+
*.ear
23+
24+
# OS generated files #
25+
.DS_Store
26+
.DS_Store?
27+
._*
28+
.Spotlight-V100
29+
.Trashes
30+
ehthumbs.db
31+
Thumbs.db
32+
33+
### Maven ###
34+
target/
35+
36+
37+
# External tool builders
38+
.externalToolBuilders/
39+
40+
# Locally stored "Eclipse launch configurations"
41+
*.launch
42+
43+
# CDT-specific
44+
.cproject
45+
46+
# PDT-specific
47+
.buildpath
48+
49+
### Java ###
50+
*.class
51+
52+
# Package Files #
53+
*.jar
54+
*.war
55+
*.ear

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Bean 2 xlsx / Excel (Java Beans to Excel files Exporter / Converter)
2+
3+
Converts Java Beans to Excel files (xlxs) via Apache POI including cellformats
4+
5+
6+
I got a little bit annoyed by the Primefaces datatable exporter (p:dataexporter), because it's just formating
7+
every cell in String (which can cause problems with functions in Excel) and doesn't support files with more than 65,536 rows - So I decided to write my own exporter.
8+
9+
I use Apache POI - the Java API for Microsoft Documents (http://poi.apache.org/)
10+
11+
**Fileformat:**
12+
The default Excel 2007 and later workbook format.
13+
In reality a ZIP compressed archive with a directory structure of XML text documents. Functions as the primary replacement for the former binary .xls format, although it does not support Excel macros for security reasons.
14+
15+
16+
Feel free to contribute
17+
18+
## Features
19+
20+
- Formats every cell with cell dataformat like money, decimal etc
21+
- CellTypes
22+
- "New" Excel Format
23+
- Simple setup via BeanColumn - class:
24+
25+
BeanColumn[] columns = new BeanColumn[] {
26+
new BeanColumn("name", "Carmodel", FormatType.TEXT),
27+
new BeanColumn("power", "Power", FormatType.INTEGER),
28+
new BeanColumn("priceinEuro", "Price in Euro", FormatType.MONEY) };
29+
30+
This simple setup transforms the attribute name to Text in Excel, priceinEuro to MONEY ("#,##0.00\\ €" = Euro) and so on.
31+
32+
33+
34+
## TODO
35+
Implement more cell formats like percent, Date, text with wrap (+width) etc.
36+
37+
Sincerely, your WurstCommander :)

pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>de.commanderwurst</groupId>
5+
<artifactId>Bean2XLSX</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<!-- UTF-8 Source Code -->
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
</properties>
12+
13+
<build>
14+
<plugins>
15+
<!-- Use JDK 1.7 -->
16+
<plugin>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<version>3.1</version>
19+
<configuration>
20+
<source>1.7</source>
21+
<target>1.7</target>
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
27+
<!-- Apache POI Libs -->
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.apache.poi</groupId>
31+
<artifactId>poi</artifactId>
32+
<version>3.10-FINAL</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.apache.poi</groupId>
37+
<artifactId>poi-ooxml</artifactId>
38+
<version>3.10-FINAL</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>commons-beanutils</groupId>
43+
<artifactId>commons-beanutils</artifactId>
44+
<version>1.9.2</version>
45+
</dependency>
46+
47+
</dependencies>
48+
</project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package de.wurstcommander.bean2xlsx;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.OutputStream;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.util.List;
9+
10+
import org.apache.commons.beanutils.PropertyUtils;
11+
import org.apache.poi.ss.usermodel.Cell;
12+
import org.apache.poi.ss.usermodel.CellStyle;
13+
import org.apache.poi.ss.usermodel.Font;
14+
import org.apache.poi.ss.usermodel.Row;
15+
import org.apache.poi.ss.usermodel.Sheet;
16+
import org.apache.poi.ss.usermodel.Workbook;
17+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
18+
19+
import de.wurstcommander.util.BeanColumn;
20+
import de.wurstcommander.util.FormatType;
21+
22+
/**
23+
* The Class Bean2Xlsx.
24+
*
25+
* @author WurstCommander
26+
*/
27+
public class Bean2xlsx {
28+
29+
private CellStyle cellstyle;
30+
private Workbook workbook;
31+
32+
public Bean2xlsx() {
33+
workbook = new XSSFWorkbook();
34+
cellstyle = workbook.createCellStyle();
35+
}
36+
37+
private void addSheet(List<?> data, BeanColumn[] columns, String sheetname) {
38+
Sheet sheet = workbook.createSheet(sheetname);
39+
// count of columns
40+
int colcount = columns.length;
41+
int currentRow = 0;
42+
Row row;
43+
44+
// Header creation
45+
row = sheet.createRow(currentRow);
46+
// Loop over colums of the provided Bean and write the headers
47+
for (int i = 0; i < colcount; i++) {
48+
// write headertext for each column
49+
writeCell(row, i, (Object) columns[i].getHeader(), FormatType.TEXT,
50+
null, null);
51+
}
52+
currentRow++; // Increment row after writing header
53+
54+
// Write the real reportdata
55+
for (int i = 0; i < data.size(); i++) {
56+
// create a row in the spreadsheet
57+
row = sheet.createRow(currentRow++);
58+
// Get the data from the bean for the current row
59+
Object bean = data.get(i);
60+
// For each column object, create a column on the current row
61+
for (int y = 0; y < colcount; y++) {
62+
Object value = null;
63+
// Get value of property
64+
try {
65+
value = PropertyUtils.getProperty(bean,
66+
columns[y].getProperty());
67+
} catch (IllegalAccessException | InvocationTargetException
68+
| NoSuchMethodException e) {
69+
System.err
70+
.println("Error getting value from the provided bean!");
71+
e.printStackTrace();
72+
}
73+
writeCell(row, y, value, columns[y].getType(),
74+
columns[y].getColor(), columns[y].getFont());
75+
}
76+
}
77+
}
78+
79+
public void bean2xlsx(List<?> data, BeanColumn[] columns, String filename,
80+
String sheetname) {
81+
addSheet(data, columns, sheetname);
82+
File f = new File(filename);
83+
// delete file -- testing!
84+
f.delete();
85+
try {
86+
FileOutputStream output = new FileOutputStream(filename);
87+
// Write Excelfile
88+
write(output);
89+
90+
} catch (FileNotFoundException e) {
91+
System.err.println("File was not found!" + filename);
92+
e.printStackTrace();
93+
} catch (Exception e) {
94+
System.err.println("Some error occured ");
95+
e.printStackTrace();
96+
}
97+
98+
}
99+
100+
private void write(OutputStream outputStream) throws Exception {
101+
workbook.write(outputStream);
102+
}
103+
104+
private void writeCell(Row row, int col, Object value,
105+
FormatType formatType, Short bgColor, Font boldFont) {
106+
Cell cell;
107+
if (value == null) {
108+
return;
109+
}
110+
if (boldFont != null) {
111+
// font later
112+
System.out.println("font given, but not yet implemented");
113+
}
114+
switch (formatType) {
115+
case TEXT:
116+
cell = row.createCell(col);
117+
cell.setCellType(Cell.CELL_TYPE_STRING);
118+
cell.setCellValue(value.toString());
119+
break;
120+
case INTEGER:
121+
cell = row.createCell(col);
122+
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
123+
cell.setCellValue(Double.valueOf(value.toString()));
124+
break;
125+
case MONEY:
126+
cell = row.createCell(col);
127+
Double bg = (Double) value;
128+
cellstyle.setDataFormat(workbook.getCreationHelper()
129+
.createDataFormat().getFormat("#,##0.00\\ €"));
130+
cell.setCellValue(bg.doubleValue());
131+
cell.setCellStyle(cellstyle);
132+
break;
133+
case PERCENTAGE:
134+
System.err.println("NOT YET IMPLEMENTED!");
135+
case FLOAT:
136+
System.err.println("NOT YET IMPLEMENTED!");
137+
break;
138+
case DATE:
139+
System.err.println("NOT YET IMPLEMENTED!");
140+
break;
141+
default:
142+
break;
143+
}
144+
if (bgColor != null) {
145+
// color later... :)
146+
System.out
147+
.println("backgroundcolor given, but not yet implemented");
148+
}
149+
}
150+
151+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package de.wurstcommander.util;
2+
3+
import org.apache.poi.xssf.usermodel.XSSFFont;
4+
5+
public class BeanColumn {
6+
7+
private Short color;
8+
private XSSFFont font;
9+
// Headertext for the column in the sheet
10+
private String headertext;
11+
// property name = global variablename of the java bean
12+
private String property;
13+
// CellFormat
14+
private FormatType type;
15+
16+
public BeanColumn(String function, String header, FormatType type) {
17+
this(function, header, type, null, null);
18+
}
19+
20+
public BeanColumn(String function, String header, FormatType type,
21+
Short color) {
22+
this(function, header, type, null, color);
23+
}
24+
25+
public BeanColumn(String function, String header, FormatType type,
26+
XSSFFont font) {
27+
this(function, header, type, font, null);
28+
}
29+
30+
public BeanColumn(String function, String header, FormatType type,
31+
XSSFFont font, Short color) {
32+
this.property = function;
33+
this.headertext = header;
34+
this.type = type;
35+
this.font = font;
36+
this.color = color;
37+
}
38+
39+
// Getter and Setter - Start
40+
41+
public Short getColor() {
42+
return color;
43+
}
44+
45+
public XSSFFont getFont() {
46+
return font;
47+
}
48+
49+
public String getHeader() {
50+
return headertext;
51+
}
52+
53+
public String getProperty() {
54+
return property;
55+
}
56+
57+
public FormatType getType() {
58+
return type;
59+
}
60+
61+
public void setColor(Short m_color) {
62+
this.color = m_color;
63+
}
64+
65+
public void setFont(XSSFFont m_font) {
66+
this.font = m_font;
67+
}
68+
69+
public void setHeader(String header) {
70+
this.headertext = header;
71+
}
72+
73+
public void setProperty(String property) {
74+
this.property = property;
75+
}
76+
77+
public void setType(FormatType type) {
78+
this.type = type;
79+
}
80+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.wurstcommander.util;
2+
3+
public enum FormatType {
4+
5+
DATE, FLOAT, INTEGER, MONEY, PERCENTAGE, TEXT
6+
7+
}

0 commit comments

Comments
 (0)