Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232474
  • 博文数量: 54
  • 博客积分: 2656
  • 博客等级: 少校
  • 技术积分: 1020
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-19 21:06
文章分类

全部博文(54)

文章存档

2016年(3)

2014年(8)

2013年(4)

2012年(2)

2011年(29)

2010年(8)

我的朋友

分类: Java

2011-03-01 10:17:00

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 (j == 0) {
                simNumber = cell.getContents();
            }
            if (j == 1) {
                termSeqId = cell.getContents();
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    rwb.close();
}

 

Excel 2007:


 

< import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
< import="org.apache.poi.xssf.usermodel.XSSFSheet"%>
< import="org.apache.poi.xssf.usermodel.XSSFRow"%>
< import="org.apache.poi.hssf.usermodel.HSSFRow"%>
< 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(j == 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();
                }


阅读(1490) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-05 16:17:27

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com