package com.gateguard.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
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;
/**
* excel解析
* <p>
* <strong>支持解析的excel文档以第一行作为标题</strong>
* <p>
* 需要以下jar包:
* <li>poi-3.8.jar</li>
* <li>poi-ooxml.jar</li>
* <li>poi-ooxml-schemas.jar</li>
* </li>xmlbeans.jar</li>
* @author gfl
*
*/
public class ExcelReader {
private String filePath;
private String sheetName;
private Workbook workBook;
private Sheet sheet;
private List<String> columnHeaderList;
private List<List<String>> listData;
private List<Map<String,String>> mapData;
private boolean flag;
/**
* 加载文件
* @param filePath
* 文件全路径。如:"D:\\document\\test.xlsx"
* @param sheetName
* excel中的标签名。如:"Sheet1"
*/
public ExcelReader(String filePath, String sheetName) {
this.filePath = filePath;
this.sheetName = sheetName;
this.flag = false;
this.load();
}
/**
* 读取文件
*/
private void load() {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
workBook = WorkbookFactory.create(inStream);
sheet = workBook.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inStream!=null){
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取单元格数据
* @param cell
* @return
*/
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
/**
* 解析excel
*/
private void getSheetData() {
listData = new ArrayList<List<String>>();
mapData = new ArrayList<Map<String, String>>();
columnHeaderList = new ArrayList<String>();
int numOfRows = sheet.getLastRowNum() + 1;
for (int i = 0; i < numOfRows; i++) {
Row row = sheet.getRow(i);
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (i == 0){
columnHeaderList.add(getCellValue(cell));
}
else{
map.put(columnHeaderList.get(j), this.getCellValue(cell));
}
list.add(this.getCellValue(cell));
}
}
if (i > 0){
mapData.add(map);
}
listData.add(list);
}
flag = true;
}
/**
* 从集合形式中获取值
* @param row
* @param col
* @return
*/
public String getCellData(int row, int col){
if(row<=0 || col<=0){
return null;
}
if(!flag){
this.getSheetData();
}
if(listData.size()>=row && listData.get(row-1).size()>=col){
return listData.get(row-1).get(col-1);
}else{
return null;
}
}
/**
* 从map中获取值
* @param row
* @param headerName
* @return
*/
public String getCellData(int row, String headerName){
if(row<=0){
return null;
}
if(!flag){
this.getSheetData();
}
if(mapData.size()>=row && mapData.get(row-1).containsKey(headerName)){
return mapData.get(row-1).get(headerName);
}else{
return null;
}
}
/**
* 以集合形式返回表格解析数据
* @return
*/
public List<List<String>> listData(){
if(!flag) {
this.getSheetData();
}
return listData();
}
/**
* 以key-value形式返回表格解析数据
* @return
*/
public List<Map<String,String>> getMapData(){
if(!flag) {
this.getSheetData();
}
return mapData;
}
/**
* 获取第一行作为key
*
* @return
*/
public List<String> getColumnHeaderList(){
if(!flag) {
this.getSheetData();
}
return columnHeaderList;
}
public static void main(String[] args) {
ExcelReader eh = new ExcelReader("D:\\test.xlsx","Sheet1");
List<Map<String,String>> list = eh.getMapData();
for(int i=0;i<list.size();i++) {
Map<String,String> map=(Map<String,String>)list.get(i);
for(String key : map.keySet()) {
System.out.println(key+":"+"第"+(i+1)+"节:"+map.get(key));
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
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;
/**
* excel解析
* <p>
* <strong>支持解析的excel文档以第一行作为标题</strong>
* <p>
* 需要以下jar包:
* <li>poi-3.8.jar</li>
* <li>poi-ooxml.jar</li>
* <li>poi-ooxml-schemas.jar</li>
* </li>xmlbeans.jar</li>
* @author gfl
*
*/
public class ExcelReader {
private String filePath;
private String sheetName;
private Workbook workBook;
private Sheet sheet;
private List<String> columnHeaderList;
private List<List<String>> listData;
private List<Map<String,String>> mapData;
private boolean flag;
/**
* 加载文件
* @param filePath
* 文件全路径。如:"D:\\document\\test.xlsx"
* @param sheetName
* excel中的标签名。如:"Sheet1"
*/
public ExcelReader(String filePath, String sheetName) {
this.filePath = filePath;
this.sheetName = sheetName;
this.flag = false;
this.load();
}
/**
* 读取文件
*/
private void load() {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
workBook = WorkbookFactory.create(inStream);
sheet = workBook.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inStream!=null){
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取单元格数据
* @param cell
* @return
*/
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
/**
* 解析excel
*/
private void getSheetData() {
listData = new ArrayList<List<String>>();
mapData = new ArrayList<Map<String, String>>();
columnHeaderList = new ArrayList<String>();
int numOfRows = sheet.getLastRowNum() + 1;
for (int i = 0; i < numOfRows; i++) {
Row row = sheet.getRow(i);
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (i == 0){
columnHeaderList.add(getCellValue(cell));
}
else{
map.put(columnHeaderList.get(j), this.getCellValue(cell));
}
list.add(this.getCellValue(cell));
}
}
if (i > 0){
mapData.add(map);
}
listData.add(list);
}
flag = true;
}
/**
* 从集合形式中获取值
* @param row
* @param col
* @return
*/
public String getCellData(int row, int col){
if(row<=0 || col<=0){
return null;
}
if(!flag){
this.getSheetData();
}
if(listData.size()>=row && listData.get(row-1).size()>=col){
return listData.get(row-1).get(col-1);
}else{
return null;
}
}
/**
* 从map中获取值
* @param row
* @param headerName
* @return
*/
public String getCellData(int row, String headerName){
if(row<=0){
return null;
}
if(!flag){
this.getSheetData();
}
if(mapData.size()>=row && mapData.get(row-1).containsKey(headerName)){
return mapData.get(row-1).get(headerName);
}else{
return null;
}
}
/**
* 以集合形式返回表格解析数据
* @return
*/
public List<List<String>> listData(){
if(!flag) {
this.getSheetData();
}
return listData();
}
/**
* 以key-value形式返回表格解析数据
* @return
*/
public List<Map<String,String>> getMapData(){
if(!flag) {
this.getSheetData();
}
return mapData;
}
/**
* 获取第一行作为key
*
* @return
*/
public List<String> getColumnHeaderList(){
if(!flag) {
this.getSheetData();
}
return columnHeaderList;
}
public static void main(String[] args) {
ExcelReader eh = new ExcelReader("D:\\test.xlsx","Sheet1");
List<Map<String,String>> list = eh.getMapData();
for(int i=0;i<list.size();i++) {
Map<String,String> map=(Map<String,String>)list.get(i);
for(String key : map.keySet()) {
System.out.println(key+":"+"第"+(i+1)+"节:"+map.get(key));
}
}
}
}