前端组件- <hd-flex>
- <el-dialog v-model="isUploadDialog" width="50%" lock-scroll=false>
- <el-upload
- class="upload-demo"
- drag
- :action="url"
- :on-success="success"
- :on-error="error"
- :headers="uploadHeaders"
- :limit="1"
- :on-exceed="handleExceed"
- :file-list="fileList"
- accept=".xlsx,.xls">
- <i class="el-icon-upload"></i>
- <em>点击上传</em>
- 只能上传xlsx/xls文件,且不超过500kb
- </el-upload>
- </el-dialog>
- </hd-flex><br><br>//变量
复制代码- url: `${conf.BASE_URL}/core/ssqd/importS`,<br>isUploadDialog: false,<br>fileList: [],<br><br><br>// 方法
复制代码- //导入<br>async importS() {<br> this.fileList=[];<br> this.isUploadDialog=true;<br>},<br>success(response, file, fileList){<br> if(response.code=='500'){<br> this.$hd.message.error(response.errorBody.errorMessage);<br> }<br> if(response.code=='200'){<br> this.$hd.message.ok('导入成功!');<br> this.isUploadDialog=false;<br> this.$refs.table.onSearch();<br> }<br><br><br>},<br>error(err, file, fileList){<br> this.$hd.message.error(err);<br><br>},<br>handleRemove(file, fileList) {<br> console.log(file, fileList)<br>},<br>handlePreview(file) {<br> console.log(file)<br>},<br>handleExceed(files, fileList) {<br> this.$message.warning(<br> `当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${<br> files.length + fileList.length<br> } 个文件`<br> )<br>},<br>beforeRemove(file, fileList) {<br> return this.$confirm(`确定移除 ${file.name}?`)<br>},
复制代码 Java - @ApiOperation("上传")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "file",value = "文件",dataTypeClass = MultipartFile.class,required = true,paramType = "")
- })
- @PostMapping ("/importS")
- public RestResponse<String> uploadExcel(MultipartFile file)throws IOException {
- String HZ = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
- if(".xlsx.xls".indexOf(HZ) < 0){
- throw new BaseException("500","导入的文件类型不正确,只能导入Excel文件");
- }
- EasyExcel.read(file.getInputStream(), SsqdVO.class,new UploadListenerBySsqd(iSsqdService)).sheet() .doRead();;
- return new RestResponse<> ("ok");
- }
复制代码 javaBean- package com.hopedove.coreserver.vo.sygl;
- import com.alibaba.excel.annotation.ExcelProperty;
- import com.baomidou.mybatisplus.annotation.TableField;
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import java.io.Serializable;
- import java.math.BigDecimal;
- import java.util.Date;
- import com.hopedove.commons.vo.BaseModel;
- import lombok.Data;
- /**
- * 璇曠罕娓呭崟
- * @TableName T_JS_SYGL_SSQD
- */
- @TableName(value ="T_JS_SYGL_SSQD")
- @Data
- public class SsqdVO extends BaseModel implements Serializable {
- /**
- * 璇曠罕娓呭崟ID
- */
- @TableId
- private String SSQDID;
- /**
- * 坯布类型
- */
- @ExcelProperty(value ="试纱类型", index = 0)
- private String PBLX;
- /**
- * 布号
- */
- @ExcelProperty(value ="布号", index = 1)
- private String BH;
- /**
- * 支数
- */
- @ExcelProperty(value ="支数", index = 2)
- private String ZS;
- /**
- * 产地
- */
- @ExcelProperty(value ="产地", index = 3)
- private String CD;
- /**
- * 批号
- */
- @ExcelProperty(value ="批号", index = 4)
- private String PH;
- /**
- * 重量
- */
- @ExcelProperty(value ="重量", index = 5)
- private BigDecimal ZL;
- /**
- * 备注
- */
- @ExcelProperty(value ="备注", index = 6)
- private String REMARK;
- @TableField(exist = false)
- private String GY;
- }
复制代码 监听器: 判断上传表格是否符合要求- package com.hopedove.coreserver.service.impl.sygl;
- import com.alibaba.excel.context.AnalysisContext;
- import com.alibaba.excel.event.AnalysisEventListener;
- import com.alibaba.nacos.common.utils.CollectionUtils;
- import com.hedu.sweet.starter.utils.exception.BusinException;
- import com.hopedove.coreserver.service.sygl.ISsqdService;
- import com.hopedove.coreserver.vo.sygl.SsqdVO;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- public class UploadListenerBySsqd extends AnalysisEventListener<SsqdVO> {
- private ISsqdService iSsqdService;
- public UploadListenerBySsqd(ISsqdService iSsqdService) {
- this.iSsqdService = iSsqdService;
- }
- private List<SsqdVO> list = new ArrayList<>(100);
- @Override
- public void invoke(SsqdVO ssqdVO, AnalysisContext analysisContext) {
- //业务判断
- System.out.println("***"+ssqdVO+"***");
- list.add(ssqdVO);
- // if (list.size() > 100) {
- // wjgbpclService.saveData(list);//保存到数据库
- // list = ListUtils.newArrayListWithExpectedSize(100);
- // }
- }
- @Override
- public void doAfterAllAnalysed(AnalysisContext analysisContext) {
- if (CollectionUtils.isNotEmpty(list)) {
- System.out.println("***结束***");
- System.out.println(list);
- iSsqdService.saveData(list);
- }else{
- throw new BusinException("500","导入的文件为空,请填写信息后重新导入。");
- }
- }
- /**
- * 在这里进行模板的判断
- * @param headMap 存放着导入表格的表头,键是索引,值是名称
- * @param context
- */
- @Override
- public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
- String isNull = "";
- if (context.readRowHolder().getRowIndex() == 0) {
- String[] headList = {"试纱类型","布号","支数","产地","批号","重量","备注"};
- for (int i = 0; i < headList.length; i++) {
- try {
- if (!headMap.get(i).equals(headList[i])) {
- isNull = "导入模板不正确,请重新导入";
- break;
- }
- } catch (Exception e) {
- isNull = "导入模板不正确,请重新导入";
- break;
- }
- }
- }
- if(isNull!=""){
- throw new BusinException("500",isNull);
- }
- }
- }
复制代码 实现类 SQL oracle数据库批量新增- <insert id="insertSSQD" parameterType="list">
- insert all
- <foreach collection="list" item="item">
- <![CDATA[
- into T_JS_SYGL_SSQD
- (
- SSQDID,
- RSQDBH,
- BH,
- PBLX,
- PBMC,
- ZS,
- CREATER,
- CRENAME,
- UPDATER,
- BMXXID,
- BMMC,
- JGXXID,
- JGMC,
- ZTXXID,
- ZTMC
- ]]>
- <if test=" item.ZL != null and item.ZL != '' ">,ZL </if>
- <if test=" item.SH != null and item.SH != '' ">,SH </if>
- <if test=" item.ZFMYQ != null and item.ZFMYQ != '' ">,ZFMYQ </if>
- <if test=" item.XSYQ != null and item.XSYQ != '' ">,XSYQ </if>
- <if test=" item.SG != null and item.SG != '' ">,SG </if>
- <if test=" item.REMARK != null and item.REMARK != '' ">,REMARK </if>
- <if test=" item.CD != null and item.CD != '' ">,CD </if>
- <if test=" item.PH != null and item.PH != '' ">,PH </if>
- <if test=" item.SC != null and item.SC != '' ">,SC </if>
- ) VALUES(
- <![CDATA[
- #{item.SSQDID},
- #{item.RSQDBH},
- #{item.BH},
- #{item.PBLX},
- #{item.PBMC},
- #{item.ZS},
- #{item.CREATER},
- #{item.CRENAME},
- #{item.UPDATER},
- #{item.BMXXID},
- #{item.BMMC},
- #{item.JGXXID},
- #{item.JGMC},
- #{item.ZTXXID},
- #{item.ZTMC}
- ]]>
- <if test=" item.ZL != null and item.ZL != '' ">,#{item.ZL} </if>
- <if test=" item.SH != null and item.SH != '' ">,#{item.SH} </if>
- <if test=" item.ZFMYQ != null and item.ZFMYQ != '' ">,#{item.ZFMYQ} </if>
- <if test=" item.XSYQ != null and item.XSYQ != '' ">,#{item.XSYQ} </if>
- <if test=" item.SG != null and item.SG != '' ">,#{item.SG} </if>
- <if test=" item.REMARK != null and item.REMARK != '' ">,#{item.REMARK} </if>
- <if test=" item.CD != null and item.CD != '' ">,#{item.CD} </if>
- <if test=" item.PH != null and item.PH != '' ">,#{item.PH} </if>
- <if test=" item.SC != null and item.SC != '' ">,#{item.SC} </if>
- )
- </foreach>
- select * from dual
- </insert>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |