PageRenderTime 154ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/sysmodb/XSSFStyleHelper.java

https://github.com/fbacall/simple-spreadsheet-extractor
Java | 71 lines | 56 code | 8 blank | 7 comment | 17 complexity | 9e0968a5fb3673664dd344f78f1aeea8 MD5 | raw file
  1. package org.sysmodb;
  2. import org.apache.poi.ss.usermodel.CellStyle;
  3. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  4. import org.apache.poi.xssf.usermodel.XSSFColor;
  5. import org.apache.poi.xssf.usermodel.XSSFFont;
  6. import org.dom4j.Element;
  7. /**
  8. *
  9. * @author Finn
  10. */
  11. public class XSSFStyleHelper implements StyleHelper{
  12. public String getBGColour(CellStyle style)
  13. {
  14. XSSFCellStyle newStyle = (XSSFCellStyle) style;
  15. XSSFColor colour = newStyle.getFillForegroundXSSFColor();
  16. return getRGBString(colour);
  17. }
  18. public void setFontProperties(CellStyle style, Element element)
  19. {
  20. XSSFCellStyle newStyle = (XSSFCellStyle) style;
  21. XSSFFont font = newStyle.getFont();
  22. if(font.getBold())
  23. element.addElement("font-weight").addText("bold");
  24. if(font.getItalic())
  25. element.addElement("font-style").addText("italics");
  26. if(font.getUnderline() != XSSFFont.U_NONE)
  27. element.addElement("text-decoration").addText("underline");
  28. if(font.getFontHeight() != XSSFFont.DEFAULT_FONT_SIZE)
  29. element.addElement("font-size").addText(String.valueOf(font.getFontHeightInPoints()) + "pt");
  30. if(!font.getFontName().equals(XSSFFont.DEFAULT_FONT_NAME))
  31. element.addElement("font-family").addText(font.getFontName());
  32. if(font.getColor() != XSSFFont.DEFAULT_FONT_COLOR)
  33. {
  34. String colorString = getRGBString(font.getXSSFColor());
  35. if(colorString != null)
  36. {
  37. element.addElement("color").addText(colorString);
  38. }
  39. }
  40. }
  41. private String getRGBString(XSSFColor colour)
  42. {
  43. String string = null;
  44. //Disregard default/automatic colours, to avoid cluttering XML
  45. if (colour == null || colour.isAuto())
  46. {
  47. return string;
  48. }
  49. else
  50. {
  51. String rgb = colour.getARGBHex();
  52. //XSSF has a bug where the above can sometimes return null
  53. // so we check here
  54. if(rgb != null)
  55. {
  56. if(rgb.length() > 6)
  57. rgb = rgb.substring(2,rgb.length());
  58. string = "#" + rgb;
  59. }
  60. }
  61. return string;
  62. }
  63. }