PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/sysmodb/xml/HSSFXMLStyleHelper.java

https://github.com/myGrid/simple-spreadsheet-extractor
Java | 124 lines | 92 code | 18 blank | 14 comment | 38 complexity | be615e38ff41ea583755ef64473a93a3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2009-2013, University of Manchester
  3. *
  4. * Licensed under the New BSD License.
  5. * Please see LICENSE file that is distributed with the source code
  6. ******************************************************************************/
  7. package org.sysmodb.xml;
  8. import javax.xml.stream.XMLStreamException;
  9. import javax.xml.stream.XMLStreamWriter;
  10. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  11. import org.apache.poi.hssf.usermodel.HSSFFont;
  12. import org.apache.poi.hssf.usermodel.HSSFPalette;
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  14. import org.apache.poi.hssf.util.HSSFColor;
  15. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  16. import org.apache.poi.ss.usermodel.CellStyle;
  17. /**
  18. *
  19. * @author Finn, Stuart Owen
  20. */
  21. public class HSSFXMLStyleHelper implements XMLStyleHelper {
  22. private static HSSFWorkbook workbook;
  23. private static HSSFPalette palette;
  24. private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();
  25. public HSSFXMLStyleHelper(HSSFWorkbook wb) {
  26. workbook = wb;
  27. palette = wb.getCustomPalette();
  28. }
  29. public String getBGColour(CellStyle style) {
  30. return getRGBString(style.getFillForegroundColor());
  31. }
  32. public boolean areFontsEmpty(CellStyle style) {
  33. HSSFCellStyle newStyle = (HSSFCellStyle) style;
  34. HSSFFont font = newStyle.getFont(workbook);
  35. if (font.getBold())
  36. return false;
  37. if (font.getItalic())
  38. return false;
  39. if (font.getUnderline() != HSSFFont.U_NONE)
  40. return false;
  41. // Ignore same-ish defaults
  42. if (font.getFontHeightInPoints() != 10 && font.getFontHeightInPoints() != 11)
  43. return false;
  44. // Arial is default for Excel, Calibri is default for OO
  45. if (!font.getFontName().equals("Arial") && !font.getFontName().equals("Calibri"))
  46. return false;
  47. if ((font.getColor() != HSSFFont.COLOR_NORMAL) && (getRGBString(font.getColor()) != null)
  48. && !getRGBString(font.getColor()).equals("#000"))
  49. return false;
  50. return true;
  51. }
  52. @Override
  53. public void writeFontProperties(XMLStreamWriter xmlWriter, CellStyle style) throws XMLStreamException {
  54. HSSFCellStyle newStyle = (HSSFCellStyle) style;
  55. HSSFFont font = newStyle.getFont(workbook);
  56. if (font.getBold()) {
  57. xmlWriter.writeStartElement("font-weight");
  58. xmlWriter.writeCharacters("bold");
  59. xmlWriter.writeEndElement();
  60. }
  61. if (font.getItalic()) {
  62. xmlWriter.writeStartElement("font-style");
  63. xmlWriter.writeCharacters("italics");
  64. xmlWriter.writeEndElement();
  65. }
  66. if (font.getUnderline() != HSSFFont.U_NONE) {
  67. xmlWriter.writeStartElement("text-decoration");
  68. xmlWriter.writeCharacters("underline");
  69. xmlWriter.writeEndElement();
  70. }
  71. // Ignore same-ish defaults
  72. if (font.getFontHeightInPoints() != 10 && font.getFontHeightInPoints() != 11) {
  73. xmlWriter.writeStartElement("font-size");
  74. xmlWriter.writeCharacters(String.valueOf(font.getFontHeightInPoints() + "pt"));
  75. xmlWriter.writeEndElement();
  76. }
  77. // Arial is default for Excel, Calibri is default for OO
  78. if (!font.getFontName().equals("Arial") && !font.getFontName().equals("Calibri")) {
  79. xmlWriter.writeStartElement("font-family");
  80. xmlWriter.writeCharacters(font.getFontName());
  81. xmlWriter.writeEndElement();
  82. }
  83. if ((font.getColor() != HSSFFont.COLOR_NORMAL) && (getRGBString(font.getColor()) != null)
  84. && !getRGBString(font.getColor()).equals("#000")) {
  85. xmlWriter.writeStartElement("color");
  86. xmlWriter.writeCharacters(getRGBString(font.getColor()));
  87. xmlWriter.writeEndElement();
  88. }
  89. }
  90. private String getRGBString(short index) {
  91. String string = null;
  92. HSSFColor color = palette.getColor(index);
  93. if (index == HSSF_AUTO.getIndex() || color == null) {
  94. } else {
  95. short[] rgb = color.getTriplet();
  96. string = "#";
  97. for (int i = 0; i <= 2; i++) {
  98. String colourSection = Integer.toHexString((int) rgb[i]);
  99. if (colourSection.length() == 1)
  100. colourSection = "0" + colourSection;
  101. string += colourSection;
  102. }
  103. }
  104. return string;
  105. }
  106. }