Skip to content

Commit

Permalink
Impl #111 - [Export] Add support for exporting to XLSX
Browse files Browse the repository at this point in the history
  • Loading branch information
fipro78 committed Aug 12, 2024
1 parent 52ead53 commit 5d54568
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 7 deletions.
16 changes: 14 additions & 2 deletions org.eclipse.nebula.widgets.nattable.examples/build-examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
src="lib/com.zaxxer.sparsebits_1.3.0.v20230929-1000.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.commons-codec_1.17.0.jar"/>
src="lib/org.apache.commons.commons-codec_1.17.1.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.commons-collections4_4.4.0.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.commons-compress_1.26.2.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.commons-io_2.16.1.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.lang3_3.14.0.jar"/>
src="lib/org.apache.commons.lang3_3.15.0.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.commons.logging_1.2.0.v20180409-1502.jar"/>
Expand All @@ -55,6 +58,15 @@
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.poi_5.3.0.v20240703-1000.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.poi.ooxml_5.3.0.v20240703-1000.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.poi.ooxml.schemas_5.3.0.v20240703-1000.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.apache.xmlbeans_5.2.1.v20240527-0800.jar"/>
<zipfileset
excludes="about_files/*,META-INF/*.SF,META-INF/*.inf,META-INF/*.RSA,*"
src="lib/org.eclipse.collections_11.1.0.v20220705-1455.jar"/>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2013, 2020 Dirk Fauth and others.
* Copyright (c) 2013, 2024 Dirk Fauth and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -29,9 +29,11 @@
import org.eclipse.nebula.widgets.nattable.export.ExportConfigAttributes;
import org.eclipse.nebula.widgets.nattable.export.IExportFormatter;
import org.eclipse.nebula.widgets.nattable.export.command.ExportCommand;
import org.eclipse.nebula.widgets.nattable.export.csv.CsvExporter;
import org.eclipse.nebula.widgets.nattable.export.image.config.DefaultImageExportBindings;
import org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter;
import org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter;
import org.eclipse.nebula.widgets.nattable.extension.poi.XSSFExcelExporter;
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultBodyDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
Expand All @@ -58,6 +60,7 @@
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
Expand Down Expand Up @@ -89,7 +92,7 @@ public Control createExampleControl(Composite parent) {
GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);

Composite buttonPanel = new Composite(panel, SWT.NONE);
buttonPanel.setLayout(new GridLayout());
buttonPanel.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);

// property names of the Person class
Expand Down Expand Up @@ -207,18 +210,53 @@ public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {

GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);

Button addColumnButton = new Button(buttonPanel, SWT.PUSH);
addColumnButton.setText("Export");
addColumnButton.addSelectionListener(new SelectionAdapter() {
Button exportCsvButton = new Button(buttonPanel, SWT.PUSH);
exportCsvButton.setText("Export CSV");
exportCsvButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(
new ExportCommand(
natTable.getConfigRegistry(),
natTable.getShell(),
false,
false,
new CsvExporter()));
}
});

Button exportXlsButton = new Button(buttonPanel, SWT.PUSH);
exportXlsButton.setText("Export XLS");
exportXlsButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(
// the default uses the HSSFExcelExporter from the
// ConfigRegistry
new ExportCommand(
natTable.getConfigRegistry(),
natTable.getShell()));
}
});

Button exportXlsxButton = new Button(buttonPanel, SWT.PUSH);
exportXlsxButton.setText("Export XLSX");
exportXlsxButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
PoiExcelExporter exporter = new XSSFExcelExporter();
exporter.setApplyVerticalTextConfiguration(true);
exporter.setApplyBackgroundColor(false);
natTable.doCommand(
new ExportCommand(
natTable.getConfigRegistry(),
natTable.getShell(),
false,
false,
exporter));
}
});

return panel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,20 @@
id="org.apache.commons.commons-io"
version="0.0.0"/>

