Java导入完整版模板

1.在pom引入jra包

<!--导入导出excel-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
    <exclusions>
        <exclusion>
            <groupId>stax</groupId>
            <artifactId>stax-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.1.导入模板(util文件)

import java.io.*;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.neusoft.unieap.base.entity.ExcelImportEntity;
import com.neusoft.unieap.base.entity.SmImportDataDetailTemp;
import com.neusoft.unieap.base.util.xxsFilter.JsoupUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 被解析的Excel最好是什么样的呢?
 * 单元格最好都是文本格式,保存数据前自己去转换,不用poi带的转换。
 * 第一列 和最后一列 必须是必填字段!!!这样的你用我这个Util,得到的List就很准确了,不会出现多余的行或列。
 *
 * @author TMACJ
 * @version 0.000000.002899
 */
@Slf4j
public class ImportExcelUtil {

    private InputStream inputStream;
    private ExcelImportEntity excelImportEntity;
    private final static String excel2003L = ".xls";    //2003- 版本的excel
    private final static String excel2007U = ".xlsx";   //2007+ 版本的excel
    static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static Integer[] yyyyMMdd1 = {14, 31, 57, 58, 179, 184, 185, 186, 187, 188};//格式为yyyy/mm/dd
    static Integer[] yyyyMMdd2 = {176};//格式为yyyy-mm-dd
    static Integer[] HHmmss = {20, 32, 190, 191, 192};
    static List<Integer> yyyyMMddList1 = Arrays.asList(yyyyMMdd1);
    static List<Integer> yyyyMMddList2 = Arrays.asList(yyyyMMdd2);
    static List<Integer> hhMMssList = Arrays.asList(HHmmss);

    public ImportExcelUtil(InputStream inputStream, ExcelImportEntity excelImportEntity) {
        this.inputStream = inputStream;
        this.excelImportEntity = excelImportEntity;
    }

    public ImportExcelUtil() {
    }

