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.

CreateTable.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.xssf.usermodel.examples;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.apache.poi.ss.util.AreaReference;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.xssf.usermodel.XSSFCell;
  22. import org.apache.poi.xssf.usermodel.XSSFRow;
  23. import org.apache.poi.xssf.usermodel.XSSFSheet;
  24. import org.apache.poi.xssf.usermodel.XSSFTable;
  25. import org.apache.poi.xssf.usermodel.XSSFTableStyleInfo;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. /**
  28. * Demonstrates how to create a simple table using Apache POI.
  29. */
  30. public class CreateTable {
  31. public static void main(String[] args) throws IOException {
  32. try (Workbook wb = new XSSFWorkbook()) {
  33. XSSFSheet sheet = (XSSFSheet) wb.createSheet();
  34. // Create
  35. XSSFTable table = sheet.createTable();
  36. table.setName("Test");
  37. table.setDisplayName("Test_Table");
  38. // For now, create the initial style in a low-level way
  39. table.getCTTable().addNewTableStyleInfo();
  40. table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2");
  41. // Style the table
  42. XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle();
  43. style.setName("TableStyleMedium2");
  44. style.setShowColumnStripes(false);
  45. style.setShowRowStripes(true);
  46. style.setFirstColumn(false);
  47. style.setLastColumn(false);
  48. style.setShowRowStripes(true);
  49. style.setShowColumnStripes(true);
  50. // Set the values for the table
  51. XSSFRow row;
  52. XSSFCell cell;
  53. for (int i = 0; i < 3; i++) {
  54. // Create row
  55. row = sheet.createRow(i);
  56. for (int j = 0; j < 3; j++) {
  57. // Create cell
  58. cell = row.createCell(j);
  59. if (i == 0) {
  60. cell.setCellValue("Column" + (j + 1));
  61. } else {
  62. cell.setCellValue((i + 1) * (j + 1));
  63. }
  64. }
  65. }
  66. // Create the columns
  67. table.createColumn("Column 1");
  68. table.createColumn("Column 2");
  69. table.createColumn("Column 3");
  70. // Set which area the table should be placed in
  71. AreaReference reference = wb.getCreationHelper().createAreaReference(
  72. new CellReference(0, 0), new CellReference(2, 2));
  73. table.setCellReferences(reference);
  74. // Save
  75. try (FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx")) {
  76. wb.write(fileOut);
  77. }
  78. }
  79. }
  80. }