這幾天在工作當中由於遇到要把excel文件導入到項目,所以在參考了網上前輩們的資料後,自己動手寫了一個符合自己要求的方法。項目中所需要的jar文件爲:
xmlbeans-2.6.0.jar
poi-ooxml-3.13-20150929.jar
poi-3.13-20150929.jar
poi-ooxml-schemas-3.13-20150929.jar
jxl.jar
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
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.ss.usermodel.Cell;
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;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
* Excel工具類
* @author NO-1
*
*/
public class ExcelUtil {
/**
* 使用jxl插件讀取Excel(2003)文件
* @param fileurl 文件路徑
* @return list<HashMap<String,String>>
* map對象:第一行表頭爲key,從第三行開始單元內容爲value
*/
public static List<HashMap<String, String>> readExcelByJxl(String fileurl) {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm =null;
try {
//文件流
InputStream is = new FileInputStream(fileurl);
//獲得Excel文檔
Workbook rwb = Workbook.getWorkbook(is);
//獲得工作區Sheet
jxl.Sheet[] sheets=rwb.getSheets();
//獲得表頭字段
jxl.Cell[] cell0=sheets[0].getRow(0);
for(int i=0;i<sheets.length;i++){
Sheet sheet =sheets[i];
//獲得總行數
int rowsNum=sheet.getRows();
//遍歷每行
for(int j=2;j<rowsNum;j++){
//創建HashMap對象,存放每行的數據
hm = new HashMap<String, String>();
jxl.Cell[] cells=sheet.getRow(j);
for(int k=0;k<cells.length;k++){
//遍歷每行單元格
jxl.Cell cell=cells[k];
if(cell!=null){
//獲得單元格內容
String value=cell.getContents();
//存放到map中,表頭爲key,內容爲value
hm.put(cell0[k].getContents(), value);
}
}
//存放到list對象中(每行爲一個map對象)
list.add(hm);
}
}
//關閉流
is.close();
rwb.close();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
*
* @param fileName
* @return List<HashMap<String, String>>
* @throws IOException
* @throws FileNotFoundException
*/
public static List<HashMap<String, String>> readExcel2007(String fileName) throws FileNotFoundException, IOException {
List<HashMap<String,String>> list = new LinkedList<HashMap<String,String>>();
// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(new File(fileName)));
// 讀取第一章表格內容
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;
//獲得總行數
int counter = sheet.getPhysicalNumberOfRows();
//獲得第一行總列數(表頭)
row=sheet.getRow(0);
int cellNum = row.getPhysicalNumberOfCells();
String[] cells=new String[cellNum];
//將表頭存放到數組中,做爲map中的key使用
for(int i=0;i<cellNum;i++){
cell=row.getCell(i);
if(cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
cells[i]=cell.getStringCellValue();
}
}
HashMap<String ,String> map=null;
//遍歷行
for (int i = 0; i < counter; i++) {
row = sheet.getRow(i);
//遍歷單元格
map=new HashMap<String,String>();
for (int j = 0; j < cellNum; j++) {
cell = row.getCell(j);
if (cell != null) {
//設置單元格內容爲String類型
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
//存放到map對象中,表頭爲key,單元格內容爲value
map.put(cells[j], value);
// System.out.println("第" + (i + 1) + "行第" + (j + 1) + "列="+ value);
}
}
list.add(map);
}
//關閉流
xwb.close();
return list;
}
/**
*
* @param fileName
* @return List<HashMap<String, String>>()
* @throws FileNotFoundException
* @throws IOException
*/
public static List<HashMap<String, String>> readExcel2003(String fileName) throws FileNotFoundException, IOException {
List<HashMap<String,String>> list = new LinkedList<HashMap<String,String>>();
// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑
HSSFWorkbook xls = new HSSFWorkbook(new FileInputStream(new File(fileName)));
// 讀取第一章表格內容
HSSFSheet sheet = xls.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
//獲得總行數
int counter = sheet.getPhysicalNumberOfRows();
//獲得第一行總列數(表頭)
row=sheet.getRow(0);
int cellNum = row.getPhysicalNumberOfCells();
String[] cells=new String[cellNum];
//將表頭存放到數組中,做爲map中的key使用
for(int i=0;i<cellNum;i++){
cell=row.getCell(i);
if(cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
cells[i]=cell.getStringCellValue();
}
}
HashMap<String ,String> map=null;
//遍歷行
for (int i = 0; i < counter; i++) {
row = sheet.getRow(i);
//遍歷單元格
map=new HashMap<String,String>();
for (int j = 0; j < cellNum; j++) {
cell = row.getCell(j);
if (cell != null) {
//設置單元格內容爲String類型
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
//存放到map對象中,表頭爲key,單元格內容爲value
map.put(cells[j], value);
// System.out.println("第" + (i + 1) + "行第" + (j + 1) + "列="+ value);
}
}
list.add(map);
}
//關閉流
xls.close();
return list;
}
/**
* 解析Excel文件,2003和2007都可以
* @param fileName
* @return List<HashMap<String, String>>()
* @throws FileNotFoundException
* @throws IOException
*/
public static List<HashMap<String, String>> readExcel(String fileName) throws FileNotFoundException, IOException{
if(fileName==null||fileName.length()==0){
return new ArrayList<HashMap<String, String>>();
}
if(fileName.endsWith(".xls")){
return readExcel2003(fileName);
}else if(fileName.endsWith(".xlsx")){
return readExcel2007(fileName);
}else{
return new ArrayList<HashMap<String, String>>();
}
}
}