java读取Excel文件内容

//注本人使用poi-3.9.jar,大家借用一下示例时请自行网上下载,或者留言留下自己的邮箱地址。


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 

public class ReadExcel {
  public static void readExcel(File file){ 
         try { 
             InputStream inputStream = new FileInputStream(file); 
             String fileName = file.getName(); 
             Workbook wb = null; 
            // poi-3.9.jar  只可以读取2007以下的版本,后缀为:xsl
             wb = new HSSFWorkbook(inputStream);//解析xls格式 
           
             Sheet sheet = wb.getSheetAt(0);//第一个工作表  ,第二个则为1,以此类推...
              
             int firstRowIndex = sheet.getFirstRowNum(); 
             int lastRowIndex = sheet.getLastRowNum(); 
             for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){ 
                 Row row = sheet.getRow(rIndex); 
                 if(row != null){ 
                     int firstCellIndex = row.getFirstCellNum(); 
                    // int lastCellIndex = row.getLastCellNum(); 
                     //此处参数cIndex决定可以取到excel的列数。
                     for(int cIndex = firstCellIndex; cIndex < 3; cIndex ++){ 
                         Cell cell = row.getCell(cIndex); 
                         String value = ""; 
                         if(cell != null){ 
                             value = cell.toString(); 
                             System.out.print(value+"\t"); 
                        
                    
                     System.out.println(); 
                
            
         } catch (FileNotFoundException e) { 
             // TODO 自动生成 catch 块 
             e.printStackTrace(); 
         } catch (IOException e) { 
             // TODO 自动生成 catch 块 
             e.printStackTrace(); 
        
    
  public static void main(String[] args) {
  File file = new File("D:/test.xls");
  readExcel(file);
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值