Created
December 1, 2011 12:53
-
-
Save jhorstmann/1416519 to your computer and use it in GitHub Desktop.
Patch for Primefaces 3.0.M4 to use native excel formats instead of converting everything to strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/org/primefaces/component/export/CSVExporter.java b/src/main/java/org/primefaces/component/export/CSVExporter.java | |
index e5e8992..db0e981 100644 | |
--- a/src/main/java/org/primefaces/component/export/CSVExporter.java | |
+++ b/src/main/java/org/primefaces/component/export/CSVExporter.java | |
@@ -20,6 +20,7 @@ import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Array; | |
+import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
@@ -34,7 +35,7 @@ import org.primefaces.component.datatable.DataTable; | |
public class CSVExporter extends Exporter { | |
@Override | |
- public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor) throws IOException { | |
+ public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, boolean convert) throws IOException { | |
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); | |
response.setContentType("text/csv"); | |
@@ -48,18 +49,18 @@ public class CSVExporter extends Exporter { | |
PrintWriter writer = new PrintWriter(osw); | |
List<UIColumn> columns = getColumnsToExport(table, excludeColumns); | |
String rowIndexVar = table.getRowIndexVar(); | |
- | |
- addFacetColumns(writer, columns, ColumnType.HEADER); | |
+ | |
+ addFacetColumns(writer, columns, ColumnType.HEADER, convert); | |
if(pageOnly) | |
- exportPageOnly(context, table, columns, writer); | |
+ exportPageOnly(context, table, columns, writer, convert); | |
else if(selectionOnly) | |
- exportSelectionOnly(context, table, columns, writer); | |
+ exportSelectionOnly(context, table, columns, writer, convert); | |
else | |
- exportAll(context, table, columns, writer); | |
+ exportAll(context, table, columns, writer, convert); | |
if(hasColumnFooter(columns)) { | |
- addFacetColumns(writer, columns, ColumnType.FOOTER); | |
+ addFacetColumns(writer, columns, ColumnType.FOOTER, convert); | |
} | |
table.setRowIndex(-1); | |
@@ -74,7 +75,7 @@ public class CSVExporter extends Exporter { | |
response.getOutputStream().flush(); | |
} | |
- public void exportPageOnly(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer) throws IOException{ | |
+ public void exportPageOnly(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer, boolean convert) throws IOException{ | |
int first = table.getFirst(); | |
int size = first + table.getRows(); | |
String rowIndexVar = table.getRowIndexVar(); | |
@@ -88,12 +89,12 @@ public class CSVExporter extends Exporter { | |
context.getExternalContext().getRequestMap().put(rowIndexVar, i); | |
} | |
- addColumnValues(writer, columns); | |
+ addColumnValues(writer, columns, convert); | |
writer.write("\n"); | |
} | |
} | |
- public void exportSelectionOnly(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer) throws IOException{ | |
+ public void exportSelectionOnly(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer, boolean convert) throws IOException{ | |
Object selection = table.getSelection(); | |
boolean single = table.isSingleSelectionMode(); | |
int size = selection == null ? 0 : single ? 1 : Array.getLength(selection); | |
@@ -101,12 +102,12 @@ public class CSVExporter extends Exporter { | |
for (int i = 0; i < size; i++) { | |
context.getExternalContext().getRequestMap().put(table.getVar(), single ? selection : Array.get(selection, i) ); | |
- addColumnValues(writer, columns); | |
+ addColumnValues(writer, columns, convert); | |
writer.write("\n"); | |
} | |
} | |
- public void exportAll(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer) throws IOException{ | |
+ public void exportAll(FacesContext context, DataTable table, List<UIColumn> columns, PrintWriter writer, boolean convert) throws IOException{ | |
String rowIndexVar = table.getRowIndexVar(); | |
int first = table.getFirst(); | |
@@ -129,7 +130,7 @@ public class CSVExporter extends Exporter { | |
context.getExternalContext().getRequestMap().put(rowIndexVar, i); | |
} | |
- addColumnValues(writer, columns); | |
+ addColumnValues(writer, columns, convert); | |
writer.write("\n"); | |
} | |
@@ -147,7 +148,7 @@ public class CSVExporter extends Exporter { | |
context.getExternalContext().getRequestMap().put(rowIndexVar, i); | |
} | |
- addColumnValues(writer, columns); | |
+ addColumnValues(writer, columns, convert); | |
writer.write("\n"); | |
} | |
@@ -156,18 +157,18 @@ public class CSVExporter extends Exporter { | |
} | |
} | |
- private void addColumnValues(PrintWriter writer, List<UIColumn> columns) throws IOException { | |
+ private void addColumnValues(PrintWriter writer, List<UIColumn> columns, boolean convert) throws IOException { | |
for(Iterator<UIColumn> iterator = columns.iterator(); iterator.hasNext();) { | |
- addColumnValue(writer, iterator.next().getChildren()); | |
+ addColumnValue(writer, iterator.next().getChildren(), convert); | |
if(iterator.hasNext()) | |
writer.write(","); | |
} | |
} | |
- private void addFacetColumns(PrintWriter writer, List<UIColumn> columns, ColumnType columnType) throws IOException { | |
+ private void addFacetColumns(PrintWriter writer, List<UIColumn> columns, ColumnType columnType, boolean convert) throws IOException { | |
for(Iterator<UIColumn> iterator = columns.iterator(); iterator.hasNext();) { | |
- addColumnValue(writer, iterator.next().getFacet(columnType.facet())); | |
+ addColumnValue(writer, iterator.next().getFacet(columnType.facet()), convert); | |
if(iterator.hasNext()) | |
writer.write(","); | |
@@ -176,23 +177,38 @@ public class CSVExporter extends Exporter { | |
writer.write("\n"); | |
} | |
- private void addColumnValue(PrintWriter writer, UIComponent component) throws IOException { | |
- String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component); | |
- | |
- writer.write("\"" + value + "\""); | |
+ private void addColumnValue(PrintWriter writer, UIComponent component, boolean convert) throws IOException { | |
+ Object value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component, convert); | |
+ if (value instanceof Number) { | |
+ writer.write(value.toString()); | |
+ } else { | |
+ writer.write("\"" + value + "\""); | |
+ } | |
} | |
- private void addColumnValue(PrintWriter writer, List<UIComponent> components) throws IOException { | |
- StringBuilder builder = new StringBuilder(); | |
- | |
- for(UIComponent component : components) { | |
- if(component.isRendered()) { | |
- String value = exportValue(FacesContext.getCurrentInstance(), component); | |
- | |
- builder.append(value); | |
- } | |
- } | |
- | |
- writer.write("\"" + builder.toString() + "\""); | |
+ private void addColumnValue(PrintWriter writer, List<UIComponent> components, boolean convert) throws IOException { | |
+ List<Object> values = new ArrayList<Object>(components.size()); | |
+ for(UIComponent component : components) { | |
+ if(component.isRendered()) { | |
+ Object value = exportValue(FacesContext.getCurrentInstance(), component, convert); | |
+ if (value != null) { | |
+ values.add(value); | |
+ } | |
+ } | |
+ } | |
+ if (values.size() == 1) { | |
+ Object value = values.get(0); | |
+ if (value instanceof Number) { | |
+ writer.write(value.toString()); | |
+ } else { | |
+ writer.write("\"" + value + "\""); | |
+ } | |
+ } else if (values.size() > 1) { | |
+ StringBuilder builder = new StringBuilder(); | |
+ for (Object value : values) { | |
+ builder.append(value); | |
+ } | |
+ writer.write("\"" + builder.toString() + "\""); | |
+ } | |
} | |
} | |
diff --git a/src/main/java/org/primefaces/component/export/DataExporter.java b/src/main/java/org/primefaces/component/export/DataExporter.java | |
index 3faac3d..0b546f5 100644 | |
--- a/src/main/java/org/primefaces/component/export/DataExporter.java | |
+++ b/src/main/java/org/primefaces/component/export/DataExporter.java | |
@@ -48,10 +48,12 @@ public class DataExporter implements ActionListener, StateHolder { | |
private MethodExpression preProcessor; | |
private MethodExpression postProcessor; | |
+ | |
+ private ValueExpression skipConversion; | |
public DataExporter() {} | |
- public DataExporter(ValueExpression target, ValueExpression type, ValueExpression fileName, ValueExpression pageOnly, ValueExpression selectionOnly, ValueExpression exludeColumns, ValueExpression encoding, MethodExpression preProcessor, MethodExpression postProcessor) { | |
+ public DataExporter(ValueExpression target, ValueExpression type, ValueExpression fileName, ValueExpression pageOnly, ValueExpression selectionOnly, ValueExpression exludeColumns, ValueExpression encoding, MethodExpression preProcessor, MethodExpression postProcessor, ValueExpression skipConversion) { | |
this.target = target; | |
this.type = type; | |
this.fileName = fileName; | |
@@ -61,6 +63,7 @@ public class DataExporter implements ActionListener, StateHolder { | |
this.preProcessor = preProcessor; | |
this.postProcessor = postProcessor; | |
this.encoding = encoding; | |
+ this.skipConversion = skipConversion; | |
} | |
public void processAction(ActionEvent event){ | |
@@ -82,13 +85,19 @@ public class DataExporter implements ActionListener, StateHolder { | |
boolean isPageOnly = false; | |
if(pageOnly != null) { | |
- isPageOnly = pageOnly.isLiteralText() ? Boolean.valueOf(pageOnly.getValue(facesContext.getELContext()).toString()) : (Boolean) pageOnly.getValue(facesContext.getELContext()); | |
+ isPageOnly = pageOnly.isLiteralText() ? Boolean.valueOf(pageOnly.getValue(elContext).toString()) : (Boolean) pageOnly.getValue(elContext); | |
} | |
boolean isSelectionOnly = false; | |
if(selectionOnly != null) { | |
- isSelectionOnly = selectionOnly.isLiteralText() ? Boolean.valueOf(selectionOnly.getValue(facesContext.getELContext()).toString()) : (Boolean) selectionOnly.getValue(facesContext.getELContext()); | |
+ isSelectionOnly = selectionOnly.isLiteralText() ? Boolean.valueOf(selectionOnly.getValue(elContext).toString()) : (Boolean) selectionOnly.getValue(elContext); | |
} | |
+ | |
+ boolean isSkipConversion = false; | |
+ if(skipConversion != null) { | |
+ isSkipConversion = skipConversion.isLiteralText() ? Boolean.valueOf(skipConversion.getValue(elContext).toString()) : (Boolean) skipConversion.getValue(elContext); | |
+ } | |
+ System.out.println("skipConversion="+skipConversion); | |
try { | |
Exporter exporter = ExporterFactory.getExporterForType(exportAs); | |
@@ -99,7 +108,7 @@ public class DataExporter implements ActionListener, StateHolder { | |
throw new FacesException("Unsupported datasource target:\"" + component.getClass().getName() + "\", exporter must target a PrimeFaces DataTable."); | |
DataTable table = (DataTable) component; | |
- exporter.export(facesContext, table, outputFileName, isPageOnly, isSelectionOnly, excludedColumnIndexes, encodingType, preProcessor, postProcessor); | |
+ exporter.export(facesContext, table, outputFileName, isPageOnly, isSelectionOnly, excludedColumnIndexes, encodingType, preProcessor, postProcessor, !isSkipConversion); | |
facesContext.responseComplete(); | |
} catch (IOException e) { | |
diff --git a/src/main/java/org/primefaces/component/export/DataExporterTagHandler.java b/src/main/java/org/primefaces/component/export/DataExporterTagHandler.java | |
index ab6aab3..c019e8d 100644 | |
--- a/src/main/java/org/primefaces/component/export/DataExporterTagHandler.java | |
+++ b/src/main/java/org/primefaces/component/export/DataExporterTagHandler.java | |
@@ -41,6 +41,7 @@ public class DataExporterTagHandler extends TagHandler { | |
private final TagAttribute preProcessor; | |
private final TagAttribute postProcessor; | |
private final TagAttribute encoding; | |
+ private final TagAttribute skipConversion; | |
public DataExporterTagHandler(TagConfig tagConfig) { | |
super(tagConfig); | |
@@ -53,6 +54,7 @@ public class DataExporterTagHandler extends TagHandler { | |
this.encoding = getAttribute("encoding"); | |
this.preProcessor = getAttribute("preProcessor"); | |
this.postProcessor = getAttribute("postProcessor"); | |
+ this.skipConversion = getAttribute("skipConversion"); | |
} | |
public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException, FacesException, FaceletException, ELException { | |
@@ -66,6 +68,7 @@ public class DataExporterTagHandler extends TagHandler { | |
ValueExpression encodingVE = null; | |
MethodExpression preProcessorME = null; | |
MethodExpression postProcessorME = null; | |
+ ValueExpression skipConversionME = null; | |
if(excludeColumns != null) { | |
excludeColumnsVE = excludeColumns.getValueExpression(faceletContext, Object.class); | |
@@ -85,9 +88,12 @@ public class DataExporterTagHandler extends TagHandler { | |
if(postProcessor != null) { | |
postProcessorME = postProcessor.getMethodExpression(faceletContext, null, new Class[]{Object.class}); | |
} | |
+ if(skipConversion != null) { | |
+ skipConversionME = skipConversion.getValueExpression(faceletContext, Boolean.class); | |
+ } | |
ActionSource actionSource = (ActionSource) parent; | |
- actionSource.addActionListener(new DataExporter(targetVE, typeVE, fileNameVE, pageOnlyVE, selectionOnlyVE, excludeColumnsVE,encodingVE, preProcessorME, postProcessorME)); | |
+ actionSource.addActionListener(new DataExporter(targetVE, typeVE, fileNameVE, pageOnlyVE, selectionOnlyVE, excludeColumnsVE,encodingVE, preProcessorME, postProcessorME, skipConversionME)); | |
} | |
} | |
diff --git a/src/main/java/org/primefaces/component/export/ExcelExporter.java b/src/main/java/org/primefaces/component/export/ExcelExporter.java | |
index 2502c66..dc0fe8a 100644 | |
--- a/src/main/java/org/primefaces/component/export/ExcelExporter.java | |
+++ b/src/main/java/org/primefaces/component/export/ExcelExporter.java | |
@@ -17,6 +17,11 @@ package org.primefaces.component.export; | |
import java.io.IOException; | |
import java.lang.reflect.Array; | |
+import java.math.BigDecimal; | |
+import java.math.BigInteger; | |
+import java.util.ArrayList; | |
+import java.util.Calendar; | |
+import java.util.Date; | |
import java.util.List; | |
import javax.el.MethodExpression; | |
@@ -24,8 +29,10 @@ import javax.faces.component.UIColumn; | |
import javax.faces.component.UIComponent; | |
import javax.faces.context.FacesContext; | |
import javax.servlet.http.HttpServletResponse; | |
+import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
+import org.apache.poi.hssf.util.HSSFColor; | |
import org.apache.poi.ss.usermodel.Cell; | |
import org.apache.poi.ss.usermodel.Row; | |
import org.apache.poi.ss.usermodel.Sheet; | |
@@ -34,28 +41,78 @@ import org.apache.poi.ss.usermodel.Workbook; | |
import org.primefaces.component.datatable.DataTable; | |
public class ExcelExporter extends Exporter { | |
+ | |
+ public static class CellStyles { | |
+ private static final short DATA_FORMAT_DATE = 0x16; | |
+ private static final short DATA_FORMAT_DECIMAL = 2; | |
+ private static final short DATA_FORMAT_INTEGER = 1; | |
+ | |
+ public final HSSFCellStyle styleNormal; | |
+ public final HSSFCellStyle styleWrap; | |
+ public final HSSFCellStyle styleDate; | |
+ public final HSSFCellStyle styleDecimal; | |
+ public final HSSFCellStyle styleInteger; | |
+ | |
+ public CellStyles(HSSFWorkbook wb) { | |
+ styleNormal = createStyle(wb, HSSFCellStyle.ALIGN_GENERAL, true); | |
+ styleWrap = createStyle(wb, HSSFCellStyle.ALIGN_GENERAL, true); | |
+ styleDate = createStyle(wb, HSSFCellStyle.ALIGN_GENERAL, true); | |
+ styleDecimal = createStyle(wb, HSSFCellStyle.ALIGN_GENERAL, true); | |
+ styleInteger = createStyle(wb, HSSFCellStyle.ALIGN_GENERAL, true); | |
+ styleWrap.setWrapText(true); | |
+ styleDate.setDataFormat(DATA_FORMAT_DATE); | |
+ styleDecimal.setDataFormat(DATA_FORMAT_DECIMAL); | |
+ styleInteger.setDataFormat(DATA_FORMAT_INTEGER); | |
+ } | |
+ | |
+ private static void addBorders(HSSFCellStyle style) { | |
+ style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
+ style.setBottomBorderColor(HSSFColor.BLACK.index); | |
+ style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
+ style.setLeftBorderColor(HSSFColor.BLACK.index); | |
+ style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
+ style.setRightBorderColor(HSSFColor.BLACK.index); | |
+ style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
+ style.setTopBorderColor(HSSFColor.BLACK.index); | |
+ } | |
+ | |
+ private static HSSFCellStyle createStyle(HSSFWorkbook wb, short align, boolean borders) { | |
+ HSSFCellStyle style = wb.createCellStyle(); | |
+ //style.setFont(createFont(wb)); | |
+ style.setAlignment(align); | |
+ | |
+ if(borders){ | |
+ addBorders(style); | |
+ } | |
+ | |
+ return style; | |
+ } | |
+ } | |
@Override | |
- public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor) throws IOException { | |
- Workbook wb = new HSSFWorkbook(); | |
- Sheet sheet = wb.createSheet(); | |
+ public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, boolean convert) throws IOException { | |
+ HSSFWorkbook wb = new HSSFWorkbook(); | |
+ | |
+ CellStyles styles = new CellStyles(wb); | |
+ | |
+ Sheet sheet = wb.createSheet(); | |
List<UIColumn> columns = getColumnsToExport(table, excludeColumns); | |
String rowIndexVar = table.getRowIndexVar(); | |
if(preProcessor != null) { | |
preProcessor.invoke(context.getELContext(), new Object[]{wb}); | |
} | |
- addFacetColumns(sheet, columns, ColumnType.HEADER, 0); | |
+ addFacetColumns(sheet, columns, ColumnType.HEADER, 0, styles, convert); | |
if(pageOnly) | |
- exportPageOnly(context, table, columns, sheet); | |
+ exportPageOnly(context, table, columns, sheet, styles, convert); | |
else if(selectionOnly) | |
- exportSelectionOnly(context, table, columns, sheet); | |
+ exportSelectionOnly(context, table, columns, sheet, styles, convert); | |
else | |
- exportAll(context, table, columns, sheet); | |
+ exportAll(context, table, columns, sheet, styles, convert); | |
if(hasColumnFooter(columns)) { | |
- addFacetColumns(sheet, columns, ColumnType.FOOTER, sheet.getLastRowNum() + 1); | |
+ addFacetColumns(sheet, columns, ColumnType.FOOTER, sheet.getLastRowNum() + 1, styles, convert); | |
} | |
table.setRowIndex(-1); | |
@@ -71,7 +128,7 @@ public class ExcelExporter extends Exporter { | |
writeExcelToResponse(((HttpServletResponse)context.getExternalContext().getResponse()), wb, filename); | |
} | |
- protected void exportPageOnly(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet) { | |
+ protected void exportPageOnly(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet, CellStyles styles, boolean convert) { | |
String rowIndexVar = table.getRowIndexVar(); | |
int numberOfColumns = columns.size(); | |
@@ -91,12 +148,12 @@ public class ExcelExporter extends Exporter { | |
Row row = sheet.createRow(sheetRowIndex++); | |
for(int j = 0; j < numberOfColumns; j++) { | |
- addColumnValue(row, columns.get(j).getChildren(), j); | |
+ addColumnValue(row, columns.get(j).getChildren(), j, styles, convert); | |
} | |
} | |
} | |
- protected void exportSelectionOnly(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet) { | |
+ protected void exportSelectionOnly(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet, CellStyles styles, boolean convert) { | |
int numberOfColumns = columns.size(); | |
Object selection = table.getSelection(); | |
@@ -111,12 +168,12 @@ public class ExcelExporter extends Exporter { | |
Row row = sheet.createRow(sheetRowIndex++); | |
for(int j = 0; j < numberOfColumns; j++) { | |
- addColumnValue(row, columns.get(j).getChildren(), j); | |
+ addColumnValue(row, columns.get(j).getChildren(), j, styles, convert); | |
} | |
} | |
} | |
- protected void exportAll(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet) { | |
+ protected void exportAll(FacesContext context, DataTable table, List<UIColumn> columns, Sheet sheet, CellStyles styles, boolean convert) { | |
String rowIndexVar = table.getRowIndexVar(); | |
int numberOfColumns = columns.size(); | |
@@ -144,7 +201,7 @@ public class ExcelExporter extends Exporter { | |
Row row = sheet.createRow(sheetRowIndex++); | |
for(int j = 0; j < numberOfColumns; j++) { | |
- addColumnValue(row, columns.get(j).getChildren(), j); | |
+ addColumnValue(row, columns.get(j).getChildren(), j, styles, convert); | |
} | |
} | |
@@ -165,7 +222,7 @@ public class ExcelExporter extends Exporter { | |
Row row = sheet.createRow(sheetRowIndex++); | |
for(int j = 0; j < numberOfColumns; j++) { | |
- addColumnValue(row, columns.get(j).getChildren(), j); | |
+ addColumnValue(row, columns.get(j).getChildren(), j, styles, convert); | |
} | |
} | |
@@ -174,35 +231,76 @@ public class ExcelExporter extends Exporter { | |
} | |
} | |
- protected void addFacetColumns(Sheet sheet, List<UIColumn> columns, ColumnType columnType, int rowIndex) { | |
+ protected void addFacetColumns(Sheet sheet, List<UIColumn> columns, ColumnType columnType, int rowIndex, CellStyles styles, boolean convert) { | |
Row rowHeader = sheet.createRow(rowIndex); | |
for(int i = 0; i < columns.size(); i++) { | |
- addColumnValue(rowHeader, columns.get(i).getFacet(columnType.facet()), i); | |
+ addColumnValue(rowHeader, columns.get(i).getFacet(columnType.facet()), i, styles, convert); | |
} | |
} | |
- | |
- protected void addColumnValue(Row row, UIComponent component, int index) { | |
+ | |
+ protected void addColumnValue(Row row, UIComponent component, int index, CellStyles styles, boolean convert) { | |
Cell cell = row.createCell(index); | |
- String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component); | |
+ Object value = component == null ? null : exportValue(FacesContext.getCurrentInstance(), component, convert); | |
+ addColumnValue(cell, value, styles); | |
+ } | |
- cell.setCellValue(new HSSFRichTextString(value)); | |
+ private void addColumnValue(Cell cell, Object value, CellStyles styles) { | |
+ if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long || value instanceof BigInteger) { | |
+ cell.setCellValue(((Number)value).doubleValue()); | |
+ cell.setCellStyle(styles.styleInteger); | |
+ } | |
+ else if (value instanceof BigDecimal) { | |
+ BigDecimal bd = (BigDecimal)value; | |
+ cell.setCellValue(bd.doubleValue()); | |
+ cell.setCellStyle(bd.scale() > 0 ? styles.styleDecimal : styles.styleInteger); | |
+ } | |
+ else if (value instanceof Number) { | |
+ cell.setCellValue(((Number)value).doubleValue()); | |
+ cell.setCellStyle(styles.styleDecimal); | |
+ } | |
+ else if (value instanceof Boolean) { | |
+ cell.setCellValue(((Boolean)value).booleanValue()); | |
+ cell.setCellStyle(styles.styleNormal); | |
+ } | |
+ else if (value instanceof Date) { | |
+ cell.setCellValue((Date)value); | |
+ cell.setCellStyle(styles.styleDate); | |
+ } | |
+ else if (value instanceof Calendar) { | |
+ cell.setCellValue((Calendar)value); | |
+ cell.setCellStyle(styles.styleDate); | |
+ } | |
+ else if (value != null) { | |
+ String str = String.valueOf(value); | |
+ HSSFRichTextString text = new HSSFRichTextString(str); | |
+ cell.setCellValue(text); | |
+ cell.setCellStyle(str.contains("\n") ? styles.styleWrap : styles.styleNormal); | |
+ } | |
} | |
- protected void addColumnValue(Row row, List<UIComponent> components, int index) { | |
+ protected void addColumnValue(Row row, List<UIComponent> components, int index, CellStyles styles, boolean convert) { | |
Cell cell = row.createCell(index); | |
- StringBuilder builder = new StringBuilder(); | |
+ List<Object> values = new ArrayList<Object>(components.size()); | |
for(UIComponent component : components) { | |
if(component.isRendered()) { | |
- String value = exportValue(FacesContext.getCurrentInstance(), component); | |
- | |
- if(value != null) | |
- builder.append(value); | |
+ Object value = exportValue(FacesContext.getCurrentInstance(), component, convert); | |
+ if (value != null) { | |
+ values.add(value); | |
+ } | |
} | |
} | |
- | |
- cell.setCellValue(new HSSFRichTextString(builder.toString())); | |
+ if (values.size() == 1) { | |
+ addColumnValue(cell, values.get(0), styles); | |
+ } | |
+ else if (values.size() > 1) { | |
+ StringBuilder builder = new StringBuilder(); | |
+ for (Object value : values) { | |
+ builder.append(value); | |
+ } | |
+ addColumnValue(cell, builder.toString(), styles); | |
+ } | |
} | |
protected void writeExcelToResponse(HttpServletResponse response, Workbook generatedExcel, String filename) throws IOException { | |
diff --git a/src/main/java/org/primefaces/component/export/Exporter.java b/src/main/java/org/primefaces/component/export/Exporter.java | |
index 3ef08c2..10ace6f 100644 | |
--- a/src/main/java/org/primefaces/component/export/Exporter.java | |
+++ b/src/main/java/org/primefaces/component/export/Exporter.java | |
@@ -58,7 +58,14 @@ public abstract class Exporter { | |
public abstract void export(FacesContext facesContext, DataTable table, | |
String outputFileName, boolean pageOnly, boolean selectionOnly, int[] excludedColumnIndexes, | |
String encodingType, MethodExpression preProcessor, | |
- MethodExpression postProcessor) throws IOException; | |
+ MethodExpression postProcessor, boolean skipConversion) throws IOException; | |
+ | |
+ public void export(FacesContext facesContext, DataTable table, | |
+ String outputFileName, boolean pageOnly, boolean selectionOnly, int[] excludedColumnIndexes, | |
+ String encodingType, MethodExpression preProcessor, | |
+ MethodExpression postProcessor) throws IOException { | |
+ export(facesContext, table, outputFileName, pageOnly, selectionOnly, excludedColumnIndexes, encodingType, preProcessor, postProcessor, false); | |
+ } | |
protected List<UIColumn> getColumnsToExport(UIData table, int[] excludedColumns) { | |
@@ -87,8 +94,13 @@ public abstract class Exporter { | |
return false; | |
} | |
+ | |
+ protected String exportStringValue(FacesContext context, UIComponent component) { | |
+ Object obj = exportValue(context, component, true); | |
+ return obj == null ? "" : obj.toString(); | |
+ } | |
- protected String exportValue(FacesContext context, UIComponent component) { | |
+ protected Object exportValue(FacesContext context, UIComponent component, boolean convert) { | |
if(component instanceof HtmlCommandLink) { //support for PrimeFaces and standard HtmlCommandLink | |
HtmlCommandLink link = (HtmlCommandLink) component; | |
@@ -100,7 +112,7 @@ public abstract class Exporter { | |
//export first value holder | |
for(UIComponent child : link.getChildren()) { | |
if(child instanceof ValueHolder) { | |
- return exportValue(context, child); | |
+ return exportValue(context, child, convert); | |
} | |
} | |
@@ -112,35 +124,37 @@ public abstract class Exporter { | |
if(component instanceof EditableValueHolder) { | |
Object submittedValue = ((EditableValueHolder) component).getSubmittedValue(); | |
if (submittedValue != null) { | |
- return submittedValue.toString(); | |
+ return submittedValue; | |
} | |
} | |
ValueHolder valueHolder = (ValueHolder) component; | |
Object value = valueHolder.getValue(); | |
- if(value == null) | |
- return ""; | |
- | |
- //first ask the converter | |
- if(valueHolder.getConverter() != null) { | |
- return valueHolder.getConverter().getAsString(context, component, value); | |
- } | |
- //Try to guess | |
- else { | |
- ValueExpression expr = component.getValueExpression("value"); | |
- if(expr != null) { | |
- Class<?> valueType = expr.getType(context.getELContext()); | |
- if(valueType != null) { | |
- Converter converterForType = context.getApplication().createConverter(valueType); | |
- | |
- if(converterForType != null) | |
- return converterForType.getAsString(context, component, value); | |
- } | |
- } | |
- } | |
- | |
- //No converter found just return the value as string | |
- return value.toString(); | |
+ if(value == null) { | |
+ return null; | |
+ } | |
+ | |
+ if (convert) { | |
+ //first ask the converter | |
+ if(valueHolder.getConverter() != null) { | |
+ return valueHolder.getConverter().getAsString(context, component, value); | |
+ } | |
+ //Try to guess | |
+ else { | |
+ ValueExpression expr = component.getValueExpression("value"); | |
+ if(expr != null) { | |
+ Class<?> valueType = expr.getType(context.getELContext()); | |
+ if(valueType != null) { | |
+ Converter converterForType = context.getApplication().createConverter(valueType); | |
+ | |
+ if(converterForType != null) | |
+ return converterForType.getAsString(context, component, value); | |
+ } | |
+ } | |
+ } | |
+ } | |
+ | |
+ return value; | |
} else { | |
//This would get the plain texts on UIInstructions when using Facelets | |
String value = component.toString(); | |
diff --git a/src/main/java/org/primefaces/component/export/PDFExporter.java b/src/main/java/org/primefaces/component/export/PDFExporter.java | |
index 6989a04..0db9d7d 100644 | |
--- a/src/main/java/org/primefaces/component/export/PDFExporter.java | |
+++ b/src/main/java/org/primefaces/component/export/PDFExporter.java | |
@@ -40,7 +40,7 @@ import java.lang.reflect.Array; | |
public class PDFExporter extends Exporter { | |
@Override | |
- public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor) throws IOException { | |
+ public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, boolean convert) throws IOException { | |
try { | |
Document document = new Document(); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
@@ -201,7 +201,7 @@ public class PDFExporter extends Exporter { | |
} | |
private void addColumnValue(PdfPTable pdfTable, UIComponent component, Font font) { | |
- String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component); | |
+ String value = component == null ? "" : exportStringValue(FacesContext.getCurrentInstance(), component); | |
pdfTable.addCell(new Paragraph(value, font)); | |
} | |
@@ -211,7 +211,7 @@ public class PDFExporter extends Exporter { | |
for(UIComponent component : components) { | |
if(component.isRendered() ) { | |
- String value = exportValue(FacesContext.getCurrentInstance(), component); | |
+ String value = exportStringValue(FacesContext.getCurrentInstance(), component); | |
if(value != null) | |
builder.append(value); | |
diff --git a/src/main/java/org/primefaces/component/export/XMLExporter.java b/src/main/java/org/primefaces/component/export/XMLExporter.java | |
index acd4078..b0c2889 100644 | |
--- a/src/main/java/org/primefaces/component/export/XMLExporter.java | |
+++ b/src/main/java/org/primefaces/component/export/XMLExporter.java | |
@@ -35,7 +35,7 @@ import org.primefaces.component.datatable.DataTable; | |
public class XMLExporter extends Exporter { | |
@Override | |
- public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor) throws IOException { | |
+ public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, int[] excludeColumns, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, boolean convert) throws IOException { | |
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); | |
response.setContentType("text/xml"); | |
@@ -199,7 +199,7 @@ public class XMLExporter extends Exporter { | |
UIComponent facet = column.getFacet(columnType.facet()); | |
if(facet != null && facet.isRendered()) { | |
- String value = exportValue(FacesContext.getCurrentInstance(), facet); | |
+ String value = exportStringValue(FacesContext.getCurrentInstance(), facet); | |
facets.add(value); | |
} else { | |
@@ -217,7 +217,7 @@ public class XMLExporter extends Exporter { | |
for(UIComponent component : components) { | |
if(component.isRendered()) { | |
- String value = exportValue(FacesContext.getCurrentInstance(), component); | |
+ String value = exportStringValue(FacesContext.getCurrentInstance(), component); | |
builder.append(value); | |
} | |
diff --git a/src/main/resources-maven-jsf/standard-facelets-taglib.xml b/src/main/resources-maven-jsf/standard-facelets-taglib.xml | |
index 5c7ceb5..c16fc0e 100644 | |
--- a/src/main/resources-maven-jsf/standard-facelets-taglib.xml | |
+++ b/src/main/resources-maven-jsf/standard-facelets-taglib.xml | |
@@ -99,8 +99,13 @@ | |
<required>false</required> | |
<type>java.lang.String</type> | |
</attribute> | |
+ <attribute> | |
+ <description>Skip conversion step.</description> | |
+ <name>skipConversion</name> | |
+ <required>false</required> | |
+ <type>java.lang.String</type> | |
+ </attribute> | |
</tag> | |
- | |
<tag> | |
<tag-name>fileDownload</tag-name> | |
<handler-class> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment