Example usage for org.apache.poi.ss.usermodel Workbook createSheet

List of usage examples for org.apache.poi.ss.usermodel Workbook createSheet

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook createSheet.

Prototype

Sheet createSheet(String sheetname);

Source Link

Document

Create a new sheet for this Workbook and return the high level representation.

Usage

From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java

License:Open Source License

@Test
public void testSheetEmpty() throws IOException {
    /* setup *//*from   w  w  w.j a  v a  2  s.  c  om*/
    final Workbook workbook = new HSSFWorkbook();
    workbook.createSheet("root"); //$NON-NLS-1$

    /* act */
    final SpreadsheetImportResult result = EMFFormsSpreadsheetImporter.INSTANCE.importSpreadsheet(workbook,
            eClass);
    assertEquals(1, result.getErrorReports().size());
}

From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java

License:Open Source License

@Test
public void testNoLabelRow() throws IOException {
    /* setup *///from   w ww . j  av a2s. c om
    final Workbook workbook = new HSSFWorkbook();
    final Sheet sheet = workbook.createSheet("root"); //$NON-NLS-1$
    final Row rowDescription = sheet.createRow(0);
    rowDescription.createCell(1).setCellValue("My feature description"); //$NON-NLS-1$

    final Row rowMeta = sheet.createRow(1);
    rowMeta.createCell(1).setCellValue("Enter Numbers"); //$NON-NLS-1$

    final Row rowData = sheet.createRow(2);
    rowData.createCell(0).setCellValue("aaa"); //$NON-NLS-1$
    rowData.createCell(1).setCellValue("My Feature Value"); //$NON-NLS-1$
    /* act */
    final SpreadsheetImportResult result = EMFFormsSpreadsheetImporter.INSTANCE.importSpreadsheet(workbook,
            eClass);
    assertEquals(1, result.getErrorReports().size());
}

From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java

License:Open Source License

@Test
public void testNoObjectIdColumn() throws IOException {
    /* setup *///from w w  w .  ja  v a2 s . c om
    final Workbook workbook = new HSSFWorkbook();
    final Sheet sheet = workbook.createSheet("root"); //$NON-NLS-1$
    final Row rowLabel = sheet.createRow(0);
    rowLabel.createCell(0).setCellValue("My feature"); //$NON-NLS-1$

    final CreationHelper factory = workbook.getCreationHelper();

    // When the comment box is visible, have it show in a 1x3 space
    final ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(0);
    anchor.setCol2(1);
    anchor.setRow1(0);
    anchor.setRow2(1);

    final Drawing drawing = sheet.createDrawingPatriarch();
    final Comment comment = drawing.createCellComment(anchor);
    comment.setString(factory.createRichTextString(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ecore=\"http://www.eclipse.org/emf/2002/Ecore\" xmlns:org.eclipse.emf.ecp.view.model=\"http://org/eclipse/emf/ecp/view/model/170\"><domainModelEFeature xsi:type=\"ecore:EAttribute\" href=\"http://eclipse/org/emf/ecp/makeithappen/model/task#//User/lastName\"/></org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference>")); //$NON-NLS-1$

    final Row rowDescription = sheet.createRow(1);
    rowDescription.createCell(0).setCellValue("My feature description"); //$NON-NLS-1$

    final Row rowMeta = sheet.createRow(2);
    rowMeta.createCell(0).setCellValue("Enter Numbers"); //$NON-NLS-1$

    final Row rowData = sheet.createRow(3);
    rowData.createCell(0).setCellValue("My Feature Value"); //$NON-NLS-1$
    /* act */
    final SpreadsheetImportResult result = EMFFormsSpreadsheetImporter.INSTANCE.importSpreadsheet(workbook,
            eClass);
    assertEquals(1, result.getErrorReports().size());
}

From source file:org.eclipse.rcptt.ecl.data.apache.poi.impl.internal.commands.WriteExcelFileService.java

License:Open Source License

