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.

XSSFHtmlHelper.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.ss.usermodel.CellStyle;
  18. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  19. import org.apache.poi.xssf.usermodel.XSSFColor;
  20. /**
  21. * Implementation of {@link HtmlHelper} for XSSF files.
  22. *
  23. * @author Ken Arnold, Industrious Media LLC
  24. */
  25. public class XSSFHtmlHelper implements HtmlHelper {
  26. @Override
  27. public void colorStyles(CellStyle style, Formatter out) {
  28. XSSFCellStyle cs = (XSSFCellStyle) style;
  29. styleColor(out, "background-color", cs.getFillForegroundXSSFColor());
  30. styleColor(out, "text-color", cs.getFont().getXSSFColor());
  31. }
  32. private void styleColor(Formatter out, String attr, XSSFColor color) {
  33. if (color == null || color.isAuto()) {
  34. return;
  35. }
  36. byte[] rgb = color.getRGB();
  37. if (rgb == null) {
  38. return;
  39. }
  40. // This is done twice -- rgba is new with CSS 3, and browser that don't
  41. // support it will ignore the rgba specification and stick with the
  42. // solid color, which is declared first
  43. out.format(" %s: #%02x%02x%02x;%n", attr, rgb[0], rgb[1], rgb[2]);
  44. byte[] argb = color.getARGB();
  45. if (argb == null) {
  46. return;
  47. }
  48. out.format(" %s: rgba(0x%02x, 0x%02x, 0x%02x, 0x%02x);%n", attr,
  49. argb[3], argb[0], argb[1], argb[2]);
  50. }
  51. }