版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rhx_1989/article/details/88711496
1.封装类
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* excel导入公共类
*/
public class InExcelUtil {
/*遍历excel文件流,将数据放入list集合当中*/
public static List<List<String>> getExcelStringList(InputStream in, int cellCount) throws Exception{
Workbook work = WorkbookFactory.create(in);
if(work == null) throw new Exception("上传文件为空");
Row row = null;
Cell cell = null;
List<List<String>> list = new ArrayList<List<String>>();
//int sheetnum = work.getNumberOfSheets();//excel中sheet总数
Sheet sheet = work.getSheetAt(0);//获取excel中第一个sheet
if(sheet != null){
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
for(int j=sheet.getFirstRowNum();j<rowCount;j++){
row = sheet.getRow(j);
if(row == null || row.getFirstCellNum() == j) continue;
//int cellCount = row.getPhysicalNumberOfCells(); //是获取不为空的列个数
//int cellCount = row.getLastCellNum();//获取最后一个不为空的列是第几个
List<String> li = new ArrayList<String>();
for(int k=row.getFirstCellNum();k<cellCount;k++){
cell = row.getCell(k);
if(cell != null){
li.add(PubExcelUtil.getCellStringValue(cell).trim());
}else {
li.add("");
}
}
list.add(li);
}
}else {
throw new Exception("无法获取excel中Sheet数据");
}
in.close();
work.close();
return list;
}
/*获取标题值 单个sheet*/
public static List<String> getTitleList(InputStream in) throws Exception{
Workbook work = WorkbookFactory.create(in);
if(work == null) throw new Exception("上传文件为空");
Sheet sheet = work.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = null;
List<String> list = new LinkedList<String>();
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
for(int k=row.getFirstCellNum();k<=cellCount;k++){
cell = row.getCell(k);
if(cell != null){
String cellValue = PubExcelUtil.getCellStringValue(cell).trim();
if(!StringUtils.isBlank(cellValue)){
list.add(cellValue);
}
}
}
in.close();
work.close();
return list;
}
public static List<List<Object>> getExcelObjectList(InputStream in, int cellCount) throws Exception{
Workbook work = WorkbookFactory.create(in);
if(work == null) throw new Exception("上传文件为空");
Row row = null;
Cell cell = null;
List<List<Object>> list = new LinkedList<List<Object>>();
//int sheetnum = work.getNumberOfSheets();//excel中sheet总数
Sheet sheet = work.getSheetAt(0);//获取excel中第一个sheet
if(sheet != null){
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
for(int j=sheet.getFirstRowNum();j<rowCount;j++){
row = sheet.getRow(j);
if(row == null || row.getFirstCellNum() == j) continue;
//int cellCount = row.getPhysicalNumberOfCells(); //是获取不为空的列个数
//int cellCount = row.getLastCellNum();//获取最后一个不为空的列是第几个
List<Object> li = new LinkedList<Object>();
for(int k=row.getFirstCellNum();k<cellCount;k++){
cell = row.getCell(k);
if(cell != null){
li.add(PubExcelUtil.getCellObjectValue(cell));
}else {
li.add("");
}
}
list.add(li);
}
}else {
throw new Exception("无法获取excel中Sheet数据");
}
in.close();
work.close();
return list;
}
/*获取标题值 多个sheet*/
public static List<String> getTitleListAt(Sheet sheet) throws Exception{
//Workbook work = WorkbookFactory.create(in);
//if(work == null) throw new Exception("上传文件为空");
//Sheet sheet = work.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = null;
List<String> list = new LinkedList<String>();
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
for(int k=row.getFirstCellNum();k<=cellCount;k++){
cell = row.getCell(k);
if(cell != null){
String cellValue = PubExcelUtil.getCellStringValue(cell).trim();
if(!StringUtils.isBlank(cellValue)){
list.add(cellValue);
}
}
}
return list;
}
public static List<List<Object>> getExcelObjectListAt(Sheet sheet, int cellCount) throws Exception{
//int sheetnum = work.getNumberOfSheets();//excel中sheet总数
Row row = null;
Cell cell = null;
List<List<Object>> list = new LinkedList<List<Object>>();
if(sheet != null){
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
for(int j=sheet.getFirstRowNum();j<rowCount;j++){
row = sheet.getRow(j);
if(row == null || row.getFirstCellNum() == j) continue;
//int cellCount = row.getPhysicalNumberOfCells(); //是获取不为空的列个数
//int cellCount = row.getLastCellNum();//获取最后一个不为空的列是第几个
List<Object> li = new LinkedList<Object>();
for(int k=row.getFirstCellNum();k<cellCount;k++){
cell = row.getCell(k);
if(cell != null){
li.add(PubExcelUtil.getCellObjectValue(cell));
}else {
li.add("");
}
}
list.add(li);
}
}else {
throw new Exception("无法获取excel中Sheet数据");
}
return list;
}
}
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import java.text.SimpleDateFormat;
/**
* excel文件类型,数据类型处理
*/
public class PubExcelUtil {
private final static String excel_xls = ".xls";
private final static String excel_xlsx = ".xlsx";
public static Integer checkExcelType(String fileName) {
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(fileType.equals(excel_xls)){
return 1;
}else if(fileType.equals(excel_xlsx)){
return 2;
}else {
return 3;
}
}
public static String getCellStringValue(Cell cell){
String cellValue = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int cellType = cell.getCellType();
//DecimalFormat dfs = new DecimalFormat("0.0000");
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = sdf.format(cell.getDateCellValue()); //日期型
} else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = "错误";
break;
default:
cellValue = cell.getStringCellValue();
}
return cellValue;
}
public static Object getCellObjectValue(Cell cell){
Object cellValue = null;
int cellType = cell.getCellType();
//DecimalFormat dfs = new DecimalFormat("0.0000");
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = cell.getDateCellValue(); //日期型
} else {
cellValue = cell.getNumericCellValue(); //数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = "错误";
break;
default:
cellValue = cell.getStringCellValue();
}
return cellValue;
}
public static String getCellStringValueNew(Cell cell){
String cellValue = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CellType cellType = cell.getCellTypeEnum();
if(cellType.equals(CellType.STRING)){//文本
cellValue = cell.getStringCellValue();
}else if(cellType.equals(CellType.NUMERIC)){
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = sdf.format(cell.getDateCellValue()); //日期型
} else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
}else if(cellType.equals(CellType.BOOLEAN)){//布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
}else if(cellType.equals(CellType.BLANK)){//空白
cellValue = cell.getStringCellValue();
}else if(cellType.equals(CellType.ERROR)){//错误
cellValue = "错误";
}else if(cellType.equals(CellType.FORMULA)){//公式
cellValue = "公式";
}else {
cellValue = cell.getStringCellValue();
}
return cellValue;
}
public static Object getCellObjectValueNew(Cell cell){
Object cellValue = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CellType cellType = cell.getCellTypeEnum();
if(cellType.equals(CellType.STRING)){//文本
cellValue = cell.getStringCellValue();
}else if(cellType.equals(CellType.NUMERIC)){
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = cell.getDateCellValue(); //日期型
} else {
cellValue = cell.getNumericCellValue(); //数字
}
}else if(cellType.equals(CellType.BOOLEAN)){//布尔型
cellValue = cell.getBooleanCellValue();
}else if(cellType.equals(CellType.BLANK)){//空白
cellValue = cell.getStringCellValue();
}else if(cellType.equals(CellType.ERROR)){//错误
cellValue = "错误";
}else if(cellType.equals(CellType.FORMULA)){//公式
cellValue = "公式";
}else {
cellValue = cell.getStringCellValue();
}
return cellValue;
}
}
2.应用示例
public void importDepartment(MultipartFile file) {
try{
//创建输入流
InputStream in = null;
in = file.getInputStream();
InputStream title_in = file.getInputStream();
//将数据放入list集合当中
List<String> title = InExcelUtil.getTitleList(title_in);
List<List<Object>> list = InExcelUtil.getExcelObjectList(in, title.size());
/*判断字段数量是否正确*/
if(title.size() != 3){
System.out.println("数据不匹配,请按照模板导入");
}else {
try {
List<Map> list = new ArrayList<>();
int size1 = list.size();
for(int i=0; i<size1; i++){
List<Object> li = list.get(i);
String val1 = String.valueOf(li.get(0));
String val2 = String.valueOf(li.get(1));
String val3 = String.valueOf(li.get(2));
if(StringUtils.isBlank(val1)){
System.out.println("字段值不能为空");
}
if(errorList.size() == 0){
Map map = new HashMap();
map.put("dmCode",val1);
map.put("dmName",val2);
map.put("txName",val3);
list.add(map);
}
}
if(list.size() > 0){
System.out.println("部门体系信息同步成功");
}
}catch (Exception e){
e.printStackTrace();
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}