Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7166: Excel export currency cells #7169

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.Color;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import javax.faces.component.UIComponent;
import javax.faces.component.UIPanel;
Expand All @@ -46,10 +47,7 @@
import org.primefaces.component.export.ExcelOptions;
import org.primefaces.component.export.ExportConfiguration;
import org.primefaces.component.export.ExporterOptions;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.Constants;
import org.primefaces.util.LangUtils;

import org.primefaces.util.*;

public class DataTableExcelExporter extends DataTableExporter {

Expand All @@ -59,7 +57,9 @@ public class DataTableExcelExporter extends DataTableExporter {
private CellStyle cellStyleCenterAlign;
private CellStyle cellStyleLeftAlign;
private CellStyle facetStyle;
private CellStyle currencyStyle;
private boolean stronglyTypedCells;
private Locale locale;

@Override
protected void preExport(FacesContext context, ExportConfiguration exportConfiguration) throws IOException {
Expand Down Expand Up @@ -89,6 +89,9 @@ public void doExport(FacesContext context, DataTable table, ExportConfiguration
else {
stronglyTypedCells = options.isStronglyTypedCells();
}
if (stronglyTypedCells) {
locale = LocaleUtils.getCurrentLocale(context);
}
Sheet sheet = createSheet(wb, sheetName, options);
applyOptions(wb, table, sheet, options);
exportTable(context, table, sheet, exportConfiguration);
Expand Down Expand Up @@ -339,17 +342,37 @@ protected int calculateColumnOffset(Sheet sheet, int row, int col) {
}

/**
* If ExcelOptions.isStronglyTypedCells = true then for cells that are all numbers make them a numeric cell
* instead of a String cell. Possible future enhancement of Date cells as well.
* If ExcelOptions.isStronglyTypedCells = true then for cells check:
* <pre>
* Numeric - String that are all numbers make them a numeric cell
* Currency - Convert to currency cell so math can be done in Excel
* String - fallback to just a normal string cell
* </pre>
* Possible future enhancement of Date cells as well.
*
* @param cell the cell to operate on
* @param value the String value to put in the cell
*/
protected void updateCell(Cell cell, String value) {
if (stronglyTypedCells && LangUtils.isNumeric(value)) {
cell.setCellValue(Double.parseDouble(value));
boolean printed = false;
if (stronglyTypedCells) {
if (LangUtils.isNumeric(value)) {
cell.setCellValue(Double.parseDouble(value));
printed = true;
}

if (!printed) {
Number currency = CurrencyValidator.getInstance().validate(value, locale);
if (currency != null) {
cell.setCellValue(currency.doubleValue());
cell.setCellStyle(currencyStyle);
printed = true;
}
}
}
else {

// fall back to just printing the string value
if (!printed) {
cell.setCellValue(createRichTextString(value));
}
}
Expand Down Expand Up @@ -432,6 +455,16 @@ protected void applyOptions(Workbook wb, DataTable table, Sheet sheet, ExporterO
cellStyleRightAlign.setAlignment(HorizontalAlignment.RIGHT);
applyCellOptions(wb, options, cellStyleRightAlign);

if (stronglyTypedCells) {
currencyStyle = wb.createCellStyle();
currencyStyle.setFont(font);
currencyStyle.setAlignment(HorizontalAlignment.RIGHT);
String pattern = CurrencyValidator.getInstance().getPattern(locale);
short currencyPattern = wb.getCreationHelper().createDataFormat().getFormat(pattern);
currencyStyle.setDataFormat(currencyPattern);
applyCellOptions(wb, options, currencyStyle);
}

PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
Expand Down Expand Up @@ -511,6 +544,10 @@ protected void applyCellOptions(Workbook wb, ExporterOptions options, CellStyle
}

protected Cell applyColumnAlignments(UIColumn column, Cell cell) {
if (cell.getCellStyle() != null) {
// don't apply style if cell already has one
return cell;
}
String[] styles = new String[] {column.getStyle(), column.getStyleClass()};
if (LangUtils.containsIgnoreCase(styles, "right")) {
cell.setCellStyle(cellStyleRightAlign);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.Color;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import javax.faces.component.UIComponent;
Expand Down Expand Up @@ -57,9 +58,7 @@
import org.primefaces.component.export.ExportConfiguration;
import org.primefaces.component.export.ExporterOptions;
import org.primefaces.component.treetable.TreeTable;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.Constants;
import org.primefaces.util.LangUtils;
import org.primefaces.util.*;


public class TreeTableExcelExporter extends TreeTableExporter {
Expand All @@ -70,7 +69,9 @@ public class TreeTableExcelExporter extends TreeTableExporter {
private CellStyle cellStyleCenterAlign;
private CellStyle cellStyleLeftAlign;
private CellStyle facetStyle;
private CellStyle currencyStyle;
private boolean stronglyTypedCells;
private Locale locale;

@Override
protected void preExport(FacesContext context, ExportConfiguration exportConfiguration) throws IOException {
Expand Down Expand Up @@ -100,6 +101,9 @@ public void doExport(FacesContext context, TreeTable table, ExportConfiguration
else {
stronglyTypedCells = options.isStronglyTypedCells();
}
if (stronglyTypedCells) {
locale = LocaleUtils.getCurrentLocale(context);
}
Sheet sheet = createSheet(wb, sheetName, options);
applyOptions(wb, table, sheet, options);
exportTable(context, table, sheet, exportConfiguration);
Expand Down Expand Up @@ -352,17 +356,37 @@ protected int calculateColumnOffset(Sheet sheet, int row, int col) {
}

/**
* If ExcelOptions.isStronglyTypedCells = true then for cells that are all numbers make them a numeric cell
* instead of a String cell. Possible future enhancement of Date cells as well.
* If ExcelOptions.isStronglyTypedCells = true then for cells check:
* <pre>
* Numeric - String that are all numbers make them a numeric cell
* Currency - Convert to currency cell so math can be done in Excel
* String - fallback to just a normal string cell
* </pre>
* Possible future enhancement of Date cells as well.
*
* @param cell the cell to operate on
* @param value the String value to put in the cell
*/
protected void updateCell(Cell cell, String value) {
if (stronglyTypedCells && LangUtils.isNumeric(value)) {
cell.setCellValue(Double.parseDouble(value));
boolean printed = false;
if (stronglyTypedCells) {
if (LangUtils.isNumeric(value)) {
cell.setCellValue(Double.parseDouble(value));
printed = true;
}

if (!printed) {
Number currency = CurrencyValidator.getInstance().validate(value, locale);
if (currency != null) {
cell.setCellValue(currency.doubleValue());
cell.setCellStyle(currencyStyle);
printed = true;
}
}
}
else {

// fall back to just printing the string value
if (!printed) {
cell.setCellValue(createRichTextString(value));
}
}
Expand Down Expand Up @@ -443,6 +467,16 @@ protected void applyOptions(Workbook wb, TreeTable table, Sheet sheet, ExporterO
cellStyleRightAlign.setAlignment(HorizontalAlignment.RIGHT);
applyCellOptions(wb, options, cellStyleRightAlign);

if (stronglyTypedCells) {
currencyStyle = wb.createCellStyle();
currencyStyle.setFont(font);
currencyStyle.setAlignment(HorizontalAlignment.RIGHT);
String pattern = CurrencyValidator.getInstance().getPattern(locale);
short currencyPattern = wb.getCreationHelper().createDataFormat().getFormat(pattern);
currencyStyle.setDataFormat(currencyPattern);
applyCellOptions(wb, options, currencyStyle);
}

PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
Expand Down Expand Up @@ -522,6 +556,10 @@ protected void applyCellOptions(Workbook wb, ExporterOptions options, CellStyle
}

protected Cell applyColumnAlignments(UIColumn column, Cell cell) {
if (cell.getCellStyle() != null) {
// don't apply style if cell already has one
return cell;
}
String[] styles = new String[] {column.getStyle(), column.getStyleClass()};
if (LangUtils.containsIgnoreCase(styles, "right")) {
cell.setCellStyle(cellStyleRightAlign);
Expand Down