| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package com.mokamrp.privates.help;
- import com.mokamrp.privates.entity.VoltaHandle;
- import com.mokamrp.privates.utils.StringUtils;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @Author:huang
- * @Date:2019-09-10 10:29
- * @Description:excel文件解析类
- */
- public class Analysis {
- public Analysis() {
- throw new Error("工具类不允许实例化!");
- }
- /**
- * 获取并解析excel文件,返回一个二维集合
- * @param file 上传的文件
- * @return 二维集合(第一重集合为行,第二重集合为列,每一行包含该行的列集合,列集合包含该行的全部单元格的值)
- */
- public static VoltaHandle<ArrayList<Map<String,String>>> analysis(MultipartFile file, Map<Integer, String> field) {
- VoltaHandle<ArrayList<Map<String,String>>> res = new VoltaHandle<>();
- ArrayList<Map<String,String>> row = new ArrayList<>();
- if(field.size()<=0){
- res.setErr("表格每列绑定参数为空,请先绑定");
- return res;
- }
- //获取文件名称
- String fileName = file.getOriginalFilename();
- System.out.println(fileName);
- try {
- //获取输入流
- InputStream in = file.getInputStream();
- //判断excel版本
- Workbook workbook = null;
- if (judegExcelEdition(fileName)) {
- workbook = new XSSFWorkbook(in);
- } else {
- workbook = new HSSFWorkbook(in);
- }
- //获取第一张工作表
- Sheet sheet = workbook.getSheetAt(0);
- //从第二行开始获取
- for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
- //循环获取工作表的每一行
- Row sheetRow = sheet.getRow(i);
- if (sheetRow == null){
- continue;
- }
- //.如果每行首列为空 直接视为本行没数据
- if(Analysis.getCellValue(sheetRow.getCell(0)).isEmpty()){
- continue;
- }
- //循环获取每一列
- Map<String,String> cell = new HashMap<>();
- for (int j = 0; j < sheet.getRow(0).getPhysicalNumberOfCells(); j++) {
- if (field.get(j) == null){
- continue;
- }
- String[] param = field.get(j).split("\\|");
- String paramKey = param[0];
- String paramValue = null;
- String paramRule = null;
- if (param.length > 1){
- paramRule = param[1];
- }
- String cellStr = Analysis.getCellValue(sheetRow.getCell(j));
- //如果列数据为空,检查是否允许此列数据为空
- if (cellStr.isEmpty()) {
- if (paramRule == null){
- String collName = Analysis.getCellValue(sheet.getRow(0).getCell(j));
- res.setErr("第"+i+"行,("+collName+")列数据不可为空,请检查表格");
- return res;
- }else if (paramRule.equals("default")) {
- continue;
- }else {
- paramValue = paramRule;
- }
- }else{
- paramValue = cellStr;
- }
- if(j>=field.size()){
- break;
- }
- cell.put(paramKey,paramValue);
- }
- if (!cell.isEmpty()){
- //将装有每一列的集合装入大集合
- row.add(cell);
- }
- //关闭资源
- workbook.close();
- }
- } catch (FileNotFoundException e) {
- res.setErr("未找到文件");
- return res;
- } catch (IOException e) {
- res.setErr("上传时报");
- return res;
- }
- res.setData(row);
- return res;
- }
- /**
- * 判断上传的excel文件版本(xls为2003,xlsx为2017)
- * @param fileName 文件路径
- * @return excel2007及以上版本返回true,excel2007以下版本返回false
- */
- private static boolean judegExcelEdition(String fileName){
- if (fileName.matches("^.+\\.(?i)(xls)$")){
- return false;
- }else {
- return true;
- }
- }
- public static String getCellValue(Cell cell) {
- String value = "";
- if (cell != null) {
- // 以下是判断数据的类型
- switch (cell.getCellTypeEnum()) {
- case NUMERIC: // 数字
- cell.setCellType(CellType.STRING);
- value = cell.getStringCellValue() + "";
- break;
- case STRING: // 字符串
- value = cell.getStringCellValue();
- break;
- case BOOLEAN: // Boolean
- value = cell.getBooleanCellValue() + "";
- break;
- case FORMULA: // 公式
- value = cell.getCellFormula() + "";
- break;
- case BLANK: // 空值
- value = "";
- break;
- case ERROR: // 故障
- value = "非法字符";
- break;
- default:
- value = "";
- break;
- }
- }
- return value.trim();
- }
- }
|