最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java批量導(dǎo)入Excel數(shù)據(jù)超詳細(xì)實(shí)例

 更新時(shí)間:2023年08月23日 10:58:05   作者:java-jcm  
這篇文章主要給大家介紹了關(guān)于java批量導(dǎo)入Excel數(shù)據(jù)的相關(guān)資料,EXCEL導(dǎo)入就是文件導(dǎo)入,操作代碼是一樣的,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下

1.后臺(tái)導(dǎo)入代碼

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
  @ApiOperation(value = "以導(dǎo)入excel方式")
    @PostMapping(value = "/uuApplyUserInfo")
    public String importMonitor(@RequestParam MultipartFile file) throws Exception {
        if (file == null) {
            return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空");
        }
        ImportParams params = new ImportParams();
        params.setNeedVerify(true);//是否開啟校驗(yàn)
        params.setHeadRows(1); //頭行忽略的行數(shù)
        final ExcelImportService excelImportService = new ExcelImportService();
        ExcelImportResult excelImportResult = excelImportService.importExcelByIs(file.getInputStream(), YzLicensedUnit.class, params, false);
        //校驗(yàn)成功數(shù)據(jù)
        List<YzLicensedUnit> list = excelImportResult.getList();
        final Field failCollection = ExcelImportService.class.getDeclaredField("failCollection");
        failCollection.setAccessible(true);
        //校驗(yàn)失敗數(shù)據(jù)
        List<YzLicensedUnit> failList = (List) failCollection.get(excelImportService);
        if (list.size() == 0 && failList.size() == 0) {
            return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空");
        }
        if (failList.size() > 0){
            return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)與模板不一致");
        }
        //如果沒有錯(cuò)誤,可以存入數(shù)據(jù)庫
        if (list.size() >= 0 &&  StringUtil.isNotEmpty(list)) {
            //批量插入sql語句
            licensedUnitService.saveBatch(list);
        }else{
            return ValueUtil.isError("導(dǎo)入失敗,上傳文件數(shù)據(jù)不能為空");
        }
        return ValueUtil.toJson("導(dǎo)入成功");
    }

2.實(shí)體類

import cn.afterturn.easypoi.excel.annotation.Excel;
@Data
@TableName("數(shù)據(jù)庫表名")
public class YzLicensedUnit {
    //表格有的字段都要加Execl,并且name要跟表格字段一致
    @Excel(name = "持證面積/畝")
    @NotNull(message = "持證面積/畝不能為空")
    private BigDecimal acreage;
    @ApiModelProperty(value = "經(jīng)度")
    private String longitude;
    @ApiModelProperty(value = "緯度")
    private String latitude;
    //replace 表格傳來的值如果等于 是,則字段內(nèi)容插到表中的是0,否就是1
    @Excel(name = "苗種生產(chǎn)許可證持證單位",replace ={"是_0","否_1"})
    @NotNull(message = "苗種生產(chǎn)許可證持證單位不能為空")
    private String permit;
    @Excel(name = "持證編號(hào)")
    @NotNull(message = "持證編號(hào)不能為空")
    private String number;
    @Excel(name = "持證單位")
    @NotNull(message = "持證單位不能為空")
    private String entName;

2.1設(shè)置表格下拉選項(xiàng) 

3.vue前端導(dǎo)入功能代碼

      <el-upload
          :auto-upload="true"
          :multiple="false"
          :on-change="handleChange"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :file-list="fileList"
          :action="BASE_API"
          name="file"
          accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      >
        <el-button size="small" type="primary">批量導(dǎo)入</el-button>
      </el-upload>
export default {
  data() {
    return {
      fileList: [],
      //批量導(dǎo)入接口地址
      BASE_API: this.http_url + "/api/uuApplyUserInfo",
    };
  },
 methods: {
    handleChange() {
    },
    // 上傳多于一個(gè)文件時(shí)
    fileUploadExceed() {
      this.$message.warning("只能選取一個(gè)文件");
    },
    //上傳成功回調(diào):通信成功
    fileUploadSuccess(row) {
      //業(yè)務(wù)失敗
      if (row.code == '500') {
        this.$message.error(row.msg);
      } else {
        //業(yè)務(wù)成功
        this.$message.success(row.msg);
      }
      this.fileList = [];
      this.search();
    },
    //上傳失敗回調(diào):通信失敗
    fileUploadError(error) {
      error = JSON.parse(error.toString().substr(6));
      this.$message.error(error.msg);
    }
}

總結(jié) 

到此這篇關(guān)于java批量導(dǎo)入Excel數(shù)據(jù)的文章就介紹到這了,更多相關(guān)java批量導(dǎo)入Excel數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

庄浪县| 广南县| 宾阳县| 青浦区| 株洲市| 玉山县| 巨野县| 贵溪市| 潢川县| 江西省| 宜城市| 卢湾区| 贡山| 乃东县| 平南县| 登封市| 宁晋县| 临猗县| 遵义县| 曲靖市| 岳池县| 丰县| 广南县| 普陀区| 黄陵县| 大余县| 托克托县| 沙雅县| 五华县| 新干县| 镇原县| 绥中县| 龙门县| 卢湾区| 梁平县| 石林| 富顺县| 临城县| 定远县| 会理县| 东海县|