public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException {
    WriteExcelFile wef = (WriteExcelFile) command;
    EList<Table> tables = wef.getTables();

    String uri = wef.getUri();/*from  ww  w  . j  a v  a  2s  .  com*/
    EclFile file = FileResolver.resolve(uri);

    boolean isAppend = wef.isAppend();
    isAppend &= file.toFile().exists();

    Workbook book;
    if (isAppend) {
        book = ExcelFileService.readBook(file);
    } else {
        book = ExcelFileService.createBook(file, uri);
    }

    int sheetnum = 1;
    for (Table table : tables) {
        String sheetName = table.getPageName();
        if (sheetName == null || sheetName.equals("")) {
            sheetName = String.format(SHEET_NAME_PATTERN, sheetnum);
        }
        Sheet sheet = book.getSheet(sheetName);
        if (sheet == null) {
            sheet = book.createSheet(sheetName);
        }
        writeTable(sheet, table);
        if (!isAppend) {
            autoSizeColumns(sheet);
        }
        context.getOutput().write(table);
        sheetnum++;
    }

    ExcelFileService.writeBook(book, file);
    return Status.OK_STATUS;
}

From source file:org.eclipse.rmf.reqif10.excel.serialization.ExcelResourceImpl.java

License:Open Source License

@Override
protected void doSave(OutputStream outputStream, Map<?, ?> options) throws IOException {
    Workbook workbook = new HSSFWorkbook();
    workbook.createSheet("FirstSheet");
    workbook.write(outputStream);//from w  w  w.  j a v  a2 s .c  o m
}

From source file:org.excel.LinkedDropDownLists.java

License:Apache License

LinkedDropDownLists(String workbookName) {
    File file = null;/*from  ww w . j ava2  s  .c  o  m*/
    FileOutputStream fos = null;
    Workbook workbook = null;
    Sheet sheet = null;
    DataValidationHelper dvHelper = null;
    DataValidationConstraint dvConstraint = null;
    DataValidation validation = null;
    CellRangeAddressList addressList = null;
    try {

        // Using the ss.usermodel allows this class to support both binary
        // and xml based workbooks. The choice of which one to create is
        // made by checking the file extension.
        if (workbookName.endsWith(".xlsx")) {
            workbook = new XSSFWorkbook();
        } else {
            workbook = new HSSFWorkbook();
        }

        // Build the sheet that will hold the data for the validations. This
        // must be done first as it will create names that are referenced 
        // later.
        sheet = workbook.createSheet("Linked Validations");
        LinkedDropDownLists.buildDataSheet(sheet);

        // Build the first data validation to occupy cell A1. Note
        // that it retrieves it's data from the named area or region called
        // CHOICES. Further information about this can be found in the
        // static buildDataSheet() method below.
        addressList = new CellRangeAddressList(0, 0, 0, 0);
        dvHelper = sheet.getDataValidationHelper();
        dvConstraint = dvHelper.createFormulaListConstraint("CHOICES");
        validation = dvHelper.createValidation(dvConstraint, addressList);
        sheet.addValidationData(validation);

        // Now, build the linked or dependent drop down list that will
        // occupy cell B1. The key to the whole process is the use of the
        // INDIRECT() function. In the buildDataSheet(0 method, a series of
        // named regions are created and the names of three of them mirror
        // the options available to the user in the first drop down list
        // (in cell A1). Using the INDIRECT() function makes it possible
        // to convert the selection the user makes in that first drop down
        // into the addresses of a named region of cells and then to use
        // those cells to populate the second drop down list.
        addressList = new CellRangeAddressList(0, 0, 1, 1);
        dvConstraint = dvHelper.createFormulaListConstraint("INDIRECT(UPPER($A$1))");
        validation = dvHelper.createValidation(dvConstraint, addressList);
        sheet.addValidationData(validation);

        file = new File(workbookName);
        fos = new FileOutputStream(file);
        workbook.write(fos);
    } catch (IOException ioEx) {
        System.out.println("Caught a: " + ioEx.getClass().getName());
        System.out.println("Message: " + ioEx.getMessage());
        System.out.println("Stacktrace follws:.....");
        ioEx.printStackTrace(System.out);
    } finally {
        try {
            if (fos != null) {
                fos.close();
                fos = null;
            }
        } catch (IOException ioEx) {
            System.out.println("Caught a: " + ioEx.getClass().getName());
            System.out.println("Message: " + ioEx.getMessage());
            System.out.println("Stacktrace follws:.....");
            ioEx.printStackTrace(System.out);
        }
    }
}

