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.

TableDemo.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.hslf;
  16. import java.awt.Color;
  17. import java.io.FileOutputStream;
  18. import org.apache.poi.hslf.usermodel.HSLFSlide;
  19. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  20. import org.apache.poi.hslf.usermodel.HSLFTable;
  21. import org.apache.poi.hslf.usermodel.HSLFTableCell;
  22. import org.apache.poi.hslf.usermodel.HSLFTextRun;
  23. import org.apache.poi.sl.draw.DrawTableShape;
  24. import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
  25. import org.apache.poi.sl.usermodel.VerticalAlignment;
  26. /**
  27. * Demonstrates how to create tables
  28. */
  29. public final class TableDemo {
  30. //test data for the first table
  31. static final String[][] txt1 = {
  32. {"INPUT FILE", "NUMBER OF RECORDS"},
  33. {"Item File", "11,559"},
  34. {"Vendor File", "502"},
  35. {"Purchase History File - # of PO\u2019s\r(12/01/04 - 05/31/06)", "12,852"},
  36. {"Purchase History File - # of PO Lines\r(12/01/04 - 05/31/06)", "53,523" },
  37. {"Total PO History Spend", "$10,172,038"}
  38. };
  39. //test data for the second taable
  40. static final String[][] txt2 = {
  41. {"Data Source"},
  42. {"CAS Internal Metrics - Item Master Summary\r" +
  43. "CAS Internal Metrics - Vendor Summary\r" +
  44. "CAS Internal Metrics - PO History Summary"}
  45. };
  46. public static void main(String[] args) throws Exception {
  47. try (HSLFSlideShow ppt = new HSLFSlideShow()) {
  48. HSLFSlide slide = ppt.createSlide();
  49. create1stTable(slide);
  50. create2ndTable(slide);
  51. try (FileOutputStream out = new FileOutputStream("hslf-table.ppt")) {
  52. ppt.write(out);
  53. }
  54. }
  55. }
  56. static void create1stTable(HSLFSlide slide) {
  57. //six rows, two columns
  58. HSLFTable table1 = slide.createTable(6, 2);
  59. for (int i = 0; i < txt1.length; i++) {
  60. for (int j = 0; j < txt1[i].length; j++) {
  61. HSLFTableCell cell = table1.getCell(i, j);
  62. HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
  63. rt.setFontFamily("Arial");
  64. rt.setFontSize(10d);
  65. if(i == 0){
  66. cell.getFill().setForegroundColor(new Color(227, 227, 227));
  67. } else {
  68. rt.setBold(true);
  69. }
  70. cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  71. cell.setHorizontalCentered(true);
  72. cell.setText(txt1[i][j]);
  73. }
  74. }
  75. DrawTableShape dts1 = new DrawTableShape(table1);
  76. dts1.setAllBorders(1.0, Color.black);
  77. table1.setColumnWidth(0, 300);
  78. table1.setColumnWidth(1, 150);
  79. int pgWidth = slide.getSlideShow().getPageSize().width;
  80. table1.moveTo((pgWidth - table1.getAnchor().getWidth())/2., 100.);
  81. }
  82. static void create2ndTable(HSLFSlide slide) {
  83. //two rows, one column
  84. HSLFTable table2 = slide.createTable(2, 1);
  85. for (int i = 0; i < txt2.length; i++) {
  86. for (int j = 0; j < txt2[i].length; j++) {
  87. HSLFTableCell cell = table2.getCell(i, j);
  88. HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
  89. rt.setFontSize(10d);
  90. rt.setFontFamily("Arial");
  91. if(i == 0){
  92. cell.getFill().setForegroundColor(new Color(0, 51, 102));
  93. rt.setFontColor(Color.white);
  94. rt.setBold(true);
  95. rt.setFontSize(14d);
  96. cell.setHorizontalCentered(true);
  97. } else {
  98. rt.getTextParagraph().setBullet(true);
  99. rt.setFontSize(12d);
  100. rt.getTextParagraph().setTextAlign(TextAlign.LEFT);
  101. cell.setHorizontalCentered(false);
  102. }
  103. cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  104. cell.setText(txt2[i][j]);
  105. }
  106. }
  107. table2.setColumnWidth(0, 300);
  108. table2.setRowHeight(0, 30);
  109. table2.setRowHeight(1, 70);
  110. DrawTableShape dts2 = new DrawTableShape(table2);
  111. dts2.setOutsideBorders(Color.black, 1.0);
  112. table2.moveTo(200, 400);
  113. }
  114. }