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

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

Introduction

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

Prototype

public HSSFWorkbook(InputStream s) throws IOException 

Source Link

Document

Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your InputStream , including all nodes.

Usage

From source file:com.dv.util.DVExcelIO.java

License:Open Source License

public static Vector setExcelBHTIMFormat(String fullExcelFileName, String sheetName, int rowNumbers) {

    File file = new File(fullExcelFileName);
    FileInputStream in = null;/*from   www . j a  v  a2s .co  m*/
    Vector cols = new Vector();

    try {

        in = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 2; i < rowNumbers; i++) {

            row = sheet.getRow(i);

            cell = row.getCell(4);//9 for cty

            String ppp = cell.toString().trim();

            cell = row.getCell(9);

            String fff = cell.toString().trim();

            if (!ppp.equals("")) {

                String contents = "Verify from FMS side for " + ppp + "(" + fff + ")";

                //                    cols.addElement(contents);

                row.getCell(16).setCellValue(contents);

            } else {
                return null;
            }

        }

        FileOutputStream fOut = new FileOutputStream(file);
        workbook.write(fOut);
        fOut.flush();
        fOut.close();

    } catch (Exception eee) {

    }

    return cols;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

@Override
public void importWorkbook(Workbook workbook, String filename) throws WorkbookParserException {
    try {//from w  ww  .  j a v a2  s  . co m
        HSSFWorkbook xlsBook = new HSSFWorkbook(new FileInputStream(new File(filename))); //WorkbookFactory.create(new FileInputStream(new File(filename))); 

        validate(xlsBook, workbook);

        importDataToWorkbook(xlsBook, workbook);

    } catch (WorkbookParserException e) {
        throw e;

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.elbeesee.poink.transreptor.ReadableBinaryStreamToHSSFWorkbook.java

License:Open Source License

public void onTransrept(INKFRequestContext aContext) throws Exception {
    IReadableBinaryStreamRepresentation aRBS = (IReadableBinaryStreamRepresentation) aContext
            .sourcePrimary(IReadableBinaryStreamRepresentation.class);
    InputStream vInputStream = aRBS.getInputStream();
    HSSFWorkbook vWorkbook = new HSSFWorkbook(vInputStream);
    IHSSFWorkbookRepresentation vRepresentation = new HSSFWorkbookImplementation(vWorkbook);

    INKFResponse vResponse = aContext.createResponseFrom(vRepresentation);
    vResponse.setExpiry(INKFResponse.EXPIRY_DEPENDENT);
}

From source file:com.envisioncn.it.super_sonic.showcase.evaluation.utils.ExcelUtils.java

License:Open Source License

public static Workbook createWorkbook(String fileName, InputStream in) throws IOException {
    String type = initType(fileName);
    Workbook workbook = null;/*from  w w  w.ja va  2  s .c  o  m*/

    if (type.equals(VERSION2003)) {
        workbook = new HSSFWorkbook(in);
    } else if (type.equals(VERSION2007)) {
        workbook = new XSSFWorkbook(in);
    }

    return workbook;
}

From source file:com.eryansky.core.excelTools.JsGridReportBase.java

License:Apache License

/**
 * ??// www.j  a  v a 2 s  . co m
 * 
 * @param
 * @return HashMap<String,HSSFCellStyle>
 */
private HashMap<String, HSSFCellStyle> initStyles(HSSFWorkbook wb) {
    HashMap<String, HSSFCellStyle> ret = new HashMap<String, HSSFCellStyle>();
    try {
        POIFSFileSystem fs = new POIFSFileSystem(getClass().getClassLoader().getResourceAsStream(MODULE_PATH));

        HSSFWorkbook src = new HSSFWorkbook(fs);
        HSSFSheet sheet = src.getSheetAt(0);

        buildStyle(wb, src, sheet, 0, ret, "TITLE");
        buildStyle(wb, src, sheet, 1, ret, "SUB_TITLE");
        buildStyle(wb, src, sheet, 2, ret, "SUB_TITLE2");

        buildStyle(wb, src, sheet, 4, ret, "TABLE_HEADER");
        buildStyle(wb, src, sheet, 5, ret, "STRING");
        buildStyle(wb, src, sheet, 6, ret, "INT");
        buildStyle(wb, src, sheet, 7, ret, "D2");
        buildStyle(wb, src, sheet, 8, ret, "D3");

        buildStyle(wb, src, sheet, 10, ret, "STRING_C");
        buildStyle(wb, src, sheet, 11, ret, "INT_C");
        buildStyle(wb, src, sheet, 12, ret, "D2_C");
        buildStyle(wb, src, sheet, 13, ret, "D3_C");

        buildStyle(wb, src, sheet, 15, ret, "RED_BG");
        buildStyle(wb, src, sheet, 16, ret, "YELLOW_BG");
        buildStyle(wb, src, sheet, 17, ret, "GREEN_BG");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:com.esd.cs.common.HExcelSheetParser.java

License:Open Source License

public HExcelSheetParser(File file) {
    try {/*from  www. j  a  v  a  2 s  .c om*/
        // ?workbook
        workbook = new HSSFWorkbook(new FileInputStream(file));
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.esd.cs.worker.WorkerUtil.java

License:Open Source License

/**
 * excel:?2003?HSSFWorkbook,2007XSSFWorkbook.
 * 2003??HSSFWorkbook,??office2003 ??//from  ww  w . j  av a  2  s  .  c  o m
 * 
 * @param file
 * @return
 * @throws IOException
 * @throws FileNotFoundException
 */
public static Object hasParser(File file) throws FileNotFoundException, IOException {
    try {
        // ?workbook
        hWorkbook = new HSSFWorkbook(new FileInputStream(file));
        logger.info("excel 97-2003");
        return hWorkbook;
    } catch (Exception e) {
        xWorkbook = new XSSFWorkbook(new FileInputStream(file));
        logger.info("excel 2007-2010");
        return xWorkbook;

    }
}

From source file:com.essa.main.ReadExcel.java

public static void main(String[] args) {
    try {/*from ww  w  .ja va 2  s . c o  m*/

        InputStream input = new BufferedInputStream(new FileInputStream("sample.xls"));
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);

        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            System.out.println("\n");
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {

                HSSFCell cell = (HSSFCell) cells.next();
                if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType())
                    System.out.print(cell.getNumericCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType())
                    System.out.print(cell.getBooleanCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType())
                    System.out.print("BLANK     ");
                else
                    System.out.print("Unknown cell type");

            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.etest.view.tq.itemanalysis.FileUploadWindow.java

void readContentFromExcelFile(File excelFile) {
    try {//from   www  . j a  v  a 2s  . c  o  m
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelFile));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(wb.getActiveSheetIndex());
        HSSFRow row;
        HSSFCell cell;

        boolean stop = false;
        boolean nonBlankRowFound;
        int s;
        HSSFRow lastRow = null;

        while (stop == false) {
            nonBlankRowFound = false;
            lastRow = sheet.getRow(sheet.getLastRowNum());
            for (s = lastRow.getFirstCellNum(); s <= lastRow.getLastCellNum(); s++) {
                cell = lastRow.getCell(s);
                if (cell != null && lastRow.getCell(s).getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    nonBlankRowFound = true;
                }
            }
            if (nonBlankRowFound == true) {
                stop = true;
            } else {
                sheet.removeRow(lastRow);
            }
        }

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        int cols = 0; // No of columns
        int tmp = 0;

        // This trick ensures that we get the data properly even if it doesn't start from first few rows
        for (int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);
            if (row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();
                if (tmp > cols)
                    cols = tmp;
            }
        }

        List<ItemAnalysis> itemAnalysisList = new ArrayList<>();
        List<Character> answer;
        ItemAnalysis itemAnalysis = null;

        for (int c = 0; c < cols; c++) {
            itemAnalysis = new ItemAnalysis();
            answer = new ArrayList<>();
            for (int r = 0; r < rows; r++) {
                row = sheet.getRow(r);
                if (row == null || row.toString().isEmpty()) {
                    ShowErrorNotification.error("Remove all blank/empty rows after the last Item!");
                    return;
                } else {
                    //                   if(row != null){
                    cell = row.getCell(c);
                    if (cell == null || cell.toString().isEmpty()) {
                        ShowErrorNotification.error("Remove all blank/empty columns after the last student!");
                        return;
                    } else {
                        //                       if(cell != null){                           
                        if (c != 0) {
                            if (r == 0) {
                                itemAnalysis.setStudentNumber(cell.toString().trim());
                            } else {
                                answer.add(cell.toString().trim().charAt(0));
                            }
                        } else {
                            if (r != 0) {
                                totalItems++;
                            }
                        }
                    }
                }
            }
            if (c != 0) {
                itemAnalysis.setAnswer(answer);
                itemAnalysisList.add(itemAnalysis);
            }
        }

        if (tq.getCellItemIdByTQCoverageId(getTqCoverageId()).size() != totalItems) {
            ShowErrorNotification.error("Total Items do not MATCH!");
            totalItems = 0;
            return;
        }

        studentNoAndTotalScore = new HashMap<>();
        studentNoAndAnswer = new HashMap<>();
        totalItems = 1;
        new Thread() {

            @Override
            public void run() {
                totalData = itemAnalysisList.size();
                for (ItemAnalysis i : itemAnalysisList) {
                    try {
                        Thread.sleep(50);
                        studentNoAndTotalScore.put(i.getStudentNumber(), ItemAnalysisInterpretation
                                .getTotalScoresOfAllStudent(tqCoverageId, i.getAnswer()));
                        studentNoAndAnswer.put(i.getStudentNumber(), i.getAnswer());

                        getUI().access(new Runnable() {

                            @Override
                            public void run() {
                                if (totalItems < itemAnalysisList.size()) {
                                    analyze.setValue("Analyzing data.. "
                                            + CommonUtilities.roundOffToTwoDecimal((current / totalData) * 100)
                                            + "%");
                                    current++;
                                } else {
                                    analyze.setValue("Data analyzed... 100%");
                                    getLowerAndUpperGroupStudent(studentNoAndTotalScore);

                                    HorizontalLayout h = new HorizontalLayout();
                                    h.setWidth("100%");

                                    h.addComponent(viewTableProportion());
                                    h.addComponent(viewStudentsTotalScore());
                                    h.addComponent(approveItemAnalysis());
                                    v.addComponent(h);

                                    v.addComponent(itemAnalysisGridPanel());
                                }
                            }

                        });

                        totalItems++;
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FileUploadWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

        }.start();
        UI.getCurrent().setPollInterval(500);
    } catch (IOException ex) {
        Logger.getLogger(TQItemAnalysisUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.example.selenium.util.ApachePOIXLSReader.java

public static Object[][] excelRead(String filePath) throws Exception {
    File excel = new File(filePath);
    FileInputStream fis = new FileInputStream(excel);

    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet ws = wb.getSheet("Sheet1");

    int rowNum = ws.getLastRowNum() + 1;
    int colNum = ws.getRow(0).getLastCellNum();
    Object[][] data = new Object[(rowNum - 1)][colNum];

    int k = 0;/*  w w w  .  j a  v a2 s . c o m*/
    for (int i = 1; i < rowNum; i++) {
        HSSFRow row = ws.getRow(i);
        for (int j = 0; j < colNum; j++) {
            HSSFCell cell = row.getCell(j);
            Object value = cellToObject(cell);
            data[k][j] = value;
        }
        k++;
    }
    return data;
}