java 读取excel文件

来源:互联网 发布:nz刷枪软件免费 编辑:程序博客网 时间:2024/05/16 17:22
Excel2003读取:

Workbook rwb = null;
String fileName = "e:\\test.xls";


try {
    File file = new File(fileName);

    rwb = Workbook.getWorkbook(file);
    Sheet result = rwb.getSheet(0);
    int rsColumns = result.getColumns();
    //获取Sheet表中所包含的总行数 

    int rsRows = result.getRows();
    System.out.print(rsColumns + "--" + rsRows);
    String simNumber = "", termSeqId = "";
    for (int k = 0; k < rsRows; k++) {
        for (int j = 0; j < rsColumns; j++) {
            Cell cell = result.getCell(j, k);
            if (== 0) {
                simNumber = cell.getContents();
            }
            if (== 1) {
                termSeqId = cell.getContents();
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    rwb.close();
}

 

Excel 2007:


 

<%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFSheet"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFCell"%> 

               //Excel文件处理

                try {
                    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(WEBROOT+"/WEB-INF/tmp/"+Calendar.getInstance().getTime().getTime()+"."+tail));
                    XSSFSheet sheet = wb.getSheetAt(0);//获取工作薄

                    int totalRows = sheet.getLastRowNum();//获取行数

                    
                    XSSFRow column = sheet.getRow(0);
                    int totalColumn = column.getLastCellNum();//获取列数

                    
                    for (int j = 0; j < totalRows; j++) {
                        if(== 0) continue;
                     XSSFRow sheetrows = sheet.getRow(j);
                     for (int k = 0; k < totalColumn; k++) {
                            XSSFCell cell = sheetrows.getCell((short) k);
                         if(cell!=null){
                             System.out.println("第" + j + "行 第" + k + "列的值是"+sheetrows.getCell(k).toString());
                         }
                        }
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }

原创粉丝点击