From source file:org.fao.faostat.api.core.ExcelExporter.java

License:Open Source License

public String createExcel(String csv, String metadata, String filename)
        throws FileNotFoundException, IOException {

    String uuid = UUID.randomUUID().toString().substring(0, 8);

    filename = uuid + filename;//from  w  w  w  . j av a 2s  . co m
    if (!filename.endsWith(".xls"))
        filename += ".xls";

    Workbook wb = new HSSFWorkbook();
    Sheet dataSheet = wb.createSheet("Data");

    CSVReader csvReader = new CSVReader(new StringReader(csv));
    String[] nextLine;
    int counter = 0;
    while ((nextLine = csvReader.readNext()) != null) {
        Row row = dataSheet.createRow((short) counter++);
        for (int i = 0; i < nextLine.length; i += 1) {
            Cell cell = row.createCell(i);
            try {
                cell.setCellValue(Double.parseDouble(nextLine[i]));
            } catch (NumberFormatException e) {
                cell.setCellValue(nextLine[i]);
            }
        }
    }

    Sheet dataSheetMetadata = wb.createSheet("Metadata");
    csvReader = new CSVReader(new StringReader(metadata));
    counter = 0;
    while ((nextLine = csvReader.readNext()) != null) {
        Row row = dataSheetMetadata.createRow((short) counter++);
        for (int i = 0; i < nextLine.length; i += 1) {
            Cell cell = row.createCell(i);
            cell.setCellValue(nextLine[i]);
        }
    }

    FileOutputStream fileOut = new FileOutputStream(this.excelPath + File.separator + filename);
    wb.write(fileOut);
    fileOut.close();

    return filename;
}

From source file:org.fao.fenix.wds.core.utils.ExcelFactory.java

License:Open Source License

public String createExcel(List<List<String>> table, String pathForExcels, String filename, String ip,
        String port) throws WDSException {

    try {/*from  www  .  j  a v  a2 s .c om*/

        if (filename == null || filename.isEmpty() || filename.startsWith("null.xls"))
            filename = UUID.randomUUID() + ".xls";
        if (!filename.endsWith(".xls"))
            filename += ".xls";

        String fullpath = pathForExcels + File.separator + filename;
        String url = "http://" + ip + ":" + port + "/wds/excels/" + filename;
        //            String url = "http://" + ip + ":" + port + "/excels/" + filename;

        Workbook wb = new HSSFWorkbook();
        Sheet s = wb.createSheet("FENIX Web Data Server");

        for (int i = 0; i < table.size(); i++) {
            Row r = s.createRow((short) i);
            for (int j = 0; j < table.get(i).size(); j++) {
                Cell c = r.createCell(j);
                try {
                    Double d = Double.valueOf(table.get(i).get(j));
                    c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                    c.setCellValue(d);
                } catch (NumberFormatException e) {
                    c.setCellType(HSSFCell.CELL_TYPE_STRING);
                    c.setCellValue(table.get(i).get(j));
                }
            }
        }

        FileOutputStream fileOut = new FileOutputStream(fullpath);
        wb.write(fileOut);
        fileOut.close();

        //         return OPEN + url + CLOSE;
        return ONE + url + TWO + url + THREE;

    } catch (Exception e) {
        throw new WDSException(e.getMessage());
    }

}