poi讀取excel文件

poi讀取excel文件,需要引入poi相關的jar包:poi-3.8.jar、poi-ooxml-3.8.jar、poi-ooxml-schemas-3.8.jar、xmlbeans-2.3.0.jar、stax-api-1.0.1.jar

package com.utils.poi;

import java.util.ArrayList;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 解析excel工作表 
 * 2015-04-17
 * @author guoyinggui
 */
public class PaserExcel {

	/**
	 * 讀取excel工作表方法
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
    public List<List<String>> readExcel(String filePath) throws IOException {
    	/**
    	 * rowData 用於存儲讀取的excel表的數據
    	 */
        List<List<String>> rowData = new ArrayList<List<String>>();
        FileInputStream in = null;
        Workbook workBook = null;
        try {
            in = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //讀取03版本的excel表
            if(filePath.toLowerCase().endsWith(".xls")){
        	workBook = new HSSFWorkbook(in);
            }
            //讀取07版本的excel表
            if(filePath.toLowerCase().endsWith(".xlsx")){
        	workBook = new XSSFWorkbook(in);
            }
            in.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
        // System.out.println(workBook.getNumberOfSheets());
	// 循環讀取工作表
        for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) {
            //獲得工作表對象
            Sheet sheet = workBook.getSheetAt(numSheet);
            if (sheet == null) {
                continue;
            }
            // 循環行Row
            // System.out.println("====頭部:"+sheet.getHeader());
            // System.out.println("====sheet名稱:"+sheet.getSheetName());
            // System.out.println("====獲取最後一行:"+sheet.getLastRowNum());
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            	//獲得工作表的行對象
                Row row = sheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }
                // 循環列Cell
                // System.out.println("====獲取最後一列:"+row.getLastCellNum());
                // arrCell:用於存儲行的所有列
                List<String> arrCell = new ArrayList<String>();
                for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
                    //獲得列對象
                    Cell cell = row.getCell(cellNum);
                    if (cell == null) {
                        continue;
                    }
                    //將列的值存入到List<String>集合中
                    arrCell.add(getValue(cell));
                }
                rowData.add(arrCell);
            }
        }
        return rowData;
    }

    private String getValue(Cell cell) {
        if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return String.valueOf(cell.getNumericCellValue());
        } else {
            return String.valueOf(cell.getStringCellValue());
        }
    }

    public static void main(String[] args) throws IOException {
        PaserExcel paser = new PaserExcel();
        List<List<String>> rowData = new ArrayList<List<String>>();
        rowData = paser.readExcel("D:\\test\\頻道管理.xlsx");
        for (List<String> cell : rowData) {
            for (String str : cell) {
                System.out.print(str + ", ");
            }
            System.out.println();
        }
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章