Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook.

Prototype

public XSSFWorkbook() 

Source Link

Document

Create a new SpreadsheetML workbook.

Usage

From source file:Viewsale.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();//from   w w  w  .j a va 2 s . com
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1",
            new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2), dm.getColumnName(3),
                    dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6), dm.getColumnName(7),
                    dm.getColumnName(8), dm.getColumnName(9), dm.getColumnName(10), dm.getColumnName(11),
                    dm.getColumnName(12), dm.getColumnName(13), dm.getColumnName(14), dm.getColumnName(15) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10), getCellValue(i, 11),
                        getCellValue(i, 12), getCellValue(i, 13), getCellValue(i, 14), getCellValue(i, 15) });

    }
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/saleview.xlsx"));
    wb.write(fos);
    fos.close();

}

From source file:CreateTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException {

    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();

    //Create /*from  w  w  w .j  a  v  a 2s  .c  o  m*/
    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(3, 3));
    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(i + j + 0.0);
            }
        }
    }

    FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
    wb.write(fileOut);
    fileOut.close();
}

From source file:spareexcel.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();/*from  w  w w .j  ava2s .co m*/
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1", new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2),
            dm.getColumnName(3), dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i), new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2),
                getCellValue(i, 3), getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6) });

    }
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/sparexcel.xlsx"));
    wb.write(fos);
    fos.close();

}

From source file:CreatePivotTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();/*  w  w w . j  a  v a2  s . co m*/

    //Create some data to build the pivot table on
    setCellData(sheet);

    XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D4"), new CellReference("H5"));
    //Configure the pivot table
    //Use first column as row label
    pivotTable.addRowLabel(0);
    //Sum up the second column
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
    //Set the third column as filter
    pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
    //Add filter on forth column
    pivotTable.addReportFilter(3);

    FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}

From source file:Viewservice.java

private void writeToExcel() throws FileNotFoundException, IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();//w  w w .  ja va2  s  .c o  m
    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("-1",
            new Object[] { dm.getColumnName(0), dm.getColumnName(1), dm.getColumnName(2), dm.getColumnName(3),
                    dm.getColumnName(4), dm.getColumnName(5), dm.getColumnName(6), dm.getColumnName(7),
                    dm.getColumnName(8), dm.getColumnName(9), dm.getColumnName(10), dm.getColumnName(11),
                    dm.getColumnName(12), dm.getColumnName(13), dm.getColumnName(14), dm.getColumnName(15),
                    dm.getColumnName(16), dm.getColumnName(17), dm.getColumnName(18) });
    for (int i = 0; i < dm.getRowCount(); i++) {

        data.put(Integer.toString(i),
                new Object[] { getCellValue(i, 0), getCellValue(i, 1), getCellValue(i, 2), getCellValue(i, 3),
                        getCellValue(i, 4), getCellValue(i, 5), getCellValue(i, 6), getCellValue(i, 7),
                        getCellValue(i, 8), getCellValue(i, 9), getCellValue(i, 10), getCellValue(i, 11),
                        getCellValue(i, 12), getCellValue(i, 13), getCellValue(i, 14), getCellValue(i, 15),
                        getCellValue(i, 16), getCellValue(i, 17), getCellValue(i, 18) });

    }
    Set<String> ids = data.keySet();

    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);
        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            XSSFCell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    FileOutputStream fos = new FileOutputStream(new File("D:/motors/serviceview.xlsx"));

    wb.write(fos);
    fos.close();

}

From source file:CreateExcel.java

public static void write_excel() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet("version 1");

    System.out.println("created");
    Row row = sheet.createRow((short) 0);
    Cell col = row.createCell(0);//from  w w w.  j  a  v  a  2 s. co  m
    col.setCellValue("FileName");
    row.createCell(1).setCellValue("Boc");

    System.out.println("here");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream("C:\\Users\\aryan_000\\Desktop\\output.xlsx");
        wb.write(fos);
        fos.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:CreateExcel.java

public static void add_column() throws FileNotFoundException, IOException {
    String excelFilePath = "C:\\Users\\aryan_000\\Desktop\\output.xlsx";
    FileOutputStream outputStream = new FileOutputStream(new File(excelFilePath));
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
    Workbook wbout = new XSSFWorkbook();
    Workbook wbin = new XSSFWorkbook(inputStream);
    Sheet firstsheet = wbin.getSheetAt(0);

    XSSFSheet sheet = (XSSFSheet) wbout.createSheet("version 1");

    int max_row = firstsheet.getLastRowNum();
    for (int i = 0; i < max_row; i++) {
        Row row = sheet.createRow(i);/*from   w ww  . jav  a 2  s  .c  o m*/

        for (int j = 0; j < firstsheet.getLeftCol(); j++) {
            //                String str = firstsheet.get
            //                Cell col = row.createCell(j).setCellValue(firstsheet.getRow(i).getCell(j).getStringCellValue());
        }
    }
}

From source file:adams.data.io.output.ExcelSpreadSheetWriter.java

License:Open Source License

