Search in sources :

Example 21 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class CreateTable method main.

public static void main(String[] args) throws 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();
    wb.close();
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) CellReference(org.apache.poi.ss.util.CellReference) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 22 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class CreateUserDefinedDataFormats method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("format sheet");
    CellStyle style;
    DataFormat format = wb.createDataFormat();
    Row row;
    Cell cell;
    short rowNum = 0;
    short colNum = 0;
    row = sheet.createRow(rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);
    row = sheet.createRow(++rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);
    FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) DataFormat(org.apache.poi.ss.usermodel.DataFormat) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 23 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class CustomXMLMapping method main.

public static void main(String[] args) throws Exception {
    OPCPackage pkg = OPCPackage.open(args[0]);
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    for (XSSFMap map : wb.getCustomXMLMappings()) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xml = os.toString("UTF-8");
        System.out.println(xml);
    }
    pkg.close();
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFExportToXml(org.apache.poi.xssf.extractor.XSSFExportToXml) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 24 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class WorkbookProperties method main.

public static void main(String[] args) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    workbook.createSheet("Workbook Properties");
    POIXMLProperties props = workbook.getProperties();
    /**
         * Extended properties are a predefined set of metadata properties
         * that are specifically applicable to Office Open XML documents.
         * Extended properties consist of 24 simple properties and 3 complex properties stored in the
         *  part targeted by the relationship of type
         */
    POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties();
    ext.getUnderlyingProperties().setCompany("Apache Software Foundation");
    ext.getUnderlyingProperties().setTemplate("XSSF");
    /**
         * Custom properties enable users to define custom metadata properties.
         */
    POIXMLProperties.CustomProperties cust = props.getCustomProperties();
    cust.addProperty("Author", "John Smith");
    cust.addProperty("Year", 2009);
    cust.addProperty("Price", 45.50);
    cust.addProperty("Available", true);
    FileOutputStream out = new FileOutputStream("workbook.xlsx");
    workbook.write(out);
    out.close();
    workbook.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 25 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class WorkingWithBorders method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("borders");
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 1);
    // Create a cell and put a value in it.
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(4);
    // Style the cell with borders all around.
    CellStyle style = wb.createCellStyle();
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.BLUE.getIndex());
    style.setBorderTop(BorderStyle.MEDIUM_DASHED);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)208 Test (org.junit.Test)95 Sheet (org.apache.poi.ss.usermodel.Sheet)72 Workbook (org.apache.poi.ss.usermodel.Workbook)72 FileOutputStream (java.io.FileOutputStream)48 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)47 Cell (org.apache.poi.ss.usermodel.Cell)36 Row (org.apache.poi.ss.usermodel.Row)35 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)23 XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)19 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)17 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)16 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)16 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)15 IOException (java.io.IOException)14 CellStyle (org.apache.poi.ss.usermodel.CellStyle)14 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)14