1. 程式人生 > >POI實現Excel中資料的讀取

POI實現Excel中資料的讀取

所需依賴包:poi-3.17.jarpoi-ooxml-3.17.jarpoi-ooxml-schemas-3.17.jarxmlbeans-2.6.0.jarcommons-collections4-4.1.jar

依賴包下載地址:http://mvnrepository.com/artifact/org.apache.poi

實現程式碼:

package com.yc..util;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

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中讀取資料到List集合中

 * @author navy

 */

public class ReadExcelToDB {

       /**

        * 將Excel中的資料按順序讀取到一個集合中

        * @param fl

        * @return

        */

       @SuppressWarnings("resource")

       publicList<List<String>> importExcel(File fl) {

              List<List<String>> dataList = newArrayList<List<String>>();

              try {

                     Workbookworkbook = null; // excel物件

                     StringfileName = fl.getName().toLowerCase(); // 獲取檔名

 

                     if(fileName.endsWith("xls")){

                            workbook= new HSSFWorkbook(new FileInputStream(fl));

                     } else if(fileName.endsWith("xlsx")){

                            workbook= new XSSFWorkbook(new FileInputStream(fl));

                     } else {

                            thrownew  RuntimeException("您選擇的檔案不是一個Excel檔案...");

                     }

 

                     Sheetsheet = workbook.getSheet("Sheet1"); // 獲取excel中的第一個表格

                     int rows =sheet.getLastRowNum();// 獲取最後一行,即得到表格中的資料行數

                     if(rows==0){

                            thrownew  RuntimeException("表格中沒有資料...");

                     }

 

                     Row row =null; // 行物件

                     Iterator<Cell>cols = null;  // 列物件

                     List<String>list =null; // 用來存放一行資料

                    

                     for (int i= 1; i <= rows; i++){ // 迴圈獲取每一行的資料

                            row= sheet.getRow(i);

                            if(row != null){

                                   cols= row.cellIterator();

                                   list= new ArrayList<String>();

                                   while(cols.hasNext()){ // 迴圈獲取每一列的資料存到list中

                                          list.add(getCellString(cols.next()));

                                   }

                                   dataList.add(list);// 將這一行資料存到dataList中

                            }

                     }

 

              } catch(FileNotFoundException e) {

                     e.printStackTrace();

              } catch(IOException e) {

                     e.printStackTrace();

              }

              return dataList;

       }

 

       //把Excel表中 每個列的原有資料型別都轉換成String型別

       @SuppressWarnings("deprecation")

       private StringgetCellString(Cell cell) {

              if(cell == null)

                     return"";

              String cellSring= "";

 

              switch(cell.getCellType()) { 

              caseHSSFCell.CELL_TYPE_STRING: // 字串 

                     cellSring= cell.getStringCellValue();

                     break; 

              case HSSFCell.CELL_TYPE_NUMERIC:// 數字 

                     cellSring=String.valueOf(cell.getNumericCellValue());

                     break;

              caseHSSFCell.CELL_TYPE_BOOLEAN: // Boolean

                     cellSring=String.valueOf(cell.getBooleanCellValue());

                     break; 

              caseHSSFCell.CELL_TYPE_FORMULA: // 公式 

                     cellSring=String.valueOf(cell.getCellFormula());

                     break; 

              caseHSSFCell.CELL_TYPE_BLANK: // 空值 

                     cellSring="";

                     break; 

              caseHSSFCell.CELL_TYPE_ERROR: // 故障 

                     cellSring="";

                     break; 

              default: 

                     cellSring=""; 

                     break;

              }       

              return cellSring;

       }

}