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 4.8KB

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