<plugin
id="org.apache.commons.commons-compress"
version="0.0.0"/>

<plugin
id="org.apache.poi.ooxml"
version="0.0.0"/>

<plugin
id="org.apache.poi.ooxml.schemas"
version="0.0.0"/>

<plugin
id="org.apache.xmlbeans"
version="0.0.0"/>

</feature>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ Bundle-Version: 2.5.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Export-Package: org.eclipse.nebula.widgets.nattable.extension.poi;version="2.5.0"
Import-Package: com.zaxxer.sparsebits;version="[1.3.0,2.0.0)",
org.apache.commons.compress.archivers.zip;version="[1.26.0,2.0.0)",
org.apache.commons.io.output;version="[2.16.0,3.0.0)",
org.apache.logging.log4j;version="[2.20.0,3.0.0)",
org.apache.poi;version="[5.3.0,6.0.0)",
org.apache.poi.hssf.usermodel;version="[5.0.0,6.0.0)",
org.apache.poi.ss.usermodel;version="[5.0.0,6.0.0)",
org.apache.poi.ss.util;version="[5.0.0,6.0.0)",
org.apache.poi.util;version="[5.0.0,6.0.0)",
org.apache.poi.xssf.usermodel;version="[5.3.0,6.0.0)",
org.apache.xmlbeans;version="[5.2.0,6.0.0)",
org.eclipse.nebula.widgets.nattable.config;version="[2.5.0,3.0.0)",
org.eclipse.nebula.widgets.nattable.export;version="[2.5.0,3.0.0)",
org.eclipse.nebula.widgets.nattable.formula;version="[2.5.0,3.0.0)",
Expand All @@ -23,6 +27,7 @@ Import-Package: com.zaxxer.sparsebits;version="[1.3.0,2.0.0)",
org.eclipse.swt,
org.eclipse.swt.graphics,
org.eclipse.swt.widgets,
org.openxmlformats.schemas.spreadsheetml.x2006.main;version="[5.0.0,6.0.0)",
org.slf4j;version="1.7.2"
Bundle-Vendor: Eclipse Nebula NatTable
Automatic-Module-Name: org.eclipse.nebula.widgets.nattable.extension.poi
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2024 Dirk Fauth.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Dirk Fauth <dirk.fauth@googlemail.com> - initial API and implementation
******************************************************************************/
package org.eclipse.nebula.widgets.nattable.extension.poi;

import java.util.HashMap;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.nebula.widgets.nattable.export.FileOutputStreamProvider;
import org.eclipse.nebula.widgets.nattable.export.IOutputStreamProvider;
import org.eclipse.swt.graphics.Color;

public class XSSFExcelExporter extends PoiExcelExporter {

private HashMap<Color, XSSFColor> colorIndex = new HashMap<>();

public XSSFExcelExporter() {
super(new FileOutputStreamProvider("table_export.xlsx", new String[] { "Excel Workbook (*.xlsx)" }, new String[] { "*.xlsx" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

public XSSFExcelExporter(IOutputStreamProvider outputStreamProvider) {
super(outputStreamProvider);
}

@Override
protected Workbook createWorkbook() {
this.colorIndex = new HashMap<>();
return new XSSFWorkbook();
}

@Override
protected void setFillForegroundColor(CellStyle xlCellStyle, Color swtColor) {
xlCellStyle.setFillForegroundColor(getColor(swtColor));
}

@Override
protected void setFontColor(Font xlFont, Color swtColor) {
((XSSFFont) xlFont).setColor(getColor(swtColor));
}

private XSSFColor getColor(Color swtColor) {
return this.colorIndex.computeIfAbsent(swtColor, c -> {
byte[] rgb = {
(byte) swtColor.getRed(),
(byte) swtColor.getGreen(),
(byte) swtColor.getBlue()
};
XSSFColor xc = new XSSFColor(rgb, null);
return xc;
});
}

}

0 comments on commit 5d54568

Please sign in to comment.