版权声明:转载请注明原创地址,否则依法追究法律责任 https://blog.csdn.net/weixin_38964895/article/details/82963873
1.代码展示
package com.XXXX.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* 读取指定路径指定表工具类
* 使用完需在调用位置:ReadExcelDataUtil.strLists.clear(); 将集合清空
* 以实现不重启项目一次读取多个Excel表格
* @author TL-thinkive-48
*
*/
public class ReadExcelDataUtil {
private static String val = null;
private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
private static DecimalFormat df = new DecimalFormat("0");
public static List<List<String>> strLists = new ArrayList<List<String>>();
private static HSSFWorkbook wb;
/**
* 列数由形参传入,解决列情况: X,X,,X读取列数为3
*
* @param filePath
* @param colNum
* 表的列数
* @param index
* 表的序号-部分表的字段采用vlookup关联,需上传多张表格
* @return
*/
public static List<List<String>> readExcelData(String filePath, int colNum,
int index) {
FileInputStream file = null;
POIFSFileSystem ts = null;
try {
file = new FileInputStream(filePath);
ts = new POIFSFileSystem(file);
wb = new HSSFWorkbook(ts);
HSSFSheet sheet = wb.getSheetAt(index); // 获取表
int rowNum = sheet.getPhysicalNumberOfRows(); // 获取行数
HSSFRow row = null;
for (int i = 1; i < rowNum; i++) {
List<String> list = new ArrayList<String>();
row = sheet.getRow(i);
for (int j = 0; j < colNum; j++) {
HSSFCell cell = row.getCell(j);
list.add(getXcellVal(cell));
}
strLists.add(list);
}
return strLists;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
file.close();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取单元格的值
*
* @param cell
* @return String
*/
public static String getXcellVal(HSSFCell cell) {
if (null == cell) {
return "";
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
val = fmt.format(cell.getDateCellValue()); // 日期型
} else {
val = df.format(cell.getNumericCellValue()); // 数字型
}
break;
case HSSFCell.CELL_TYPE_STRING: // 文本类型
val = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
try {
val = String.valueOf(cell.getStringCellValue());
} catch (IllegalStateException e) {
val = String.valueOf(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_BLANK: // 空
val = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
val = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:// 错误
val = "ERROR..CHECK DATA";
break;
default:
val = cell.getRichStringCellValue() == null ? null : cell
.getRichStringCellValue().toString();
}
return val;
}
}
2.实例调用
List<List<String>> dataList = ReadExcelUtil.readExcelData("filePath",要读取的列数,要读取的第几张表);
以下表为例:
则,调用如下
List<List<String>> dataList = ReadExcelUtil.readExcelData("filePath",2,0);