PageRenderTime 153ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/HSSFExcelExporter.java

https://github.com/apauzies/nebula.widgets.nattable
Java | 72 lines | 43 code | 15 blank | 14 comment | 1 complexity | 5dc0bfccf555b3239b3954a8211284c2 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2013 Original authors and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Original authors and others - initial API and implementation
  10. ******************************************************************************/
  11. package org.eclipse.nebula.widgets.nattable.extension.poi;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import org.apache.poi.hssf.usermodel.HSSFPalette;
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. import org.apache.poi.ss.usermodel.CellStyle;
  17. import org.apache.poi.ss.usermodel.Font;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.eclipse.nebula.widgets.nattable.export.FileOutputStreamProvider;
  20. import org.eclipse.nebula.widgets.nattable.export.IOutputStreamProvider;
  21. import org.eclipse.swt.graphics.Color;
  22. public class HSSFExcelExporter extends PoiExcelExporter {
  23. private List<Color> colorIndex = new ArrayList<Color>();
  24. public HSSFExcelExporter() {
  25. super(new FileOutputStreamProvider("table_export.xls", new String[] { "Excel Workbook (*.xls)" }, new String[] { "*.xls" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  26. }
  27. public HSSFExcelExporter(IOutputStreamProvider outputStreamProvider) {
  28. super(outputStreamProvider);
  29. }
  30. @Override
  31. protected Workbook createWorkbook() {
  32. colorIndex = new ArrayList<Color>();
  33. return new HSSFWorkbook();
  34. }
  35. protected void setFillForegroundColor(CellStyle xlCellStyle, Color swtColor) {
  36. xlCellStyle.setFillForegroundColor(getColorIndex(swtColor));
  37. }
  38. protected void setFontColor(Font xlFont, Color swtColor) {
  39. xlFont.setColor(getColorIndex(swtColor));
  40. }
  41. /**
  42. * Note: The Excel HSSF format only supports a maximum of 56 custom colors. If you have more than that number of colors,
  43. * bad things will happen when you try to export.
  44. */
  45. private short getColorIndex(Color swtColor) {
  46. if (!colorIndex.contains(swtColor)) {
  47. colorIndex.add(swtColor);
  48. HSSFPalette palette = ((HSSFWorkbook) xlWorkbook).getCustomPalette();
  49. palette.setColorAtIndex((short) (55 - colorIndex.indexOf(swtColor)),
  50. (byte) swtColor.getRed(),
  51. (byte) swtColor.getGreen(),
  52. (byte) swtColor.getBlue()
  53. );
  54. }
  55. return (short) (55 - colorIndex.indexOf(swtColor));
  56. }
  57. }