Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDirk Fauth2014-11-13 13:35:01 +0000
committerDirk Fauth2014-11-13 13:35:01 +0000
commitdfc9fc51909f209d96dd13b9658f60669dc4c526 (patch)
tree4bf24181af7656473ef36dce115b9176f6387bf6
parent3da88188e36c64e06f77d06f974969d4cf8679d4 (diff)
downloadorg.eclipse.nebula.widgets.nattable-dfc9fc51909f209d96dd13b9658f60669dc4c526.tar.gz
org.eclipse.nebula.widgets.nattable-dfc9fc51909f209d96dd13b9658f60669dc4c526.tar.xz
org.eclipse.nebula.widgets.nattable-dfc9fc51909f209d96dd13b9658f60669dc4c526.zip
Bug 450443 - made charset and sheetname configurable for ExcelExporter
-rw-r--r--org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/ExcelExporter.java115
-rw-r--r--org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/excelExportHeader.txt23
2 files changed, 72 insertions, 66 deletions
diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/ExcelExporter.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/ExcelExporter.java
index 847a75fa..8da26e19 100644
--- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/ExcelExporter.java
+++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/ExcelExporter.java
@@ -1,19 +1,19 @@
/*******************************************************************************
- * Copyright (c) 2012 Original authors and others.
+ * Copyright (c) 2012, 2014 Original authors and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Original authors and others - initial API and implementation
+ * Dirk Fauth <dirk.fauth@googlemail.com> - Bug 450443
******************************************************************************/
package org.eclipse.nebula.widgets.nattable.export.excel;
-import static org.eclipse.nebula.widgets.nattable.util.ObjectUtils.isNotNull;
-
+import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
@@ -41,6 +41,12 @@ public class ExcelExporter implements ILayerExporter {
private static final String EXCEL_HEADER_FILE = "excelExportHeader.txt"; //$NON-NLS-1$
+ private static final String CHARSET_PLACEHOLDER = "${charset}"; //$NON-NLS-1$
+ private static final String SHEETNAME_PLACEHOLDER = "${sheetname}"; //$NON-NLS-1$
+
+ private String charset = "windows-1252"; //$NON-NLS-1$
+ private String sheetname = "Sheet1"; //$NON-NLS-1$
+
/**
* The IOutputStreamProvider that is used to create new OutputStreams on
* beginning new export operations.
@@ -52,15 +58,14 @@ public class ExcelExporter implements ILayerExporter {
* values.
*/
public ExcelExporter() {
- this(
- new FileOutputStreamProvider(
- "table_export.xls", new String[] { "Excel Workbok (*.xls)" }, new String[] { "*.xls" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ this(new FileOutputStreamProvider("table_export.xls", //$NON-NLS-1$
+ new String[] { "Excel Workbok (*.xls)" }, new String[] { "*.xls" })); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Creates a new ExcelExporter that uses the given IOutputStreamProvider for
* retrieving the OutputStream to write the export to.
- *
+ *
* @param outputStreamProvider
* The IOutputStreamProvider that is used to retrieve the
* OutputStream to write the export to.
@@ -71,7 +76,7 @@ public class ExcelExporter implements ILayerExporter {
@Override
public OutputStream getOutputStream(Shell shell) {
- return outputStreamProvider.getOutputStream(shell);
+ return this.outputStreamProvider.getOutputStream(shell);
}
@Override
@@ -81,8 +86,7 @@ public class ExcelExporter implements ILayerExporter {
public void exportEnd(OutputStream outputStream) throws IOException {}
@Override
- public void exportLayerBegin(OutputStream outputStream, String layerName)
- throws IOException {
+ public void exportLayerBegin(OutputStream outputStream, String layerName) throws IOException {
writeHeader(outputStream);
outputStream.write(asBytes("<body><table border='1'>")); //$NON-NLS-1$
}
@@ -90,44 +94,49 @@ public class ExcelExporter implements ILayerExporter {
/**
* Writes the Excel header informations that are stored locally in the
* package structure.
- *
+ *
* @throws IOException
* if an I/O error occurs on closing the stream to the header
* content file
*/
private void writeHeader(OutputStream outputStream) throws IOException {
- InputStream headerStream = null;
+ BufferedReader reader = null;
try {
- headerStream = this.getClass().getResourceAsStream(
- EXCEL_HEADER_FILE);
- int c;
- while ((c = headerStream.read()) != -1) {
- outputStream.write(c);
+ reader = new BufferedReader(
+ new InputStreamReader(
+ this.getClass().getResourceAsStream(EXCEL_HEADER_FILE)));
+
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ // don not export comments in export header template
+ if (!line.startsWith("#")) { //$NON-NLS-1$
+ line = line.replace(CHARSET_PLACEHOLDER, this.charset);
+ line = line.replace(SHEETNAME_PLACEHOLDER, this.sheetname);
+ outputStream.write(line.getBytes());
+ outputStream.write(System.getProperty("line.separator").getBytes()); //$NON-NLS-1$
+ }
}
} catch (Exception e) {
log.error("Excel Exporter failed: " + e.getMessage(), e); //$NON-NLS-1$
} finally {
- if (isNotNull(headerStream)) {
- headerStream.close();
+ if (reader != null) {
+ reader.close();
}
}
}
@Override
- public void exportLayerEnd(OutputStream outputStream, String layerName)
- throws IOException {
+ public void exportLayerEnd(OutputStream outputStream, String layerName) throws IOException {
outputStream.write(asBytes("</table></body></html>")); //$NON-NLS-1$
}
@Override
- public void exportRowBegin(OutputStream outputStream, int rowPosition)
- throws IOException {
+ public void exportRowBegin(OutputStream outputStream, int rowPosition) throws IOException {
outputStream.write(asBytes("<tr>\n")); //$NON-NLS-1$
}
@Override
- public void exportRowEnd(OutputStream outputStream, int rowPosition)
- throws IOException {
+ public void exportRowEnd(OutputStream outputStream, int rowPosition) throws IOException {
outputStream.write(asBytes("</tr>\n")); //$NON-NLS-1$
}
@@ -135,30 +144,26 @@ public class ExcelExporter implements ILayerExporter {
public void exportCell(OutputStream outputStream,
Object exportDisplayValue, ILayerCell cell,
IConfigRegistry configRegistry) throws IOException {
- CellStyleProxy cellStyle = new CellStyleProxy(configRegistry,
- DisplayMode.NORMAL, cell.getConfigLabels().getLabels());
- Color fg = cellStyle
- .getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR);
- Color bg = cellStyle
- .getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR);
+ CellStyleProxy cellStyle = new CellStyleProxy(
+ configRegistry,
+ DisplayMode.NORMAL,
+ cell.getConfigLabels().getLabels());
+ Color fg = cellStyle.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR);
+ Color bg = cellStyle.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR);
Font font = cellStyle.getAttributeValue(CellStyleAttributes.FONT);
- String htmlAttributes = String.format(
- "style='color: %s; background-color: %s; %s;'", //$NON-NLS-1$
+ String htmlAttributes = String.format("style='color: %s; background-color: %s; %s;'", //$NON-NLS-1$
getColorInCSSFormat(fg), getColorInCSSFormat(bg),
getFontInCSSFormat(font));
- String htmlText = exportDisplayValue != null ? exportDisplayValue
- .toString() : ""; //$NON-NLS-1$
+ String htmlText = exportDisplayValue != null ? exportDisplayValue.toString() : ""; //$NON-NLS-1$
if (htmlText.startsWith(" ")) { //$NON-NLS-1$
htmlAttributes += " x:str=\"'" + htmlText + "\";"; //$NON-NLS-1$ //$NON-NLS-2$
- htmlText = htmlText.replaceFirst(
- "^(\\ *)", "<span style='mso-spacerun:yes'>$1</span>"); //$NON-NLS-1$ //$NON-NLS-2$
+ htmlText = htmlText.replaceFirst("^(\\ *)", "<span style='mso-spacerun:yes'>$1</span>"); //$NON-NLS-1$ //$NON-NLS-2$
}
- outputStream.write(asBytes(String.format(
- "\t<td %s>%s</td>\n", htmlAttributes, htmlText))); //$NON-NLS-1$
+ outputStream.write(asBytes(String.format("\t<td %s>%s</td>\n", htmlAttributes, htmlText))); //$NON-NLS-1$
}
private byte[] asBytes(String string) {
@@ -171,15 +176,13 @@ public class ExcelExporter implements ILayerExporter {
int fontStyle = fontData.getStyle();
String HTML_STYLES[] = new String[] { "NORMAL", "BOLD", "ITALIC" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- return String.format(
- "font: %s; font-family: %s", //$NON-NLS-1$
+ return String.format("font: %s; font-family: %s", //$NON-NLS-1$
fontStyle <= 2 ? HTML_STYLES[fontStyle] : HTML_STYLES[0],
- fontName);
+ fontName);
}
private String getColorInCSSFormat(Color color) {
- return String.format(
- "rgb(%d,%d,%d)", //$NON-NLS-1$
+ return String.format("rgb(%d,%d,%d)", //$NON-NLS-1$
Integer.valueOf(color.getRed()),
Integer.valueOf(color.getGreen()),
Integer.valueOf(color.getBlue()));
@@ -187,7 +190,25 @@ public class ExcelExporter implements ILayerExporter {
@Override
public Object getResult() {
- return outputStreamProvider.getResult();
+ return this.outputStreamProvider.getResult();
+ }
+
+ /**
+ * @param charset
+ * The charset that should be used as replacement for the charset
+ * value in the export header. Default is <i>windows-1252</i>
+ */
+ public void setCharset(String charset) {
+ this.charset = charset;
}
+ /**
+ *
+ * @param sheetname
+ * The name that should be set as sheet name in the resulting
+ * Excel file. Default is <i>Sheet1</i>
+ */
+ public void setSheetname(String sheetname) {
+ this.sheetname = sheetname;
+ }
}
diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/excelExportHeader.txt b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/excelExportHeader.txt
index c1c55ab1..dc972f73 100644
--- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/excelExportHeader.txt
+++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/export/excel/excelExportHeader.txt
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2012 Original authors and others.
+# Copyright (c) 2012, 2014 Original authors and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
@@ -7,20 +7,21 @@
#
# Contributors:
# Original authors and others - initial API and implementation
+# Dirk Fauth <dirk.fauth@googlemail.com> - Bug 450443
#-------------------------------------------------------------------------------
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
-<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
+<meta http-equiv=Content-Type content="text/html; charset=${charset}">
<meta name=ProgId content=Excel.Sheet>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
- <x:Name>Sheet1</x:Name>
+ <x:Name>${sheetname}</x:Name>
<x:WorksheetOptions>
<x:Selected/>
<x:Panes>
@@ -35,22 +36,6 @@ xmlns="http://www.w3.org/TR/REC-html40">
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
- <x:ExcelWorksheet>
- <x:Name>Sheet2</x:Name>
- <x:WorksheetOptions>
- <x:ProtectContents>False</x:ProtectContents>
- <x:ProtectObjects>False</x:ProtectObjects>
- <x:ProtectScenarios>False</x:ProtectScenarios>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- <x:ExcelWorksheet>
- <x:Name>Sheet3</x:Name>
- <x:WorksheetOptions>
- <x:ProtectContents>False</x:ProtectContents>
- <x:ProtectObjects>False</x:ProtectObjects>
- <x:ProtectScenarios>False</x:ProtectScenarios>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>8895</x:WindowHeight>
<x:WindowWidth>13980</x:WindowWidth>

Back to the top