Skip to content

Commit

Permalink
Added CELL_TYPE_BOOLEAN support to ExcelGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
mhast authored and rkiddy committed Feb 11, 2013
1 parent 4d2bbf6 commit 173dace
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ private void appendCell(HSSFCell cell) {
case HSSFCell.CELL_TYPE_FORMULA:
value = cell.getCellFormula();
break;


case HSSFCell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;

default:
value = cell.getStringCellValue();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,21 @@ private void parseTable(Node tableNode) {
log.info(e1);
}

case HSSFCell.CELL_TYPE_BOOLEAN:
cell.setCellType(cellType.intValue());
if (value != null) {
try {
Integer integer = Integer.parseInt(value.toString());
cell.setCellValue(integer > 0);
} catch (NumberFormatException ex) {
if (log.isDebugEnabled()) {
log.debug(ex.getMessage(), ex);
}
cell.setCellValue(new Boolean(value.toString()));
}
}
break;

case HSSFCell.CELL_TYPE_STRING:
default:
cell.setCellType(cellType.intValue());
Expand Down
83 changes: 59 additions & 24 deletions Frameworks/Excel/ExcelGenerator/Sources/er/excel/EGWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.commons.lang.CharEncoding;
import org.apache.log4j.Logger;
import org.xml.sax.SAXParseException;

import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WOResponse;
Expand Down Expand Up @@ -103,32 +104,66 @@ public void appendToResponse(WOResponse response, WOContext context) {
InputStream stream = new ByteArrayInputStream(bytes);

EGSimpleTableParser parser = new EGSimpleTableParser(stream, fonts(), styles());
NSData data = parser.data();
if((hasBinding("data") && canSetValueForBinding("data")) ||
(hasBinding("stream") && canSetValueForBinding("stream"))
) {
if(hasBinding("data")) {
setValueForBinding(data, "data");
}
if(hasBinding("stream")) {
setValueForBinding(data.stream(), "stream");
}
response.appendContentString(contentString);
} else {
String fileName = fileName();
if(fileName == null) {
fileName = "results.xls";
}

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");
}
try {
NSData data = parser.data();
if((hasBinding("data") && canSetValueForBinding("data")) ||
(hasBinding("stream") && canSetValueForBinding("stream"))
) {
if(hasBinding("data")) {
setValueForBinding(data, "data");
}
if(hasBinding("stream")) {
setValueForBinding(data.stream(), "stream");
}
response.appendContentString(contentString);
} else {
String fileName = fileName();
if(fileName == null) {
fileName = "results.xls";
}

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");
}
} catch (Exception ex) {
if (ex.getCause() instanceof SAXParseException) {
SAXParseException parseException = (SAXParseException)ex.getCause();
String logMessage = "'"+context().page().getClass().getName()+"' caused a SAXParseException";
logMessage += "\nMessage: '"+parseException.getMessage()+"'";
// weird but true, getLineNumber is off by 1 (for display purposes I think - mhast)
logMessage += "\nLine : "+(parseException.getLineNumber() - 1);
logMessage += "\nColumn : "+parseException.getColumnNumber();
logMessage += "\n--- content begin ---";
logMessage += addLineNumbers(contentString);
logMessage += "--- content end ---";
log.error(logMessage);
throw new NSForwardException(ex);
}
// else don't handle exception just pass it forward
else {
throw new NSForwardException(ex);
}
}
} else {
super.appendToResponse(response, context);
}
}

protected String addLineNumbers(String in) {
String out = "";
int i = 1, beginIndex = 0;
int endIndex = in.indexOf('\n');
while (endIndex != -1) {
out += in.substring(beginIndex, endIndex+1);
beginIndex = endIndex+1;
endIndex = in.indexOf('\n', beginIndex);
// only want to add line numbers if we have a next newline
if (endIndex != -1) out += (i++) + " ";
}
return out;
}
}

0 comments on commit 173dace

Please sign in to comment.