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.b510.excel.client.HSSFReadWrite.java

License:Apache License

/**
 * creates an {@link HSSFWorkbook} the specified OS filename.
 *///from w w w .  j  a  va  2 s. co  m
private static HSSFWorkbook readFile(String filename) throws IOException {
    return new HSSFWorkbook(new FileInputStream(filename));
}

From source file:com.bayareasoftware.chartengine.ds.ExcelDataSource.java

License:Apache License

public static HSSFWorkbook getWorkbook(InputStream is) throws IOException {
    try {//  w ww .ja v  a 2s.c om
        HSSFWorkbook wbk = new HSSFWorkbook(is);
        rewriteFormulas(wbk);
        return wbk;
    } finally {
        is.close();
    }
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java

License:Apache License

private static void runNew(String fileName) throws Exception {
    InputStream inp = new FileInputStream(fileName);
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
    ExcelExtractor xt = new ExcelExtractor(wb);

    xt.setFormulasNotResults(false);/*from   w  ww .  j a v  a2s . co m*/
    xt.setIncludeBlankCells(true);
    xt.setIncludeSheetNames(false);
    String text = xt.getText();
    String[] lines = StringUtil.splitCompletely(text, '\n');
    for (int i = 0; i < lines.length; i++) {
        System.out.println((i + 1) + ") " + lines[i]);
    }
    System.out.println("XLS: \n" + text);
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java

License:Apache License

private static void runOld(String fileName) throws Exception {
    InputStream is = new FileInputStream(fileName);
    POIFSFileSystem fs = new POIFSFileSystem(is);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);/* w ww.java2  s . c o m*/
    int firstRow = sheet.getFirstRowNum();
    int lastRow = sheet.getLastRowNum();
    p("first/last row: " + firstRow + "/" + lastRow);
    HSSFRow[] rows = new HSSFRow[lastRow + 1];
    int maxFirstCell = 0, maxLastCell = 0;
    for (int i = firstRow; i <= lastRow; i++) {
        HSSFRow r = sheet.getRow(i);
        if (r != null) {
            rows[i] = r;
            maxFirstCell = Math.max(maxFirstCell, r.getFirstCellNum());
            maxLastCell = Math.max(maxLastCell, r.getLastCellNum());

        }
    }
    p("maxFirstCell=" + maxFirstCell + ", maxLastCell=" + maxLastCell);

    StringBuilder table = new StringBuilder();
    table.append("<html><head><style>\n");
    table.append("body,td { font-family: monospaced; font-size: 12 }\n");
    table.append("</style></head>");
    table.append("<p>maxFirstCell=" + maxFirstCell + " maxLastCell=" + maxLastCell + "</p>");
    table.append("<table border=\"1\">");
    for (int i = firstRow; i <= lastRow; i++) {
        HSSFRow r = sheet.getRow(i);
        if (r == null) {
            System.err.println("NULL row at " + i);
        }
        table.append(row2string(r, maxLastCell));
    }
    table.append("</table></body></html>");
    File f = new File("sheet.html");
    Writer w = new FileWriter(f);
    w.write(table.toString());
    w.close();
    p("saved to " + f.getAbsolutePath());
}

From source file:com.beginner.core.utils.ObjectExcelRead.java

License:Apache License

/**
 * @param filepath ///*  w w w .ja  v a  2 s.c o m*/
 * @param filename //??
 * @param startrow //?
 * @param startcol //?
 * @param sheetnum //sheet
 * @return list
 */
@SuppressWarnings("deprecation")
public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol,
        int sheetnum) {
    List<Object> varList = new ArrayList<Object>();

    try {
        File target = new File(filepath, filename);
        FileInputStream fi = new FileInputStream(target);
        HSSFWorkbook wb = new HSSFWorkbook(fi);
        HSSFSheet sheet = wb.getSheetAt(sheetnum); //sheet 0
        int rowNum = sheet.getLastRowNum() + 1; //???

        for (int i = startrow; i < rowNum; i++) { //

            PageData varpd = new PageData();
            HSSFRow row = sheet.getRow(i); //
            int cellNum = row.getLastCellNum(); //????

            for (int j = startcol; j < cellNum; j++) { //

                HSSFCell cell = row.getCell(Short.parseShort(j + ""));
                String cellValue = null;
                if (null != cell) {
                    switch (cell.getCellType()) { // excel?????
                    case 0:
                        cellValue = String.valueOf((int) cell.getNumericCellValue());
                        break;
                    case 1:
                        cellValue = cell.getStringCellValue();
                        break;
                    case 2:
                        cellValue = cell.getNumericCellValue() + "";
                        // cellValue = String.valueOf(cell.getDateCellValue());
                        break;
                    case 3:
                        cellValue = "";
                        break;
                    case 4:
                        cellValue = String.valueOf(cell.getBooleanCellValue());
                        break;
                    case 5:
                        cellValue = String.valueOf(cell.getErrorCellValue());
                        break;
                    }
                } else {
                    cellValue = "";
                }
                varpd.put("var" + j, cellValue);
            }
            varList.add(varpd);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    return varList;
}

From source file:com.benasmussen.maven.plugin.i18n.io.ResourceReader.java

License:Apache License

public ResourceReader(InputStream is) throws IOException {
    workbook = new HSSFWorkbook(is);
}

From source file:com.beyondb.io.ExcelReader.java

/**
 * ?Excel//from   w  w  w. ja v  a  2s.com
 * @param InputStream
 * @return String 
 */
public String[] readExcelTitle(InputStream is) {
    try {
        fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    sheet = wb.getSheetAt(0);
    row = sheet.getRow(0);
    // 
    int colNum = row.getPhysicalNumberOfCells();
    System.out.println("colNum:" + colNum);
    String[] title = new String[colNum];
    for (int i = 0; i < colNum; i++) {
        //title[i] = getStringCellValue(row.getCell((short) i));
        title[i] = getCellFormatValue(row.getCell(i));
    }
    return title;
}

From source file:com.beyondb.io.ExcelReader.java

/**
 * ?Excel?/*from  w w w.j av a2  s .  c  o  m*/
 * @param InputStream
 * @return Map ???Map
 */
public Map<Integer, String> readExcelContent(InputStream is) {
    Map<Integer, String> content = new HashMap<Integer, String>();
    String str = "";
    try {
        fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    sheet = wb.getSheetAt(0);
    // 
    int rowNum = sheet.getLastRowNum();
    row = sheet.getRow(0);
    int colNum = row.getPhysicalNumberOfCells();
    // ,
    for (int i = 1; i <= rowNum; i++) {
        row = sheet.getRow(i);
        int j = 0;
        while (j < colNum) {
            // ???"-"??Stringreplace()?
            // ????javabean?javabean
            // str += getStringCellValue(row.getCell((short) j)).trim() +
            // "-";
            str += getCellFormatValue(row.getCell(j)).trim() + "    ";
            j++;
        }
        content.put(i, str);
        str = "";
    }
    return content;
}

From source file:com.binlist.binlistreader.BinlistReader.java

public static void main(String[] args) {
    String result[] = null;//from w  ww  .  ja  va  2 s.  c  o  m

    String folder = "/opt/";
    String sourceFileName = "binlist.xls";
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(folder + sourceFileName);

        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        Iterator<Cell> cellIterator = null;
        Row row = null;
        Cell cell = null;
        int cellNo = 0;
        String binno = "";
        String longUrl = "";
        String shortUrl = "";
        int rownum = 0;
        while (rowIterator.hasNext()) {
            rownum++;
            if (rownum == 1)
                continue;

            row = rowIterator.next();
            cellIterator = row.cellIterator();
            cellNo = 0;
            binno = "";
            longUrl = "";
            shortUrl = "";

            cell = row.getCell(4);
            if (cell != null && cell.getStringCellValue() != null) {
                binno = cell.getStringCellValue();
            }

            if (binno != null && binno.length() > 5) {
                result = postRequestV3(binno);
                System.out.println("rownum..:" + rownum + " binno..:" + binno + " result..:" + result.length);
                if (result != null) {
                    row.getCell(5).setCellValue(checkNull(result[0], ""));
                    row.getCell(6).setCellValue(checkNull(result[1], ""));
                    row.getCell(7).setCellValue(checkNull(result[2], ""));
                }
            }

        }

        System.out.println("rownum..:" + rownum);
        fis.close();
        FileOutputStream out = new FileOutputStream(folder + "newfile/" + sourceFileName);
        workbook.write(out);
        out.close();

        workbook.close();
        workbook = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(BinlistReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        fis = null;
    }

}

From source file:com.bizosys.dataservice.dao.ReadXLS.java

License:Apache License

@Override
protected List<String> populate() throws SQLException {

    checkCondition();/*from   w w  w.  j  a  va 2 s  . c om*/

    Workbook workbook = getWorkbook();
    Sheet sheet = workbook.createSheet();

    ResultSetMetaData md = rs.getMetaData();
    int totalCol = md.getColumnCount();
    String[] cols = createLabels(md, totalCol);

    try {

        if (null != templateFile) {
            File templateFileObject = new File(templateFile);
            if (templateFileObject.exists()) {
                Workbook templateWorkbook = new HSSFWorkbook(new FileInputStream(templateFileObject));
                Sheet templatesheet = templateWorkbook.getSheetAt(0);
                Iterator<Row> rowIterator = templatesheet.iterator();

                while (rowIterator.hasNext()) {
                    Row templateRow = rowIterator.next();
                    Row row = sheet.createRow(startRowIndex++);

                    Iterator<Cell> cellIterator = templateRow.cellIterator();
                    while (cellIterator.hasNext()) {
                        Cell templateCell = cellIterator.next();
                        Cell cell = row.createCell(templateCell.getColumnIndex());
                        cell.setCellType(templateCell.getCellType());
                        switch (templateCell.getCellType()) {
                        case Cell.CELL_TYPE_BLANK:
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(templateCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_ERROR:
                            cell.setCellValue(templateCell.getErrorCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(templateCell.getCellFormula());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            cell.setCellValue(templateCell.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(templateCell.getStringCellValue());
                            break;
                        }
                    }
                }
            } else {
                System.err.println("Can not read " + templateFileObject.getAbsolutePath());
            }

        }

        while (this.rs.next()) {
            createRecord(totalCol, cols, sheet);
        }
        workbook.write(out);
    } catch (IOException ex) {
        throw new SQLException(ex);
    }
    return null;
}