Bug 581562 - Numbers are not exported to Excel with the correct format
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
Change-Id: I0ac920c3f71e9a889901c966ebac5642002b641c
diff --git a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_800_Integration/_811_GroupBySummaryFixedSummaryRowExample.java b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_800_Integration/_811_GroupBySummaryFixedSummaryRowExample.java
index 7c9fdcb..d0c7355 100644
--- a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_800_Integration/_811_GroupBySummaryFixedSummaryRowExample.java
+++ b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_800_Integration/_811_GroupBySummaryFixedSummaryRowExample.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2014, 2021 Dirk Fauth and others.
+ * Copyright (c) 2014, 2023 Dirk Fauth and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -36,6 +36,7 @@
import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample;
import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner;
import org.eclipse.nebula.widgets.nattable.export.ExportConfigAttributes;
+import org.eclipse.nebula.widgets.nattable.export.IExportFormatter;
import org.eclipse.nebula.widgets.nattable.export.action.ExportAction;
import org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler;
import org.eclipse.nebula.widgets.nattable.export.excel.DefaultExportFormatter;
@@ -49,11 +50,13 @@
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByHeaderLayer;
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByHeaderMenuConfiguration;
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByModel;
+import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.GroupByObject;
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.ModernGroupByThemeExtension;
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.summary.IGroupBySummaryProvider;
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.summary.SummationGroupBySummaryProvider;
import org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter;
import org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter;
+import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultSummaryRowHeaderDataProvider;
@@ -71,6 +74,7 @@
import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
import org.eclipse.nebula.widgets.nattable.layer.cell.AbstractOverrider;
import org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator;
+import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter;
import org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter;
@@ -277,8 +281,58 @@
configRegistry.registerConfigAttribute(
ExportConfigAttributes.EXPORT_FORMATTER,
new DefaultExportFormatter());
+
+ IExportFormatter formatter = new IExportFormatter() {
+ @Override
+ public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
+ // simply return the data value doing this avoids
+ // the default conversion to string for export
+ return cell.getDataValue();
+ }
+ };
configRegistry.registerConfigAttribute(
- ExportConfigAttributes.DATE_FORMAT, "m/d/yy h:mm"); //$NON-NLS-1$
+ ExportConfigAttributes.EXPORT_FORMATTER,
+ formatter,
+ DisplayMode.NORMAL,
+ GridRegion.ROW_HEADER);
+
+ configRegistry.registerConfigAttribute(
+ ExportConfigAttributes.EXPORT_FORMATTER,
+ formatter,
+ DisplayMode.NORMAL,
+ ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
+
+ configRegistry.registerConfigAttribute(
+ ExportConfigAttributes.EXPORT_FORMATTER,
+ formatter,
+ DisplayMode.NORMAL,
+ ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
+
+ configRegistry.registerConfigAttribute(
+ ExportConfigAttributes.EXPORT_FORMATTER,
+ new IExportFormatter() {
+ @Override
+ public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
+ if (cell.getDataValue() instanceof GroupByObject) {
+ return null;
+ }
+ return cell.getDataValue();
+ }
+ },
+ DisplayMode.NORMAL,
+ ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
+ configRegistry.registerConfigAttribute(
+ ExportConfigAttributes.DATE_FORMAT,
+ "m/d/yy", //$NON-NLS-1$
+ DisplayMode.NORMAL,
+ ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
+
+ configRegistry.registerConfigAttribute(
+ ExportConfigAttributes.EXPORT_FORMATTER,
+ formatter,
+ DisplayMode.NORMAL,
+ SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
+
}
@Override
@@ -322,9 +376,11 @@
DisplayMode.NORMAL,
ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
+ DefaultDoubleDisplayConverter doubleDisplayConverter = new DefaultDoubleDisplayConverter();
+ doubleDisplayConverter.setMinimumFractionDigits(2);
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,
- new DefaultDoubleDisplayConverter(),
+ doubleDisplayConverter,
DisplayMode.NORMAL,
ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
diff --git a/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java b/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java
index cb4434b..a8fa729 100644
--- a/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java
+++ b/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012, 2020 Original authors and others.
+ * Copyright (c) 2012, 2023 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -212,11 +212,7 @@
Color bg = cellStyle.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR);
org.eclipse.swt.graphics.Font font = cellStyle.getAttributeValue(CellStyleAttributes.FONT);
FontData fontData = font.getFontData()[0];
- String dataFormat = null;
- if (exportDisplayValue instanceof Calendar
- || exportDisplayValue instanceof Date) {
- dataFormat = getDataFormatString(cell, configRegistry);
- }
+ String dataFormat = getDataFormatString(exportDisplayValue, cell, configRegistry);
int hAlign = HorizontalAlignmentEnum.getSWTStyle(cellStyle);
int vAlign = VerticalAlignmentEnum.getSWTStyle(cellStyle);
@@ -402,6 +398,40 @@
}
/**
+ * @param exportDisplayValue
+ * The value that should be exported.
+ * @param cell
+ * The cell for which the data format needs to be determined.
+ * @param configRegistry
+ * The ConfigRegistry needed to retrieve the configuration.
+ * @return The data format that should be used to format values in the
+ * export.
+ * @since 2.1
+ */
+ protected String getDataFormatString(Object exportDisplayValue, ILayerCell cell, IConfigRegistry configRegistry) {
+ String dataFormat = null;
+ if (exportDisplayValue instanceof Calendar
+ || exportDisplayValue instanceof Date) {
+ dataFormat = getDataFormatString(cell, configRegistry);
+ } else {
+ dataFormat = configRegistry.getConfigAttribute(
+ ExportConfigAttributes.DATE_FORMAT,
+ DisplayMode.NORMAL,
+ cell.getConfigLabels());
+
+ if (dataFormat == null) {
+ if (exportDisplayValue instanceof Integer) {
+ return "0"; //$NON-NLS-1$
+ } else if (exportDisplayValue instanceof Double) {
+ return "0.00"; //$NON-NLS-1$
+ }
+ }
+ }
+
+ return dataFormat;
+ }
+
+ /**
*
* @param cell
* The cell for which the date format needs to be determined.