java使用poi遍历Excel保存到容器中

来源:互联网 发布:阿里云短信接口开发 编辑:程序博客网 时间:2024/06/05 20:33





package com.liucb.excelutil;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;


public class ExcelUtil {
private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 


public static void main(String[] args){
URL path=ExcelUtil.class.getClassLoader().getResource("jins.xlsx");
File file = new File(path.getFile());
List<Map<String,Object>> list = readFromExcel(file);
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Map<String, Object> m = list.get(i);
for (Entry<String, Object> entry : m.entrySet()) {
  System.out.print(entry.getKey() + ":" + entry.getValue()+"\t");
}
System.out.println();
}
}


public static List<Map<String,Object>> readFromExcel(File file){
List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
String[] titles = {"名称","代码"};
String title = "";
try {
FileInputStream fis = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(fis);
//获取Sheet的数量
int sheetCount = workbook.getNumberOfSheets();
//遍历每个Sheet
for (int s = 0; s < sheetCount; s++) {
Sheet sheet = workbook.getSheetAt(s);
if(sheet!=null){
System.out.println("读取sheet:"+sheet.getSheetName()+"开始!");
//获取总行数
int rowCount = sheet.getPhysicalNumberOfRows();
//遍历每一行
for (int r = 0; r < rowCount; r++) {
Map<String,Object> map = new HashMap<String, Object>();//每一行数据保存到一个Map中
Row row = sheet.getRow(r);
if(row!=null){
//获取总列数
int cellCount = row.getPhysicalNumberOfCells();
//遍历每一列
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c);
if(cell!=null){
int cellType = cell.getCellType();
String cellValue = null;
switch(cellType){
case Cell.CELL_TYPE_STRING://文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC://日期、数字
if(DateUtil.isCellDateFormatted(cell)){
cellValue = fmt.format(cell.getDateCellValue());//日期
}else{
cellValue = String.valueOf(cell.getNumericCellValue());//数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型  
                           cellValue = String.valueOf(cell.getBooleanCellValue());  
                           break;  
                       case Cell.CELL_TYPE_BLANK: //空白  
                           cellValue = cell.getStringCellValue();  
                           break;  
                       case Cell.CELL_TYPE_ERROR: //错误  
                           cellValue = "错误";  
                           break;  
                       case Cell.CELL_TYPE_FORMULA: //公式  
                           cellValue = "错误";  
                           break;  
                       default:  
                           cellValue = "错误";  
}
if(c==0){
title=titles[0];
}
if(c==1){
title=titles[1];
}
map.put(title,cellValue);
}
}
   }
resultList.add(map);
 }
}
System.out.println("读取sheet:"+sheet.getSheetName()+"结束!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultList;
}
}