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.

ToHtml.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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.io.BufferedReader;
  17. import java.io.Closeable;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.PrintWriter;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Formatter;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.TreeMap;
  32. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  33. import org.apache.poi.ss.format.CellFormat;
  34. import org.apache.poi.ss.format.CellFormatResult;
  35. import org.apache.poi.ss.usermodel.BorderStyle;
  36. import org.apache.poi.ss.usermodel.Cell;
  37. import org.apache.poi.ss.usermodel.CellStyle;
  38. import org.apache.poi.ss.usermodel.CellType;
  39. import org.apache.poi.ss.usermodel.Font;
  40. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  41. import org.apache.poi.ss.usermodel.Row;
  42. import org.apache.poi.ss.usermodel.Sheet;
  43. import org.apache.poi.ss.usermodel.VerticalAlignment;
  44. import org.apache.poi.ss.usermodel.Workbook;
  45. import org.apache.poi.ss.usermodel.WorkbookFactory;
  46. import org.apache.poi.util.IOUtils;
  47. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  48. /**
  49. * This example shows how to display a spreadsheet in HTML using the classes for
  50. * spreadsheet display.
  51. */
  52. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  53. public final class ToHtml {
  54. private final Workbook wb;
  55. private final Appendable output;
  56. private boolean completeHTML;
  57. private Formatter out;
  58. private boolean gotBounds;
  59. private int firstColumn;
  60. private int endColumn;
  61. private HtmlHelper helper;
  62. private static final String DEFAULTS_CLASS = "excelDefaults";
  63. private static final String COL_HEAD_CLASS = "colHeader";
  64. private static final String ROW_HEAD_CLASS = "rowHeader";
  65. private static final Map<HorizontalAlignment, String> HALIGN = mapFor(
  66. HorizontalAlignment.LEFT, "left",
  67. HorizontalAlignment.CENTER, "center",
  68. HorizontalAlignment.RIGHT, "right",
  69. HorizontalAlignment.FILL, "left",
  70. HorizontalAlignment.JUSTIFY, "left",
  71. HorizontalAlignment.CENTER_SELECTION, "center");
  72. private static final Map<VerticalAlignment, String> VALIGN = mapFor(
  73. VerticalAlignment.BOTTOM, "bottom",
  74. VerticalAlignment.CENTER, "middle",
  75. VerticalAlignment.TOP, "top");
  76. private static final Map<BorderStyle, String> BORDER = mapFor(
  77. BorderStyle.DASH_DOT, "dashed 1pt",
  78. BorderStyle.DASH_DOT_DOT, "dashed 1pt",
  79. BorderStyle.DASHED, "dashed 1pt",
  80. BorderStyle.DOTTED, "dotted 1pt",
  81. BorderStyle.DOUBLE, "double 3pt",
  82. BorderStyle.HAIR, "solid 1px",
  83. BorderStyle.MEDIUM, "solid 2pt",
  84. BorderStyle.MEDIUM_DASH_DOT, "dashed 2pt",
  85. BorderStyle.MEDIUM_DASH_DOT_DOT, "dashed 2pt",
  86. BorderStyle.MEDIUM_DASHED, "dashed 2pt",
  87. BorderStyle.NONE, "none",
  88. BorderStyle.SLANTED_DASH_DOT, "dashed 2pt",
  89. BorderStyle.THICK, "solid 3pt",
  90. BorderStyle.THIN, "dashed 1pt");
  91. private static final int IDX_TABLE_WIDTH = -2;
  92. private static final int IDX_HEADER_COL_WIDTH = -1;
  93. @SuppressWarnings({"unchecked"})
  94. private static <K, V> Map<K, V> mapFor(Object... mapping) {
  95. Map<K, V> map = new HashMap<>();
  96. for (int i = 0; i < mapping.length; i += 2) {
  97. map.put((K) mapping[i], (V) mapping[i + 1]);
  98. }
  99. return map;
  100. }
  101. /**
  102. * Creates a new examples to HTML for the given workbook.
  103. *
  104. * @param wb The workbook.
  105. * @param output Where the HTML output will be written.
  106. *
  107. * @return An object for converting the workbook to HTML.
  108. */
  109. public static ToHtml create(Workbook wb, Appendable output) {
  110. return new ToHtml(wb, output);
  111. }
  112. /**
  113. * Creates a new examples to HTML for the given workbook. If the path ends
  114. * with "<tt>.xlsx</tt>" an {@link XSSFWorkbook} will be used; otherwise
  115. * this will use an {@link HSSFWorkbook}.
  116. *
  117. * @param path The file that has the workbook.
  118. * @param output Where the HTML output will be written.
  119. *
  120. * @return An object for converting the workbook to HTML.
  121. */
  122. public static ToHtml create(String path, Appendable output)
  123. throws IOException {
  124. return create(new FileInputStream(path), output);
  125. }
  126. /**
  127. * Creates a new examples to HTML for the given workbook. This attempts to
  128. * detect whether the input is XML (so it should create an {@link
  129. * XSSFWorkbook} or not (so it should create an {@link HSSFWorkbook}).
  130. *
  131. * @param in The input stream that has the workbook.
  132. * @param output Where the HTML output will be written.
  133. *
  134. * @return An object for converting the workbook to HTML.
  135. */
  136. public static ToHtml create(InputStream in, Appendable output)
  137. throws IOException {
  138. Workbook wb = WorkbookFactory.create(in);
  139. return create(wb, output);
  140. }
  141. private ToHtml(Workbook wb, Appendable output) {
  142. if (wb == null) {
  143. throw new NullPointerException("wb");
  144. }
  145. if (output == null) {
  146. throw new NullPointerException("output");
  147. }
  148. this.wb = wb;
  149. this.output = output;
  150. setupColorMap();
  151. }
  152. private void setupColorMap() {
  153. if (wb instanceof HSSFWorkbook) {
  154. helper = new HSSFHtmlHelper((HSSFWorkbook) wb);
  155. } else if (wb instanceof XSSFWorkbook) {
  156. helper = new XSSFHtmlHelper();
  157. } else {
  158. throw new IllegalArgumentException(
  159. "unknown workbook type: " + wb.getClass().getSimpleName());
  160. }
  161. }
  162. /**
  163. * Run this class as a program
  164. *
  165. * @param args The command line arguments.
  166. *
  167. * @throws Exception Exception we don't recover from.
  168. */
  169. public static void main(String[] args) throws Exception {
  170. if(args.length < 2){
  171. System.err.println("usage: ToHtml inputWorkbook outputHtmlFile");
  172. return;
  173. }
  174. try (PrintWriter pw = new PrintWriter(args[1], "UTF-8")) {
  175. ToHtml toHtml = create(args[0], pw);
  176. toHtml.setCompleteHTML(true);
  177. toHtml.printPage();
  178. }
  179. }
  180. public void setCompleteHTML(boolean completeHTML) {
  181. this.completeHTML = completeHTML;
  182. }
  183. public void printPage() throws IOException {
  184. try {
  185. ensureOut();
  186. if (completeHTML) {
  187. out.format(
  188. "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>%n");
  189. out.format("<html>%n");
  190. out.format("<head>%n");
  191. out.format("</head>%n");
  192. out.format("<body>%n");
  193. }
  194. print();
  195. if (completeHTML) {
  196. out.format("</body>%n");
  197. out.format("</html>%n");
  198. }
  199. } finally {
  200. IOUtils.closeQuietly(out);
  201. if (output instanceof Closeable) {
  202. IOUtils.closeQuietly((Closeable) output);
  203. }
  204. }
  205. }
  206. public void print() {
  207. printInlineStyle();
  208. printSheets();
  209. }
  210. private void printInlineStyle() {
  211. //out.format("<link href=\"excelStyle.css\" rel=\"stylesheet\" type=\"text/css\">%n");
  212. out.format("<style type=\"text/css\">%n");
  213. printStyles();
  214. out.format("</style>%n");
  215. }
  216. private void ensureOut() {
  217. if (out == null) {
  218. out = new Formatter(output, Locale.ROOT);
  219. }
  220. }
  221. public void printStyles() {
  222. ensureOut();
  223. // First, copy the base css
  224. try (BufferedReader in = new BufferedReader(new InputStreamReader(
  225. getClass().getResourceAsStream("excelStyle.css"), StandardCharsets.ISO_8859_1))){
  226. String line;
  227. while ((line = in.readLine()) != null) {
  228. out.format("%s%n", line);
  229. }
  230. } catch (IOException e) {
  231. throw new IllegalStateException("Reading standard css", e);
  232. }
  233. // now add css for each used style
  234. Set<CellStyle> seen = new HashSet<>();
  235. for (int i = 0; i < wb.getNumberOfSheets(); i++) {
  236. Sheet sheet = wb.getSheetAt(i);
  237. Iterator<Row> rows = sheet.rowIterator();
  238. while (rows.hasNext()) {
  239. Row row = rows.next();
  240. for (Cell cell : row) {
  241. CellStyle style = cell.getCellStyle();
  242. if (!seen.contains(style)) {
  243. printStyle(style);
  244. seen.add(style);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. private void printStyle(CellStyle style) {
  251. out.format(".%s .%s {%n", DEFAULTS_CLASS, styleName(style));
  252. styleContents(style);
  253. out.format("}%n");
  254. }
  255. private void styleContents(CellStyle style) {
  256. styleOut("text-align", style.getAlignment(), HALIGN);
  257. styleOut("vertical-align", style.getVerticalAlignment(), VALIGN);
  258. fontStyle(style);
  259. borderStyles(style);
  260. helper.colorStyles(style, out);
  261. }
  262. private void borderStyles(CellStyle style) {
  263. styleOut("border-left", style.getBorderLeft(), BORDER);
  264. styleOut("border-right", style.getBorderRight(), BORDER);
  265. styleOut("border-top", style.getBorderTop(), BORDER);
  266. styleOut("border-bottom", style.getBorderBottom(), BORDER);
  267. }
  268. private void fontStyle(CellStyle style) {
  269. Font font = wb.getFontAt(style.getFontIndex());
  270. if (font.getBold()) {
  271. out.format(" font-weight: bold;%n");
  272. }
  273. if (font.getItalic()) {
  274. out.format(" font-style: italic;%n");
  275. }
  276. int fontheight = font.getFontHeightInPoints();
  277. if (fontheight == 9) {
  278. //fix for stupid ol Windows
  279. fontheight = 10;
  280. }
  281. out.format(" font-size: %dpt;%n", fontheight);
  282. // Font color is handled with the other colors
  283. }
  284. private String styleName(CellStyle style) {
  285. if (style == null) {
  286. style = wb.getCellStyleAt((short) 0);
  287. }
  288. StringBuilder sb = new StringBuilder();
  289. try (Formatter fmt = new Formatter(sb, Locale.ROOT)) {
  290. fmt.format("style_%02x", style.getIndex());
  291. return fmt.toString();
  292. }
  293. }
  294. private <K> void styleOut(String attr, K key, Map<K, String> mapping) {
  295. String value = mapping.get(key);
  296. if (value != null) {
  297. out.format(" %s: %s;%n", attr, value);
  298. }
  299. }
  300. private static CellType ultimateCellType(Cell c) {
  301. CellType type = c.getCellType();
  302. if (type == CellType.FORMULA) {
  303. type = c.getCachedFormulaResultType();
  304. }
  305. return type;
  306. }
  307. private void printSheets() {
  308. ensureOut();
  309. Sheet sheet = wb.getSheetAt(0);
  310. printSheet(sheet);
  311. }
  312. public void printSheet(Sheet sheet) {
  313. ensureOut();
  314. Map<Integer, Integer> widths = computeWidths(sheet);
  315. int tableWidth = widths.get(IDX_TABLE_WIDTH);
  316. out.format("<table class=%s style=\"width:%dpx;\">%n", DEFAULTS_CLASS, tableWidth);
  317. printCols(widths);
  318. printSheetContent(sheet);
  319. out.format("</table>%n");
  320. }
  321. /**
  322. * computes the column widths, defined by the sheet.
  323. *
  324. * @param sheet The sheet for which to compute widths
  325. * @return Map with key: column index; value: column width in pixels
  326. * <br>special keys:
  327. * <br>{@link #IDX_HEADER_COL_WIDTH} - width of the header column
  328. * <br>{@link #IDX_TABLE_WIDTH} - width of the entire table
  329. */
  330. private Map<Integer, Integer> computeWidths(Sheet sheet) {
  331. Map<Integer, Integer> ret = new TreeMap<>();
  332. int tableWidth = 0;
  333. ensureColumnBounds(sheet);
  334. // compute width of the header column
  335. int lastRowNum = sheet.getLastRowNum();
  336. int headerCharCount = String.valueOf(lastRowNum).length();
  337. int headerColWidth = widthToPixels((headerCharCount + 1) * 256.0);
  338. ret.put(IDX_HEADER_COL_WIDTH, headerColWidth);
  339. tableWidth += headerColWidth;
  340. for (int i = firstColumn; i < endColumn; i++) {
  341. int colWidth = widthToPixels(sheet.getColumnWidth(i));
  342. ret.put(i, colWidth);
  343. tableWidth += colWidth;
  344. }
  345. ret.put(IDX_TABLE_WIDTH, tableWidth);
  346. return ret ;
  347. }
  348. /**
  349. * Probably platform-specific, but appears to be a close approximation on some systems
  350. * @param widthUnits POI's native width unit (twips)
  351. * @return the approximate number of pixels for a typical display
  352. */
  353. protected int widthToPixels(final double widthUnits) {
  354. return Math.toIntExact(Math.round(widthUnits * 9 / 256));
  355. }
  356. private void printCols(Map<Integer, Integer> widths) {
  357. int headerColWidth = widths.get(IDX_HEADER_COL_WIDTH);
  358. out.format("<col style=\"width:%dpx\"/>%n", headerColWidth);
  359. for (int i = firstColumn; i < endColumn; i++) {
  360. int colWidth = widths.get(i);
  361. out.format("<col style=\"width:%dpx;\"/>%n", colWidth);
  362. }
  363. }
  364. private void ensureColumnBounds(Sheet sheet) {
  365. if (gotBounds) {
  366. return;
  367. }
  368. Iterator<Row> iter = sheet.rowIterator();
  369. firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
  370. endColumn = 0;
  371. while (iter.hasNext()) {
  372. Row row = iter.next();
  373. short firstCell = row.getFirstCellNum();
  374. if (firstCell >= 0) {
  375. firstColumn = Math.min(firstColumn, firstCell);
  376. endColumn = Math.max(endColumn, row.getLastCellNum());
  377. }
  378. }
  379. gotBounds = true;
  380. }
  381. private void printColumnHeads() {
  382. out.format("<thead>%n");
  383. out.format(" <tr class=%s>%n", COL_HEAD_CLASS);
  384. out.format(" <th class=%s>&#x25CA;</th>%n", COL_HEAD_CLASS);
  385. //noinspection UnusedDeclaration
  386. StringBuilder colName = new StringBuilder();
  387. for (int i = firstColumn; i < endColumn; i++) {
  388. colName.setLength(0);
  389. int cnum = i;
  390. do {
  391. colName.insert(0, (char) ('A' + cnum % 26));
  392. cnum /= 26;
  393. } while (cnum > 0);
  394. out.format(" <th class=%s>%s</th>%n", COL_HEAD_CLASS, colName);
  395. }
  396. out.format(" </tr>%n");
  397. out.format("</thead>%n");
  398. }
  399. private void printSheetContent(Sheet sheet) {
  400. printColumnHeads();
  401. out.format("<tbody>%n");
  402. Iterator<Row> rows = sheet.rowIterator();
  403. while (rows.hasNext()) {
  404. Row row = rows.next();
  405. out.format(" <tr>%n");
  406. out.format(" <td class=%s>%d</td>%n", ROW_HEAD_CLASS,
  407. row.getRowNum() + 1);
  408. for (int i = firstColumn; i < endColumn; i++) {
  409. String content = "&nbsp;";
  410. String attrs = "";
  411. CellStyle style = null;
  412. if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
  413. Cell cell = row.getCell(i);
  414. if (cell != null) {
  415. style = cell.getCellStyle();
  416. attrs = tagStyle(cell, style);
  417. //Set the value that is rendered for the cell
  418. //also applies the format
  419. CellFormat cf = CellFormat.getInstance(
  420. style.getDataFormatString());
  421. CellFormatResult result = cf.apply(cell);
  422. content = result.text; //never null
  423. if (content.isEmpty()) {
  424. content = "&nbsp;";
  425. }
  426. }
  427. }
  428. out.format(" <td class=%s %s>%s</td>%n", styleName(style),
  429. attrs, content);
  430. }
  431. out.format(" </tr>%n");
  432. }
  433. out.format("</tbody>%n");
  434. }
  435. private String tagStyle(Cell cell, CellStyle style) {
  436. if (style.getAlignment() == HorizontalAlignment.GENERAL) {
  437. switch (ultimateCellType(cell)) {
  438. case STRING:
  439. return "style=\"text-align: left;\"";
  440. case BOOLEAN:
  441. case ERROR:
  442. return "style=\"text-align: center;\"";
  443. case NUMERIC:
  444. default:
  445. // "right" is the default
  446. break;
  447. }
  448. }
  449. return "";
  450. }
  451. }