fix hang in excel report generation when special chars present in datatype

This commit is contained in:
adam-m 2013-01-08 15:26:30 -05:00
parent 7454f4ce74
commit ad6ae1ba3c

View File

@ -124,12 +124,14 @@ public class ReportExcel implements TableReportModule {
} }
} }
/** /**
* Start a new sheet for the given data type. * Start a new sheet for the given data type.
* @param title data type name * @param title data type name
*/ */
@Override @Override
public void startDataType(String title) { public void startDataType(String title) {
title = escapeForExcel(title);
sheet = wb.createSheet(title); sheet = wb.createSheet(title);
sheet.setAutobreaks(true); sheet.setAutobreaks(true);
currentDataType = title; currentDataType = title;
@ -154,6 +156,7 @@ public class ReportExcel implements TableReportModule {
*/ */
@Override @Override
public void startSet(String setName) { public void startSet(String setName) {
setName = escapeForExcel(setName);
Row temp = sheet.createRow(rowCount); Row temp = sheet.createRow(rowCount);
temp.setRowStyle(setStyle); temp.setRowStyle(setStyle);
temp.createCell(0).setCellValue(setName); temp.createCell(0).setCellValue(setName);
@ -179,6 +182,7 @@ public class ReportExcel implements TableReportModule {
*/ */
@Override @Override
public void addSetElement(String elementName) { public void addSetElement(String elementName) {
elementName = escapeForExcel(elementName);
Row temp = sheet.createRow(rowCount); Row temp = sheet.createRow(rowCount);
temp.setRowStyle(elementStyle); temp.setRowStyle(elementStyle);
temp.createCell(0).setCellValue(elementName); temp.createCell(0).setCellValue(elementName);
@ -248,4 +252,13 @@ public class ReportExcel implements TableReportModule {
return "Excel.xlsx"; return "Excel.xlsx";
} }
/**
* Escape special chars for Excel that would cause errors/hangs in generating report
* The following are not valid for sheet names: ? / \ * :
* @param text
* @return
*/
private static String escapeForExcel(String text) {
return text.replaceAll("[\\/\\:\\?\\*\\\\]", "_");
}
} }