maven依赖:
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
工具类代码:
package com.cfcc.tsms.common.utils;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class PoiUtils {
static Workbook workbook;
public static void loadExcel(String filepath) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(filepath));
workbook = WorkbookFactory.create(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取cell内的值
* @param cell
* @return
*/
public static String getCellValue(Cell cell) {
String cellvalue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue();
break;
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_FORMULA://单元格公式格式
try {
cellvalue = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
cellvalue = String.valueOf(cell.getRichStringCellValue());
}
break;
case Cell.CELL_TYPE_ERROR:
cellvalue = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
cellvalue = "";
break;
default:
cellvalue = cell.toString().trim();
break;
}
}
return cellvalue.trim();
}
/**
* 获取Sheet --> Row 值
* @param workbook
* @return
*/
public static Map<Integer, LinkedList[]> getRowData(Workbook workbook){
Map<Integer, LinkedList[]> mapList = new HashMap<Integer, LinkedList[]>(); // 用于保存对应的行数据对象 //key-->sheetNum ; value-->DataByRow
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
LinkedList[] linkedLists = AnalysisBySheetNum(workbook, mapList, i);
}
return mapList;
}
/**
* 获取Sheet --> Row 值
* @param workbook
* @return
*/
public static Map<Integer, LinkedList[]> getRowData(Workbook workbook,int SheetNo){
Map<Integer, LinkedList[]> mapList = new HashMap<Integer, LinkedList[]>(); // 用于保存对应的行数据对象 //key-->sheetNum ; value-->DataByRow
LinkedList[] linkedLists = AnalysisBySheetNum(workbook, mapList, SheetNo);
return mapList;
}
private static LinkedList[] AnalysisBySheetNum(Workbook workbook, Map<Integer, LinkedList[]> mapList, int i) {
Sheet sheet = workbook.getSheetAt(i);
int RowNum = sheet.getLastRowNum()==0?sheet.getLastRowNum():sheet.getLastRowNum()+1;
if (RowNum<1) {
return null;
}
LinkedList[] linkedLists = new LinkedList[RowNum];
for (int j = 0; j < RowNum ; j++) {//行
Row row = sheet.getRow(j);
if (row!=null) {
linkedLists[j] = new LinkedList();//每行数据创建对应的LinkedList
for (int k = 0; k < row.getLastCellNum(); k++) {
Cell cell = row.getCell(k);
String value = getCellValue(cell);
if ((k==0) &&StringUtils.isBlank(value)) {
break;
}
linkedLists[j].add(value);
}
}
}
mapList.put(i, linkedLists);//key-->sheetNum ; value-->DataByRow
return linkedLists;
}
/**
*
* @param filepath
* Map<Integer, LinkedList[]> 解释: //key-->sheetNum ; value-->DataByRow
* @return
*/
public static Map<Integer, LinkedList[]> FileDataList(String filepath){
loadExcel(filepath);
return getRowData(workbook);
}
public static Map<Integer, LinkedList[]> FileDataListBySheetNo(String filepath,int SheetNo){
loadExcel(filepath);
return getRowData(workbook,SheetNo);
}
public static void main(String[] args) {
Map<Integer, LinkedList[]> map= FileDataList("d:\\财政专户.xls");
for (int i = 0; i < map.size(); i++) {//sheet
LinkedList[] LinkedList = map.get(i);
for (int j = 0; j < LinkedList.length; j++) {//sheet中行数据集合
System.out.println();
System.out.println("-------------第["+(j+1)+"]行-------------");
java.util.LinkedList LinkedList2 = LinkedList[j];
for (int k = 0; k < LinkedList2.size(); k++) {//cell数据集合
System.out.print(LinkedList2.get(k)+" ,");
}
}
}
}
}