PageRenderTime 81ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/sysmodb/HSSFStyleHelper.java

https://github.com/fbacall/simple-spreadsheet-extractor
Java | 81 lines | 60 code | 15 blank | 6 comment | 21 complexity | cd0ae220218a70d51bdfd27ee616f9a1 MD5 | raw file
  1. package org.sysmodb;
  2. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  3. import org.apache.poi.hssf.usermodel.HSSFFont;
  4. import org.apache.poi.hssf.usermodel.HSSFPalette;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.hssf.util.HSSFColor;
  7. import org.apache.poi.ss.usermodel.CellStyle;
  8. import org.dom4j.Element;
  9. /**
  10. *
  11. * @author Finn
  12. */
  13. public class HSSFStyleHelper implements StyleHelper {
  14. private static HSSFWorkbook workbook;
  15. private static HSSFPalette palette;
  16. private static final HSSFColor HSSF_AUTO = new HSSFColor.AUTOMATIC();
  17. public HSSFStyleHelper(HSSFWorkbook wb)
  18. {
  19. workbook = wb;
  20. palette = wb.getCustomPalette();
  21. }
  22. public String getBGColour(CellStyle style)
  23. {
  24. return getRGBString(style.getFillForegroundColor());
  25. }
  26. public void setFontProperties(CellStyle style, Element element)
  27. {
  28. HSSFCellStyle newStyle = (HSSFCellStyle) style;
  29. HSSFFont font = newStyle.getFont(workbook);
  30. if(font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD)
  31. element.addElement("font-weight").addText("bold");
  32. if(font.getItalic())
  33. element.addElement("font-style").addText("italics");
  34. if(font.getUnderline() != HSSFFont.U_NONE)
  35. element.addElement("text-decoration").addText("underline");
  36. //Ignore same-ish defaults
  37. if(font.getFontHeightInPoints() != 10 && font.getFontHeightInPoints() != 11)
  38. element.addElement("font-size").addText(String.valueOf(font.getFontHeightInPoints() + "pt"));
  39. //Arial is default for Excel, Calibri is default for OO
  40. if(!font.getFontName().equals("Arial") && !font.getFontName().equals("Calibri"))
  41. element.addElement("font-family").addText(font.getFontName());
  42. if((font.getColor() != HSSFFont.COLOR_NORMAL) && !getRGBString(font.getColor()).equals("#000"))
  43. element.addElement("color").addText(getRGBString(font.getColor()));
  44. }
  45. private String getRGBString(short index)
  46. {
  47. String string = null;
  48. HSSFColor color = palette.getColor(index);
  49. if (index == HSSF_AUTO.getIndex() || color == null)
  50. {
  51. }
  52. else
  53. {
  54. short[] rgb = color.getTriplet();
  55. string = "#";
  56. for(int i = 0; i <= 2; i++)
  57. {
  58. String colourSection = Integer.toHexString((int) rgb[i]);
  59. if(colourSection.length() == 1)
  60. colourSection = "0" + colourSection;
  61. string += colourSection;
  62. }
  63. }
  64. return string;
  65. }
  66. }