Java POI Excel 入门

来源:互联网 发布:电子科大网络 编辑:程序博客网 时间:2024/06/06 02:27
importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Iterator;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.poifs.filesystem.POIFSFileSystem;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row; publicclass main {     publicstatic void main(String[] args){        try{               InputStream input = newFileInputStream("D:\\接口.xls");               POIFSFileSystem fs = newPOIFSFileSystem(input);               HSSFWorkbook wb = newHSSFWorkbook(fs);               HSSFSheet sheet = wb.getSheetAt(0);               // Iterate over each row in the sheet               Iterator<Row> rows = sheet.rowIterator();               while(rows.hasNext()) {                HSSFRow row = (HSSFRow) rows.next();                System.out.println("Row #" + row.getRowNum());                // Iterate over each cell in the row and print out the cell"s                // content                Iterator<Cell> cells = row.cellIterator();                while(cells.hasNext()) {                 HSSFCell cell = (HSSFCell) cells.next();                 System.out.println("Cell #" + cell.getCellNum());                 switch(cell.getCellType()) {                 caseHSSFCell.CELL_TYPE_NUMERIC:                  System.out.println(cell.getNumericCellValue());                  break;                 caseHSSFCell.CELL_TYPE_STRING:                  System.out.println(cell.getStringCellValue());                  break;                 caseHSSFCell.CELL_TYPE_BOOLEAN:                  System.out.println(cell.getBooleanCellValue());                  break;                 caseHSSFCell.CELL_TYPE_FORMULA:                  System.out.println(cell.getCellFormula());                  break;                 default:                  System.out.println("unsuported sell type");                  break;                 }                }               }              }catch(IOException ex) {               ex.printStackTrace();              }     }}


0 0
原创粉丝点击