Skip to content

Commit

Permalink
Merge pull request #792 from desertsky/ExcelGenerator_XlsxEnhancements
Browse files Browse the repository at this point in the history
Updated POI to 3.14 to support XLSX file generation
  • Loading branch information
darkv authored Aug 8, 2016
2 parents 97803f0 + beea23f commit 5362f42
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 27 deletions.
8 changes: 3 additions & 5 deletions Frameworks/Excel/ExcelGenerator/.classpath
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="Sources"/>
<classpathentry exported="true" kind="lib" path="Libraries/poi-3.6-20091214.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/ExcelGenerator/Support/poi-bin-3.6-20091214.zip!/poi-3.6/docs/apidocs"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="Libraries/poi-3.14-20160307.jar"/>
<classpathentry exported="true" kind="lib" path="Libraries/poi-ooxml-3.14-20160307.jar"/>
<classpathentry exported="true" kind="lib" path="Libraries/poi-ooxml-schemas-3.14-20160307.jar"/>
<classpathentry exported="true" kind="con" path="WOFramework/ERExtensions"/>
<classpathentry exported="true" kind="con" path="WOFramework/ERJars"/>
<classpathentry exported="true" kind="con" path="WOFramework/JavaFoundation"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<span>
<webobject name = "ComponentContent"></webobject></span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ComponentContent : WOComponentContent { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"WebObjects Release" = "WebObjects 5.0";
encoding = "UTF-8";
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -86,7 +86,7 @@ public class EGSimpleTableParser {
private static final Logger log = LoggerFactory.getLogger(EGSimpleTableParser.class);

private InputStream _contentStream;
private HSSFWorkbook _workbook;
private Workbook _workbook;
private NSMutableDictionary _styles = new NSMutableDictionary();
private NSMutableDictionary _fonts = new NSMutableDictionary();
private NSMutableDictionary _styleDicts;
Expand Down Expand Up @@ -126,7 +126,7 @@ public NSData data() {
return null;
}

public HSSFWorkbook workbook() {
public Workbook workbook() {
if(_workbook == null) {
parse();
}
Expand Down Expand Up @@ -251,7 +251,7 @@ private void parse() {
InputStream stream = _contentStream;
document = builder.parse(stream);

_workbook = new HSSFWorkbook();
_workbook = createWorkbook();

log.debug("{}", document.getDocumentElement());

Expand All @@ -267,6 +267,10 @@ private void parse() {
}
}

protected Workbook createWorkbook() {
return new HSSFWorkbook();
}

private void parseNode(Node node) {
String tagName = node.getLocalName().toLowerCase();
if("font".equals(tagName)) {
Expand Down Expand Up @@ -339,7 +343,7 @@ private void parseTable(Node tableNode) {
sheetName = sheetName.substring(0,31);
log.warn("Sheet name too long (max 31 Characters): {}", sheetName);
}
HSSFSheet sheet = _workbook.createSheet(sheetName);
Sheet sheet = _workbook.createSheet(sheetName);

NodeList rowNodes = tableNode.getChildNodes();

Expand All @@ -359,7 +363,7 @@ private void parseTable(Node tableNode) {
addEntriesFromNode(rowDict, rowNode);

log.debug("Row: {}", rowNum);
HSSFRow row = sheet.createRow(rowNum);
Row row = sheet.createRow(rowNum);

rowNum = rowNum + 1;
NodeList cellNodes = rowNode.getChildNodes();
Expand All @@ -369,7 +373,7 @@ private void parseTable(Node tableNode) {
&& ("td".equals(cellNode.getLocalName().toLowerCase())
|| "th".equals(cellNode.getLocalName().toLowerCase()))) {
int currentColumnNumber = row.getPhysicalNumberOfCells();
HSSFCell cell = row.createCell(currentColumnNumber);
Cell cell = row.createCell(currentColumnNumber);
Object value = null;
if(cellNode.getFirstChild() != null) {
value = cellNode.getFirstChild().getNodeValue();
Expand Down Expand Up @@ -420,7 +424,7 @@ private void parseTable(Node tableNode) {
case HSSFCell.CELL_TYPE_STRING:
default:
cell.setCellType(cellType.intValue());
cell.setCellValue(new HSSFRichTextString(value != null ? value.toString() : null));
cell.setCellValue(createRichTextString(value));
break;
}

Expand Down Expand Up @@ -452,7 +456,7 @@ private void parseTable(Node tableNode) {
}
}

HSSFCellStyle style = styleWithDictionary(cellDict);
CellStyle style = styleWithDictionary(cellDict);

if(style != null) {
cell.setCellStyle(style);
Expand All @@ -475,8 +479,12 @@ private void parseTable(Node tableNode) {
}
}

private HSSFFont fontWithID(String id) {
HSSFFont font = (HSSFFont)_fonts.objectForKey(id);
protected RichTextString createRichTextString(Object value) {
return new HSSFRichTextString(value != null ? value.toString() : null);
}

private Font fontWithID(String id) {
Font font = (Font)_fonts.objectForKey(id);
if(font == null) {
font = _workbook.createFont();

Expand Down Expand Up @@ -514,7 +522,7 @@ private HSSFFont fontWithID(String id) {
"alignment","verticalAlignment","format"
});

private HSSFCellStyle styleWithDictionary(NSDictionary dict) {
private CellStyle styleWithDictionary(NSDictionary dict) {
String cellClass = dictValueForKey(dict, "class", null);

log.debug("before - {}: {}", cellClass, dict);
Expand All @@ -538,13 +546,13 @@ private HSSFCellStyle styleWithDictionary(NSDictionary dict) {
}
log.debug("after - {}: {}", cellClass, dict);

HSSFCellStyle cellStyle = (HSSFCellStyle)_styles.objectForKey(dict);
CellStyle cellStyle = (CellStyle)_styles.objectForKey(dict);
if(cellStyle == null) {
cellStyle = _workbook.createCellStyle();

String fontID = dictValueForKey(dict, "font", null);
if(fontID != null) {
HSSFFont font = fontWithID(fontID);
Font font = fontWithID(fontID);
if(font == null) {
throw new IllegalArgumentException("Font ID not found!");
}
Expand Down Expand Up @@ -575,7 +583,7 @@ private HSSFCellStyle styleWithDictionary(NSDictionary dict) {

String formatString = dictValueForKey(dict, "format", null);
if(formatString != null) {
HSSFDataFormat format = _workbook.createDataFormat();
DataFormat format = _workbook.createDataFormat();
short formatId = format.getFormat(formatString);
cellStyle.setDataFormat(formatId);
}
Expand Down
18 changes: 15 additions & 3 deletions Frameworks/Excel/ExcelGenerator/Sources/er/excel/EGWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void appendToResponse(WOResponse response, WOContext context) {
}
InputStream stream = new ByteArrayInputStream(bytes);

EGSimpleTableParser parser = new EGSimpleTableParser(stream, fonts(), styles());
EGSimpleTableParser parser = parser(stream);
try {
NSData data = parser.data();
if((hasBinding("data") && canSetValueForBinding("data")) ||
Expand All @@ -118,15 +118,15 @@ public void appendToResponse(WOResponse response, WOContext context) {
} else {
String fileName = fileName();
if(fileName == null) {
fileName = "results.xls";
fileName = defaultFilename();
}

response.disableClientCaching();
response.appendHeader(String.valueOf( data.length()), "Content-Length" );
response.setContent(data); // Changed by ishimoto because it was sooooo buggy and didn't work in Japanese

response.setHeader("inline; filename=\"" + fileName + "\"", "content-disposition");
response.setHeader("application/vnd.ms-excel", "content-type");
response.setHeader(contentType(), "content-type");
}
} catch (Exception ex) {
if (ex.getCause() instanceof SAXParseException) {
Expand Down Expand Up @@ -165,4 +165,16 @@ protected String addLineNumbers(String in) {
}
return out;
}

protected String defaultFilename() {
return "results.xls";
}

protected String contentType() {
return "application/vnd.ms-excel";
}

protected EGSimpleTableParser parser(InputStream stream) {
return new EGSimpleTableParser(stream, fonts(), styles());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package er.excel;

import java.io.InputStream;

import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.webobjects.foundation.NSDictionary;

/**
* Instances of EGXLSXSimpleTableParser are used to generate Excel spreadsheets
* in XLSX format.
*
* @author Michael Hast on Jun 6, 2016
*/
public class EGXLSXSimpleTableParser extends EGSimpleTableParser {

// private static final Logger log = LoggerFactory.getLogger(EGXLSXSimpleTableParser.class);
//
// private InputStream _contentStream;
// private XSSFWorkbook _workbook;
// private NSMutableDictionary<String, NSDictionary<String, String>> _fontDicts;
// private NSMutableDictionary<String, NSDictionary<String, String>> _styleDicts;
// private NSMutableDictionary<String, XSSFFont> _fonts = new NSMutableDictionary<String, XSSFFont>();
// private NSMutableDictionary<NSDictionary<String, ?>, XSSFCellStyle> _styles = new NSMutableDictionary<NSDictionary<String, ?>, XSSFCellStyle>();
//
public EGXLSXSimpleTableParser(InputStream contentStream) {
this(contentStream, null, null);
}

public EGXLSXSimpleTableParser(InputStream contentStream, NSDictionary<String, NSDictionary<String, String>> fontDicts, NSDictionary<String, NSDictionary<String, String>> styleDicts) {
super(contentStream, fontDicts, styleDicts);
}
//
// EGSimpleTableParser API
//
@Override
protected Workbook createWorkbook() {
return new XSSFWorkbook();
}

@Override
protected RichTextString createRichTextString(Object value) {
return new XSSFRichTextString(value != null ? value.toString() : null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package er.excel;

import java.io.InputStream;

import com.webobjects.appserver.WOContext;

/**
* Class for XLSX Excel Component EGXLSXWrapper.
*
* @binding sample sample binding explanation
*
* @author Michael Hast on Jun 6, 2016 9:22:46 AM
*/
public class EGXLSXWrapper extends EGWrapper {

public EGXLSXWrapper(WOContext context) {
super(context);
}
//
// EGWrapper API
//
@Override
protected String defaultFilename() {
return "results.xlsx";
}

@Override
protected String contentType() {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}

@Override
protected EGSimpleTableParser parser(InputStream stream) {
return new EGXLSXSimpleTableParser(stream, fonts(), styles());
}
}

0 comments on commit 5362f42

Please sign in to comment.