poi 读取excel 2003 及 2007 精简版

来源:互联网 发布:手机淘宝邮箱注册链接 编辑:程序博客网 时间:2024/06/07 14:22
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class read_Excel_to{
    /**  
     * 对外提供读取excel 的方法  
     */  
    public static List<String[]> readExcel(File file) throws IOException{
        List<String[]> list = new LinkedList<String[]>();
        InputStream inp;
        try {
            inp = new FileInputStream(file);
            if(! inp.markSupported()) {
                inp = new PushbackInputStream(inp, 8);
            }
            try {
                if(POIFSFileSystem.hasPOIFSHeader(inp)) {
                    list = read2003Excel(file);
                }
                if(POIXMLDocument.hasOOXMLHeader(inp)) {
                    list = read2007Excel(file);
                }
            } catch (IOException e) {
                 throw new IOException("不支持的文件类型");  
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }  
 
    /**  
     * 读取 office 2003 excel  
     *   
     * @throws IOException  
     * @throws FileNotFoundException  
     */  
    private static List<String[]> read2003Excel(File file)  
            throws IOException {  
        List<String[]> list = new LinkedList<String[]>();  
        HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
        HSSFSheet sheet = hwb.getSheetAt(0);  
        HSSFRow row = null;  
        HSSFCell cell = null;  
        for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) {
            row = sheet.getRow(i);
            if (row == null) {  
                continue;  
            }
            String[] str = new String[row.getLastCellNum()];
            for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                if(null == cell){
                   str[j] = null;
                }else{
                    cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                    str[j] = cell.getStringCellValue();
                }
            }  
            list.add(str);  
        }  
 
        return list;  
    }  
 
    /**  
     * 读取Office 2007 excel  
     */  
 
    private static List<String[]> read2007Excel(File file) throws IOException {  
        List<String[]> list = new LinkedList<String[]>();  
        XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
        // 读取第一章表格内容  
        XSSFSheet sheet = xwb.getSheetAt(0);  
        XSSFRow row = null;  
        XSSFCell cell = null;  
        for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) {  
            row = sheet.getRow(i);  
            if (row == null) {  
                continue;  
            }
            String[] str = new String[row.getLastCellNum()];
            for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                if(null == cell){
                    str[j] = null;
                 }else{
                     cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                     str[j] = cell.getStringCellValue();
                 }
               
            }  
            list.add(str);  
        }  
        return list;  
    }  
}
0 0
原创粉丝点击