package com.hxzy.xazb.util;
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.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
Created by Sunshine on 2020/8/11.
*/
public class POIUtil {
// 扩展名
public final static String XLS = “xls”;
public final static String XLSX = “xlsx”;
/**
@param excelFile excel文件
@param startRow 读取数据的起始行, 行号从0开始
@return
@throws IOException
*/
public static List<List> readExcelFile(MultipartFile excelFile) throws Exception {
// 检查文件
String fileName = checkFile(excelFile);
List<List> lists = null;
if(fileName.endsWith(XLSX)){
lists = readXlsx(excelFile.getInputStream());
}else if(fileName.endsWith(XLS)){
lists = readXls(excelFile.getInputStream());
}
return lists;
}
/**
/**
xlsx文档
@return 错误提示类型 0-正常 1-存在不能为空的列 2-格式不对 3-空文件
*/
private static List<List> readXlsx(InputStream inputstream) throws Exception {
XSSFWorkbook hssfworkbook = new XSSFWorkbook(inputstream);
XSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);// 第一个工作表
List<List> list = new ArrayList<>();
if (hssfsheet != null) {
int totalrows = hssfsheet.getPhysicalNumberOfRows();// --获取sheet总行数
if (totalrows > 1)// 除了标题外,必须有数据
{
for (int i = 1; i < totalrows; i++) {
List rowList = new ArrayList<>();
XSSFRow hssfrow = hssfsheet.getRow(i);
// 获取每一行的总列数
int cellNum = hssfrow.getPhysicalNumberOfCells();
if (cellNum==0){
continue;
}
for(int j = 1; j < cellNum; j++){
XSSFCell cell = hssfrow.getCell(j);
String xssfCellValue = getXSSFCellValue(cell);
rowList.add(xssfCellValue);
}
list.add(rowList);
}
return list;
}
else{
return null;
}
}
return null;
}
/**
xls文档
@return 错误提示类型 0-正常 1-存在不能为空的列 2-格式不对 3-空文件
*/
private static List<List> readXls(InputStream inputstream) throws Exception {
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);// 第一个工作表
List<List> list = new ArrayList<>();
if (hssfsheet != null) {
int totalrows = hssfsheet.getPhysicalNumberOfRows();// --获取sheet总行数
if (totalrows > 1)// 除了标题外,必须有数据
{
for (int i = 1; i < totalrows; i++) {
List rowList = new ArrayList<>();
HSSFRow hssfrow = hssfsheet.getRow(i);
// 获取每一行的总列数
int cellNum = hssfrow.getPhysicalNumberOfCells();
if (cellNum==0){
continue;
}
for(int j = 1; j < cellNum; j++){
HSSFCell cell = hssfrow.getCell(j);
String xssfCellValue = getHSSFCellValue(cell);
rowList.add(xssfCellValue);
}
list.add(rowList);
}
return list;
}else{
return null;
}
}
return null;
}
/**
/**
遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/youjianbo_han_87/article/details/7393916