Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook close

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook close

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes the underlying POIFSFileSystem from which the Workbook was read, if any.

Usage

From source file:rzd.vivc.documentexamination.service.ReportGeneratorServicePOI.java

@Override
public String generateReportUsersForDoc(List<ExaminationLine> examinationLines, int percentage) {
    InputStream in = null;//from  w ww  .  java 2s.co m
    HSSFWorkbook wb = null;
    try {
        in = new FileInputStream("c://Documents/1/usersDoc.xls");
        wb = new HSSFWorkbook(in);
    } catch (IOException ex) {
        Logger.getLogger(ReportGeneratorServicePOI.class.getName()).log(Level.SEVERE, null, ex);
    }
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(0);
    Cell cell = row.getCell(3);
    cell.setCellValue(examinationLines.size() > 0 ? examinationLines.get(0).getName()
            : "  ??  ");
    int i = 2;
    for (ExaminationLine line : examinationLines) {
        if (!line.isChecked()) {
            Row createRow = sheet.createRow(i++);
            Cell createCell = createRow.createCell(0);
            createCell.setCellValue(line.getFio());
        }
    }
    Row createRow = sheet.createRow(i);
    Cell createCell = createRow.createCell(0);
    createCell.setCellValue("? :");
    createCell = createRow.createCell(1);
    createCell.setCellValue(percentage + "%");
    String tail = new Date().getTime() + ".xls";
    String addr = "c://Documents/1/usersDoc" + tail;
    File file = new File(addr);
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(ReportGeneratorServicePOI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    try {
        wb.write(new FileOutputStream(file));
        wb.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ReportGeneratorServicePOI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ReportGeneratorServicePOI.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "docs/1/usersDoc" + tail;
}

From source file:scheduler.Multiworkbooks.java

public static void main(String[] args) {

    double[] list = new double[] { 4, 7, 6, 5, 9, 4, 7, 5, 6, 10, 8, 2, 3, 44, 78, 53 };
    max(list);//from www. ja va2 s  . c o m

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Datatypes in Java");
    Object[][] datatypes = { { "Datatype", "Type", "Size(in bytes)" }, { "int", "Primitive", 2 },
            { "float", "Primitive", 4 }, { "double", "Primitive", 8 }, { "char", "Primitive", 1 },
            { "String", "Non-Primitive", "No fixed size" } };

    int rowNum = 0;
    System.out.println("Creating excel");

    for (Object[] datatype : datatypes) {

        Row row = sheet.createRow(rowNum++);
        int colNum = 0;
        for (Object field : datatype) {
            Cell cell = row.createCell(colNum++);
            if (field instanceof String) {
                cell.setCellValue((String) field);
            } else if (field instanceof Integer) {
                cell.setCellValue((Integer) field);
            }
        }
    }

    try {
        FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
        workbook.write(outputStream);
        workbook.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

    System.out.println("Done");

    String bello = "ILE_NAME";
    if (bello.length() == 3) {
        System.err.println("ERROR");
    } else {
        System.out.println("GOOD");
    }
}

From source file:shopv2.work.java

public void Importstore() throws IOException {

    try {/*from ww  w .j  a  v a 2s .c o  m*/
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        Set<Integer> keys = goodsID.keySet();

        int nr = 0;
        for (Integer key : keys) {
            HSSFRow r = sheet.createRow(nr);
            nr += 1;
            goods p = goodsID.get(key);

            r.createCell(0).setCellValue(p.getID());
            r.createCell(1).setCellValue(p.getName());
            r.createCell(2).setCellValue(p.getPrice());
            r.createCell(3).setCellValue(p.getCount());
        }
        workBook.write(new FileOutputStream("C:\\Users\\?\\Desktop\\store.xls"));
        workBook.close();
    } catch (Exception e) {
        System.out.println("");
    }
}

From source file:shopv2.work.java

public void Importsale() throws IOException {

    try {//from   ww w.j a v  a  2 s  .  c om
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        Set<Integer> keys = salesID.keySet();

        int nr = 0;
        for (Integer key : keys) {
            HSSFRow r = sheet.createRow(nr);
            nr += 1;
            salegoods p = salesID.get(key);

            r.createCell(0).setCellValue(p.getID());
            r.createCell(1).setCellValue(p.getIDtov());
            r.createCell(2).setCellValue(p.getPrice());
            r.createCell(3).setCellValue(p.getName());
            r.createCell(4).setCellValue(p.getCount());
        }
        workBook.write(new FileOutputStream("C:\\Users\\?\\Desktop\\sale.xls"));
        workBook.close();
    } catch (Exception e) {
        System.out.println("");
    }
}

From source file:Test.LeerExcel.java

public static void main(String args[]) throws IOException {
    FileInputStream file = new FileInputStream(new File("C:\\Users\\f2 a55m-s1\\Documents\\baseSqlLite\\"));
    // Crear el objeto que tendra el libro de Excel
    HSSFWorkbook workbook = new HSSFWorkbook(file);
    /*//  w w  w .java  2  s  .  c o  m
     * Obtenemos la primera pestaa a la que se quiera procesar indicando el indice.
     * Una vez obtenida la hoja excel con las filas que se quieren leer obtenemos el iterator
     * que nos permite recorrer cada una de las filas que contiene.
     */
    HSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
    Row row;
    // Recorremos todas las filas para mostrar el contenido de cada celda
    while (rowIterator.hasNext()) {
        row = rowIterator.next();
        // Obtenemos el iterator que permite recorres todas las celdas de una fila
        Iterator<Cell> cellIterator = row.cellIterator();
        Cell celda;
        while (cellIterator.hasNext()) {
            celda = cellIterator.next();
            // Dependiendo del formato de la celda el valor se debe mostrar como String, Fecha, boolean, entero...
            switch (celda.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(celda)) {
                    System.out.println(celda.getDateCellValue());
                } else {
                    System.out.println(celda.getNumericCellValue());
                }
                System.out.println(celda.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.println(celda.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.println(celda.getBooleanCellValue());
                break;
            }
        }
    }
    // cerramos el libro excel
    workbook.close();
}

From source file:vone.HSSFReadWrite.java

License:Apache License

/**
 * Method main// w w  w  . j  a va2 s.  com
 *
 * Given 1 argument takes that as the filename, inputs it and dumps the cell
 * values/types out to sys.out.<br/>
 *
 * given 2 arguments where the second argument is the word "write" and the
 * first is the filename - writes out a sample (test) spreadsheet see
 * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
 *
 * given 2 arguments where the first is an input filename and the second an
 * output filename (not write), attempts to fully read in the spreadsheet
 * and fully write it out.<br/>
 *
 * given 3 arguments where the first is an input filename and the second an
 * output filename (not write) and the third is "modify1", attempts to read
 * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col
 * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1".
 * If you take the output from the write test, you'll have a valid scenario.
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("At least one argument expected");
        return;
    }

    String fileName = args[0];
    try {
        if (args.length < 2) {

            HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);

            System.out.println("Data dump:\n");

            for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                HSSFSheet sheet = wb.getSheetAt(k);
                int rows = sheet.getPhysicalNumberOfRows();
                System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
                for (int r = 0; r < rows; r++) {
                    HSSFRow row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }

                    // int cells = row.getPhysicalNumberOfCells();
                    int cells = row.getLastCellNum();
                    System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s).");
                    for (int c = 0; c < cells; c++) {
                        HSSFCell cell = row.getCell(c);
                        String value = null;
                        if (cell == null) {
                            System.out.println("CELL col=" + c + " VALUE= Empty");
                            continue;
                        }

                        switch (cell.getCellTypeEnum()) {

                        case FORMULA:
                            value = "FORMULA value=" + cell.getCellFormula();
                            break;

                        case NUMERIC:
                            value = "NUMERIC value=" + cell.getNumericCellValue();
                            break;

                        case STRING:
                            value = "STRING value=" + cell.getStringCellValue();
                            break;

                        default:
                        }
                        System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
                    }
                }
            }
            wb.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}