You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFHtmlHelper.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss.html;
  16. import java.util.Formatter;
  17. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  18. import org.apache.poi.hssf.usermodel.HSSFPalette;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.hssf.util.HSSFColor;
  21. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. /**
  24. * Implementation of {@link HtmlHelper} for HSSF files.
  25. */
  26. public class HSSFHtmlHelper implements HtmlHelper {
  27. private final HSSFWorkbook wb;
  28. private final HSSFPalette colors;
  29. private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();
  30. public HSSFHtmlHelper(HSSFWorkbook wb) {
  31. this.wb = wb;
  32. // If there is no custom palette, then this creates a new one that is
  33. // a copy of the default
  34. colors = wb.getCustomPalette();
  35. }
  36. @Override
  37. public void colorStyles(CellStyle style, Formatter out) {
  38. HSSFCellStyle cs = (HSSFCellStyle) style;
  39. out.format(" /* fill pattern = %d */%n", cs.getFillPattern().getCode());
  40. styleColor(out, "background-color", cs.getFillForegroundColor());
  41. styleColor(out, "color", cs.getFont(wb).getColor());
  42. styleColor(out, "border-left-color", cs.getLeftBorderColor());
  43. styleColor(out, "border-right-color", cs.getRightBorderColor());
  44. styleColor(out, "border-top-color", cs.getTopBorderColor());
  45. styleColor(out, "border-bottom-color", cs.getBottomBorderColor());
  46. }
  47. private void styleColor(Formatter out, String attr, short index) {
  48. HSSFColor color = colors.getColor(index);
  49. if (index == HSSF_AUTO.getIndex() || color == null) {
  50. out.format(" /* %s: index = %d */%n", attr, index);
  51. } else {
  52. short[] rgb = color.getTriplet();
  53. out.format(" %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0],
  54. rgb[1], rgb[2], index);
  55. }
  56. }
  57. }