/**
 * Performs the actual writing. The caller must ensure that the writer gets
 * closed./*from   ww w.  j  a va  2 s.  c  o m*/
 *
 * @param content   the spreadsheet to write
 * @param out      the writer to write the spreadsheet to
 * @return      true if successfully written
 */
@Override
protected boolean doWrite(SpreadSheet[] content, OutputStream out) {
    boolean result;
    Workbook workbook;
    Sheet sheet;
    Row row;
    adams.data.spreadsheet.Row spRow;
    adams.data.spreadsheet.Cell spCell;
    Cell cell;
    int i;
    int n;
    int count;
    CellStyle styleDate;
    CellStyle styleDateTime;
    CellStyle styleTime;
    HashSet<String> names;
    String name;

    result = true;

    try {
        if (getWriteOOXML())
            workbook = new XSSFWorkbook();
        else
            workbook = new HSSFWorkbook();
        styleDate = ExcelHelper.getDateCellStyle(workbook, Constants.DATE_FORMAT);
        styleDateTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIMESTAMP_FORMAT);
        styleTime = ExcelHelper.getDateCellStyle(workbook, Constants.TIME_FORMAT);

        count = 0;
        names = new HashSet<>();
        for (SpreadSheet cont : content) {
            if (m_Stopped)
                return false;

            sheet = workbook.createSheet();
            if (cont.getName() != null) {
                name = cont.getName().replace("'", "");
                if (names.contains(name))
                    name += (count + 1);
            } else {
                name = m_SheetPrefix + (count + 1);
            }
            names.add(name);
            workbook.setSheetName(count, name);

            // header
            row = sheet.createRow(0);
            for (i = 0; i < cont.getColumnCount(); i++) {
                cell = row.createCell(i);
                cell.setCellValue(cont.getHeaderRow().getCell(i).getContent());
            }

            // data
            for (n = 0; n < cont.getRowCount(); n++) {
                if (m_Stopped)
                    return false;
                row = sheet.createRow(n + 1);
                spRow = cont.getRow(n);
                for (i = 0; i < cont.getColumnCount(); i++) {
                    cell = row.createCell(i);
                    spCell = spRow.getCell(i);
                    if ((spCell == null) || spCell.isMissing()) {
                        if (m_MissingValue.length() > 0)
                            cell.setCellValue(m_MissingValue);
                        else
                            cell.setCellType(Cell.CELL_TYPE_BLANK);
                        continue;
                    }

                    if (spCell.isFormula() && !m_OutputAsDisplayed) {
                        cell.setCellFormula(spCell.getFormula().substring(1));
                    } else {
                        if (spCell.isDate()) {
                            cell.setCellValue(spCell.toDate());
                            cell.setCellStyle(styleDate);
                        } else if (spCell.isTime()) {
                            cell.setCellValue(spCell.toTime());
                            cell.setCellStyle(styleTime);
                        } else if (spCell.isDateTime()) {
                            cell.setCellValue(spCell.toDateTime());
                            cell.setCellStyle(styleDateTime);
                        } else if (spCell.isNumeric()) {
                            cell.setCellValue(Utils.toDouble(spCell.getContent()));
                        } else {
                            cell.setCellValue(spCell.getContent());
                        }
                    }
                }
            }

            // next sheet
            count++;
        }

        // save
        workbook.write(out);
    } catch (Exception e) {
        result = false;
        getLogger().log(Level.SEVERE, "Failed writing spreadsheet data", e);
    }

    return result;
}

From source file:amazonreviews.AmazonReviews.java

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    //new AmazonReviews.filea("B00I8BIBCW");
    String s1 = "B002RL9CYK";
    reviews = new ArrayList<String>();
    reviewsDate = new ArrayList<String>();
    reviewsStar = new ArrayList<String>();
    XSSFWorkbook workbook = new XSSFWorkbook();

    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Employee Data");

    //This data needs to be written (Object[])
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
    data.put("0", new Object[] { "Review Text", "Review Date", "Review Stars" });
    for (int i = 1; i <= 100; i++) {
        GetReviews(s1, Integer.toString(i));
    }/*w w  w  .j a va 2 s.  co  m*/
    for (int i = 0; i < reviews.size(); i++) {
        data.put(Integer.toString(i + 1),
                new Object[] { reviews.get(i), reviewsDate.get(i), reviewsStar.get(i) });
    }

    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        XSSFRow row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String)
                cell.setCellValue((String) obj);
            else if (obj instanceof Integer)
                cell.setCellValue((Integer) obj);
        }
    }
    try {
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File(name + ".xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ambit2.core.io.XLSFileWriter.java

License:Open Source License

public XLSFileWriter(OutputStream out, boolean hssf) throws Exception {
    super();/*from w w  w .  j  a v a 2s .c o  m*/
    workbook = hssf ? new HSSFWorkbook() : new XSSFWorkbook();
    sheet = workbook.createSheet();
    dataformat = workbook.createDataFormat();
    style = workbook.createCellStyle();
    style.setDataFormat(dataformat.getFormat("0.00"));

    this.out = out;
}