From 50af15f97ff1236b2052da18a699e7ad3b643797 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 24 Jul 2014 17:09:23 +0000 Subject: [PATCH] XSSF table example, from Sofia Larsson from bug #55562 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1613191 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/usermodel/examples/CreateTable.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java new file mode 100644 index 0000000000..c26ff067c5 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java @@ -0,0 +1,92 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.usermodel.examples; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.AreaReference; +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFTable; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo; + +/** + * Demonstrates how to create a simple table using Apache POI. + */ +public class CreateTable { + + public static void main(String[] args) throws FileNotFoundException, + IOException { + + Workbook wb = new XSSFWorkbook(); + XSSFSheet sheet = (XSSFSheet) wb.createSheet(); + + //Create + XSSFTable table = sheet.createTable(); + table.setDisplayName("Test"); + CTTable cttable = table.getCTTable(); + + //Style configurations + CTTableStyleInfo style = cttable.addNewTableStyleInfo(); + style.setName("TableStyleMedium2"); + style.setShowColumnStripes(false); + style.setShowRowStripes(true); + + //Set which area the table should be placed in + AreaReference reference = new AreaReference(new CellReference(0, 0), + new CellReference(2,2)); + cttable.setRef(reference.formatAsString()); + cttable.setId(1); + cttable.setName("Test"); + cttable.setTotalsRowCount(1); + + CTTableColumns columns = cttable.addNewTableColumns(); + columns.setCount(3); + CTTableColumn column; + XSSFRow row; + XSSFCell cell; + for(int i=0; i<3; i++) { + //Create column + column = columns.addNewTableColumn(); + column.setName("Column"); + column.setId(i+1); + //Create row + row = sheet.createRow(i); + for(int j=0; j<3; j++) { + //Create cell + cell = row.createCell(j); + if(i == 0) { + cell.setCellValue("Column"+j); + } else { + cell.setCellValue("0"); + } + } + } + + FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx"); + wb.write(fileOut); + fileOut.close(); + } +} -- 2.39.5