进来利用POI操作Excel解析数据,遇到问题,最主要的就是版本的事情。03版的07版等更高版本。03版本的Excel后缀是 .xls,07+版本的后缀是 .xlsx,对于不同的版本要做不同的处理,否则会出现版本解析错误。话不多说,直接代码。
一:导入的jar包
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.record.pivottable.StreamIDRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
二:操作类
/**
*
* Title:DoExcel
* Description
*/
public class DoExcel {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
public static void main(String[] args) throws Exception {
DoExcel doExcel=new DoExcel();
File file=new File("C:\\Users\\Administrator\\Desktop\\testFiles\\2017-12-12_uid-file_path.xlsx");
String fileName=file.getName();
try {
InputStream inputStream=new FileInputStream(file);
ArrayList< List<Object>> list=(ArrayList<List<Object>>) doExcel.getBankListByExcel(inputStream, fileName);
for (int i = 0; i < list.size(); i++) {
List<Object> myList=list.get(i);
for (int j = 0; j < myList.size(); j++) {
String aa=(String) myList.get(j);
System.out.println(aa);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 得到Excel中的行内容
* @param path
* @return
* @throws Exception
*/
public static ArrayList<String> getExcelContentList(String path) throws Exception{
ArrayList< String> excelList=new ArrayList<>();
DoExcel doExcel=new DoExcel();
File file=new File(path);
String fileName=file.getName();
try {
InputStream inputStream=new FileInputStream(file);
ArrayList< List<Object>> list=(ArrayList<List<Object>>) doExcel.getBankListByExcel(inputStream, fileName);
for (int i = 0; i < list.size(); i++) {
List<Object> myList=list.get(i);
for (int j = 0; j < myList.size(); j++) {
String aa=(String) myList.get(j);
excelList.add(aa);
//System.out.println(aa);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return excelList;
}
/**
* 获取IO流中的数据,组装成List<List<Object>>对象
* @param in,fileName
* @return
* @throws Exception
*/
public List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{
List<List<Object>> list = null;
//创建Excel工作薄
Workbook work = null;
try {
work = this.getWorkbook(in,fileName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new ArrayList<List<Object>>();
//遍历Excel中第一页sheet
sheet = work.getSheetAt(0);
if (sheet == null) {
throw new Exception("Excel工作薄为空!");
}
//遍历当前sheet中的所有行
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if(row==null){
continue;
}
//遍历所有的列
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum()+1; y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
Object value = this.getCellValue(cell);
if(null != value){
li.add(value);
}
}
list.add(li);
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 根据文件后缀,自适应上传文件的版本
* @param inStr,fileName
* @return
* @throws Exception
*/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2003L.equals(fileType)){
wb = new HSSFWorkbook(inStr); //2003-
}else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}else{
throw new Exception("解析的文件格式有误!");
}
return wb;
}
/**
* 对表格中数值进行格式化
* @param cell
* @return
*/
public Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue());
}else{
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
}
}
楼主亲测,复制即可用,此类是将Excel的数据解析为双层list存储,外层list存储每个单元行,内层list存储单元格数据。