    // 描述:获取IO流中的数据,组装成List<List<Object>>对象
    public List<SmImportDataDetailTemp> getBankListByExcel() throws Exception {
        List<SmImportDataDetailTemp> mapList = new ArrayList<>();
        //创建Excel工作薄
        Workbook work = this.getWorkbook(inputStream, excelImportEntity.getFileType());
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        //遍历Excel中所有的sheet
        SmImportDataDetailTemp smImportDataDetailTemp = null;
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            log.info("遍历sheet");
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            sheet.setAutobreaks(true);
            String sheetName = sheet.getSheetName();
            row = sheet.getRow(0);
            if (row == null) {
                continue;
            }
            int totalCell = sheet.getRow(0).getPhysicalNumberOfCells();//标题行一共有多少列
            for (int j = 0; j < totalCell; j++) {
                cell = row.getCell(j);
                if (cell == null || "".equals(getCellData(cell).trim())) {
                    totalCell = j;
                    break;
                }
            }
            log.info("标题行一共多少totalCell");
            //遍历当前sheet中的所有行
            for (int j = excelImportEntity.getBeginRow() - 1; j < sheet.getLastRowNum() + 1; j++) {
                smImportDataDetailTemp = new SmImportDataDetailTemp();
                smImportDataDetailTemp.setSheetName(sheetName);
                smImportDataDetailTemp.setImportDataId(excelImportEntity.getImportId());
                smImportDataDetailTemp.setLineNo((j + 1) + "");
                smImportDataDetailTemp.setCreatedBy(excelImportEntity.getCreatedBy());
                row = sheet.getRow(j);
                if (validateRow(row)) {
                    continue;
                }
                if (row == null) {
                    continue;
                }
                for (int y = row.getFirstCellNum(); y < totalCell; y++) {
                    cell = row.getCell(y);
                    if (cell == null) {
                        cell = row.createCell(y);
                        cell.setCellValue("");
                    }
                    int columnIndex = y + 1;
                    Class m = smImportDataDetailTemp.getClass();
                    Method mm = m.getDeclaredMethod("setCol" + columnIndex, String.class);
                    mm.invoke(smImportDataDetailTemp, getCellData(cell));
                }
                mapList.add(smImportDataDetailTemp);
            }
            // 简单起见,这里只解析第一个工作簿!
            break;
        }
        inputStream.close();
        return mapList;
    }

    // 过滤空行,(其中一行的数据的确都为空,可是其原本的格式还在,并没有连带删除,这样计算出来的行数就不真实,比真实的大)
    private boolean validateRow(Row row) throws Exception {
        boolean flag = true;
        if (row != null) {
            for (Cell c : row) {
                if (c.getCellType() != Cell.CELL_TYPE_BLANK) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }

    // 描述:根据文件后缀,自适应上传文件的版本
    public Workbook getWorkbook(InputStream inStr, String fileType) throws Exception {
        Workbook wb = null;
        if (excel2003L.equals(fileType)) {
            wb = new HSSFWorkbook(inStr);  //2003-
        } else if (excel2007U.equals(fileType)) {
            wb = new XSSFWorkbook(inStr);  //2007+
        }
        return wb;
    }

    // 获取单元中值(字符串类型)
    public String getCellData(Cell cell) throws Exception {
        String cellValue = "";
        if (cell != null) {
            try {
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BLANK://空白
                        cellValue = "";
                        break;
                    case Cell.CELL_TYPE_NUMERIC: //数值型 0----日期类型也是数值型的一种
                        if (DateUtil.isCellDateFormatted(cell)) {
                            Integer format = (Integer) (int) cell.getCellStyle().getDataFormat();
                            if (yyyyMMddList2.contains(format)) {
                                sFormat = new SimpleDateFormat("yyyy-MM-dd");
                            } else if (hhMMssList.contains(format)) {
                                sFormat = new SimpleDateFormat("HH:mm:ss");
                            } else if (yyyyMMddList1.contains(format)) {
                                sFormat = new SimpleDateFormat("yyyy/MM/dd");
                            }
                            Date date = cell.getDateCellValue();
                            cellValue = sFormat.format(date);
                        } else {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            cellValue = replaceBlank(cell.getStringCellValue());
                            //Double numberDate = new BigDecimal(cell.getNumericCellValue()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//似乎还是有点问题
                            //cellValue = numberDate + "";
                        }
                        break;
                    case Cell.CELL_TYPE_STRING: //字符串型 1
                        cellValue = replaceBlank(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_FORMULA: //公式型 2
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        cellValue = replaceBlank(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_BOOLEAN: //布尔型 4
                        cellValue = String.valueOf(cell.getBooleanCellValue());
                        break;
                    case Cell.CELL_TYPE_ERROR: //错误 5
                        cellValue = "!#REF!";
                        break;
                }
            } catch (Exception e) {
                throw new Exception("读取Excel单元格数据出错:" + e.getMessage());
            }
        }
        return JsoupUtil.clean(cellValue);
    }

    public static String replaceBlank(String source) {
        String dest = "";
        if (source != null) {
            Pattern p = Pattern.compile("\t|\r|\n");
            Matcher m = p.matcher(source);
            dest = m.replaceAll("");
        }
        return dest.trim();
    }
}

2.2.导入实体

@Data
public class ExcelImportEntity {
    private String fileType; //文件类型 .xlsx/.xls
    private Integer beginRow;//开始读取行
    private String createdBy;//导入人Id
    private String importId;//导入id
}

2.3.数据保存的临时表

import lombok.Data;

@Data
public class SmImportDataDetailTemp {

    /**
     * ID
     */
    private String rowId;
    /**
     * 创建人
     */
    private String createdBy;
    /**
     * 创建时间
     */
    private String createdDate;
    /**
     * 创建程度代号
     */
    private String createdProgram;
    /**
     * 修改人
     */
    private String lastUpdBy;
    /**
     * 修改时间
     */
    private String lastUpdDate;
    /**
     * 修改程式代号
     */
    private String lastUpdProgram;
    /**
     * 修改次数
     */
    private String modificationNum;
    /**
     * 修改标志
     */
    private String tag;
    /**
     * 外键,关联导入数据主表的ROW_ID字段
     */
    private String importDataId;
    /**
     * 当前execl数据在第几行
     */
    private String lineNo;
    /**
     * 导入的execl数据在哪张sheet表中
     */
    private String sheetName;
    private String col1;
    private String col2;
    private String col3;
    private String col4;
    private String col5;
    private String col6;
    private String col7;
    private String col8;
    private String col9;
    private String col10;
    private String col11;
    private String col12;
    private String col13;
    private String col14;
    private String col15;
    private String col16;
    private String col17;
    private String col18;
    private String col19;
    private String col20;
    private String col21;
    private String col22;
    private String col23;
    private String col24;
    private String col25;
    private String col26;
    private String col27;
    private String col28;
    private String col29;
    private String col30;
    private String col31;
    private String col32;
    private String col33;
    private String col34;
    private String col35;
    private String col36;
    private String col37;
    private String col38;
    private String col39;
    private String col40;
    private String col41;
    private String col42;
    private String col43;
    private String col44;
    private String col45;
    private String col46;
    private String col47;
    private String col48;
    private String col49;
    private String col50;
    private String col51;
    private String col52;
    private String col53;
    private String col54;
    private String col55;
    private String col56;
    private String col57;
    private String col58;
    private String col59;
    private String col60;
    private String col61;
    private String col62;
    private String col63;
    private String col64;
    private String col65;
    private String col66;
    private String col67;
    private String col68;
    private String col69;
    private String col70;
    private String col71;
    private String col72;
    private String col73;
    private String col74;
    private String col75;
    private String col76;
    private String col77;
    private String col78;
    private String col79;
    private String col80;
    private String col81;
    private String col82;
    private String col83;
    private String col84;
    private String col85;
    private String col86;
    private String col87;
    private String col88;
    private String col89;
    private String col90;
    private String col91;
    private String col92;
    private String col93;
    private String col94;
    private String col95;
    private String col96;
    private String col97;
    private String col98;
    private String col99;
    private String col100;
    /**
     * 该行execl数据是否出错
     */
    private String isError;
    private String errorMessage;
    private String remark;
    private String useFlag;
    private String activeFlag;
    private String deleteFlag;
    private String code;
    private String msg;
    private String type;
}

2.4.接收前端传过来的值

import lombok.Data;

@Data
public class SmImportData  {
    /**
     * ID
     */
    private String rowId;
    /**
     * 创建人
     */
    private String createdBy;
    /**
     * 创建时间
     */
    private String createdDate;
    /**
     * 创建程度代号
     */
    private String createdProgram;
    /**
     * 修改人
     */
    private String lastUpdBy;
    /**
     * 修改时间
     */
    private String lastUpdDate;
    /**
     * 修改程式代号
     */
    private String lastUpdProgram;
    /**
     * 修改次数
     */
    private String modificationNum;
    /**
     * 修改标志
     */
    private String tag;
    /**
     * 关联系统模块表的MODULE_CODE字段
     */
    private String moduleCode;
    private String importDate;
    private String importNo;
    /**
     * A01        未导入
     A02        已导入暂时表(临时表)
     --A03        校验成功
     A04        校验未通过
     A05        已转入正式表
     */
    private String importStatus;
    /**
     * 外键,关联导入模板主表的ROW_ID字段
     */
    private String importTemplateId;
    /**
     * 导入execl文件的保存路径
     */
    private String importFilename;
    /**
     * 导入进临时表的execl数据总行数
     */
    private String recordCount;
    private String remark;
    private String useFlag;
    private String activeFlag;
    private String deleteFlag;
    private String templateCode;
    private String startRow;
}

3.service

// 上传人员清单
@Transactional
public Object uploadPersonnelList(MultipartFile mFile, HttpServletRequest request,
                                  SmImportData smImportData) {
    Map<String, Object> resutlMap = new HashMap<>();
    long startTime = System.currentTimeMillis();

    Map<String, String> map = new LinkedHashMap();
    String filePath = uploadExcelPath;
    String fileName = "";
    String filetype = "";
    File file = null;
    if (!mFile.isEmpty()) {
        try {
            //保存文件
            map = saveUploadFile(mFile, request, filePath);
            fileName = map.get("fileName");
            filetype = map.get("filetype");
            file = new File(filePath + fileName);
            log.info("文件Path:" + filePath + fileName);
            resutlMap = importPersonnelList(file, filetype, smImportData, startTime);
        } catch (Exception e) {
            log.error("" + e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            String msg = "上传失败";
            resutlMap.put("code", -1);
            String Message = e.getMessage();
            log.error("上传错误信息: " + Message);
            if ("Failed to find end of row/cell records".equals(Message)) {
                msg = "上传失败,文档在受保护状态,请关闭后上传";
            }
            if ("No valid entries or contents found, this is not a valid OOXML (Office Open XML) file".contains(Message)) {
                msg = "上传失败,文档类型错误,不是excel文件";
            }
            if (Message.contains("无效数字")) {
                msg = "上传失败,文字与数据库字段不匹配";
            }
            resutlMap.put("msg", msg);
        }
    } else {
        resutlMap.put("code", 0);
        resutlMap.put("msg", "请选择文件");
        resutlMap.put("data", "");
        return resutlMap;
    }
    return resutlMap;
}
public Map<String, Object> importPersonnelList(File file, String filetype, SmImportData smImportData, long startTime) throws Exception {
    Map<String, Object> resutlMap = new HashMap<>();
    String smImportDataId = smImportData.getRowId();
    int beginRow = Integer.parseInt(smImportData.getStartRow());
    InputStream inputStream = new FileInputStream(file);
    //获得excel数据
    ExcelImportEntity excelImportEntity = new ExcelImportEntity();
    excelImportEntity.setFileType(filetype);
    excelImportEntity.setImportId(smImportDataId);
    excelImportEntity.setCreatedBy(smImportData.getCreatedBy());
    excelImportEntity.setBeginRow(beginRow);
    ImportExcelUtil importExcelUtil = new ImportExcelUtil(inputStream, excelImportEntity);
    List<SmImportDataDetailTemp> smImportDataDetailTemps = importExcelUtil.getBankListByExcel();
    if (null != smImportDataDetailTemps && !smImportDataDetailTemps.isEmpty()) {

        for (SmImportDataDetailTemp sm : smImportDataDetailTemps) {
            sm.getCol1();//得到第一个字段的值
            sm.getCol1();//得到第二个字段的值
        }
        long endTime = System.currentTimeMillis();
        log.info("导入一共时间:" + (endTime - startTime));
        resutlMap.put("code", 1);
        resutlMap.put("msg", "上传成功");
        resutlMap.put("smImportDataId", smImportDataId);
        resutlMap.put("smImportData", smImportData);
    } else {
        resutlMap.put("code", -1);
        resutlMap.put("msg", "上传失败,人员清单不可为空");
    }
    return resutlMap;
}

4.controller

@PostMapping("/uploadPersonnelList")
public Object uploadPersonnelList(MultipartFile file,HttpServletRequest request,  SmImportData smImportData) throws Exception {
    return  excleService.uploadPersonnelList(file,request,smImportData);
}

5.前端调用导入

// 上传人员清单
$("#buttonList").on('click', "#uploadPersonnelList", function () {
    var companyId = $.trim($('#COMPANY').val());
    if (companyId == null || companyId == "") {
        layer.msg("请选择外包公司");
        return false;
    }else{
        $('#uploadPersonnels').trigger('click');
    }
});

// 上传事件
upload.render({ //允许上传的文件后缀
    elem: '#uploadPersonnels'
    , headers: {
        userId: localStorage.getItem("userId") ? localStorage.getItem("userId") : '',
        userName: userName,
        sgin: localStorage.getItem("sgin") ? localStorage.getItem("sgin") : '',
        userCode: localStorage.getItem("userCode") ? localStorage.getItem("userCode") : '',
        authToken: localStorage.getItem("authToken") ? localStorage.getItem("authToken") : ''
    }
    , url: getApi() + "excel/uploadPersonnelList"
    , data: {
        createdBy: localStorage.getItem("userId")
        , startRow: 2
        , billMonth: function () { // 动态值
            return $.trim($('#monthlyBill').val());
        }
        , companyId: function () { // 动态值
            return $.trim($('#COMPANY').val());
        }
    }
    , exts: 'xlsx|xls'
    , before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
        layer.load(1); //上传loading
    }
    , done: function (res, file) {
        layer.closeAll('loading'); //关闭loading
        layer.msg(res.msg);
        if (res.code == 1) {
            methods.initTable(res.smImportData.billMonth, res.smImportData.companyId);
        }
    }
    , error: function (index, upload) {
        layer.closeAll('loading'); //关闭loading
    }
});
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Java EasyExcel可以实现模板导入的功能。首先,你需要引入EasyExcel的依赖,可以在pom.xml中添加以下代码: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency> ``` 接下来,你需要创建一个监听器来处理导入的数据。监听器可以继承`AnalysisEventListener`并重写相应的方法。在`invoke`方法中,你可以获取到每一行的数据,并进行相应的处理。你可以按照以下步骤进行操作: 1. 创建一个实体类来存储导入的数据。 2. 创建一个监听器类,并继承`AnalysisEventListener`,重写`invoke`方法来处理每一行的数据。 3. 在监听器类中,创建一个方法来循环设置要添加到Excel的数据,并将其封装到一个List集合中。 4. 使用EasyExcel提供的`ExcelReaderBuilder`来构建一个`ExcelReader`对象。 5. 使用`ExcelReader`对象的`read`方法来读取Excel文件,传入监听器对象,以及要读取的Sheet和开始读取的行数。 6. 最后,通过监听器中的方法获取到处理后的数据。 以上是使用EasyExcel进行模板导入的基本步骤。你可以根据你的具体需求进行相应的处理和操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java操作EasyExcel实现导入导出入门](https://blog.csdn.net/IndexMan/article/details/130435246)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [使用easyexcel导入复杂的模板](https://blog.csdn.net/m0_72167535/article/details/127788576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值