JAVA获取Excel表的内容

使用的jar包是poi-3.9.jar,只可以读取2007以下的版本,后缀为:xsl

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 Test {
    public static void readExcel(File file){ 
        try { 
            InputStream inputStream = new FileInputStream(file); 
            String fileName = file.getName(); 
          Workbook wb = null; 
            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(); 
                    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) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO 自动生成 catch 块 
            e.printStackTrace(); 
        } 
    } 
 public static void main(String[] args) {
     File file = new File("F:/2.xls");  //使用的路径是自己本地的。
     readExcel(file);
     }

}
 

猜你喜欢

转载自blog.csdn.net/qq_38146131/article/details/81281660