当前位置: 首页 > news >正文

清河网站建设西安网站建设网络推广

清河网站建设,西安网站建设网络推广,公司网站找哪家,上海突发事件文章目录 前言一、EasyExcel二、使用步骤1.引入jar包2.数据准备2.1 数据库 3.方法实例3.1 无实体的导入3.1.1 Controller3.1.2 Service3.1.3 Listener3.1.4 Utils3.1.5 无实体导入数据返回说明 3.2 无实体的导出3.2.1 无实体导出数据(这里只贴出关键代码,Service代码处理)3.2.2… 文章目录 前言一、EasyExcel二、使用步骤1.引入jar包2.数据准备2.1 数据库 3.方法实例3.1 无实体的导入3.1.1 Controller3.1.2 Service3.1.3 Listener3.1.4 Utils3.1.5 无实体导入数据返回说明 3.2 无实体的导出3.2.1 无实体导出数据(这里只贴出关键代码,Service代码处理)3.2.2 Controller3.2.2 无实体导出总结 原创不易望一键三连 (^ _ ^) 前言 今天产品提了个需求导入导的Excel文件表头根据数据库的配置来。 因为之前大部分的导入和导出都是有固定表头或者固定的模板来做导入导出这个需求。。。嗯搞起 一、EasyExcel EasyExcel前面已经有过介绍了这里不做具体介绍大家看EasyExcel官网: EasyExcel官网 这里主要参考EasyExcel不创建对象的读和不创建对象的写 二、使用步骤 1.引入jar包 dependencygroupIdcom.alibaba/groupIdartifactIdeasyexcel/artifactIdversion3.1.1/version/dependency2.数据准备 2.1 数据库 3.方法实例 3.1 无实体的导入 3.1.1 Controller RestController RequestMapping(/product) Slf4j Api(tags 产品控制器) public class ProductController {PostMapping(/v1/importCountryGroupConf)ApiOperation(value 批量导入国家分组配置, httpMethod POST)public ResponseBean importCountryGroupConf(RequestParam(file) MultipartFile file){try {return productService.importCountryGroupConf(file);} catch (Exception e) {log.error(国家分组配置导入报错具体报错信息{},e.getMessage(),e);return ResponseBean.buildFail(50002,导入失败!!!);}} }3.1.2 Service Slf4j Service public class ProductService extends BaseServiceProduct {public ResponseBeanVoid importCountryGroupConf(MultipartFile file) {// 文件解析及返回ListMapString, String readResults DynamicHeadImportUtils.importExcel(file);log.info(导入内容{}, JSONObject.toJSONString(readResults));// 获取所有国家分组ListCountryGroupConf groupConfList groupConfService.lambdaQuery().eq(CountryGroupConf::getDelFlag, DelFlagEnums.NORMAL.getCode()).list();// 将国家分组配置按照国家名称进行分组返回的Map,key为国家名称,value为国家分组配置的idMapString, Long countryGroupConfMap groupConfList.stream().collect(Collectors.toMap(CountryGroupConf::getGroupName, CountryGroupConf::getId));// 商品Code列表ListString productCodes new ArrayList();ListProduct updateCondition new ArrayList();for (int i 0; i readResults.size(); i) {int lineNum i 2;Product product new Product();ListLong groupConfIds new ArrayList();for (Map.EntryString, String entry : readResults.get(i).entrySet()) {String key entry.getKey();String value entry.getValue();if (SKU.equals(key)) {if (productCodes.contains(value)) {return ResponseBean.buildFail(第 lineNum 行,SKU: value 存在重复!!!);} else {product.setProductCode(value);productCodes.add(value);}} else {if (ObjectUtil.isNotNull(countryGroupConfMap.get(key)) StringUtils.isNotBlank(value)) {if (!可售.equals(value)) {return ResponseBean.buildFail(第 lineNum 行 key 分组请正确填写!!!);} else {groupConfIds.add(countryGroupConfMap.get(key));}}}}if (CollectionUtil.isNotEmpty(groupConfIds)) {product.setCountryGroupConfIds(groupConfIds);updateCondition.add(product);}}// 是否有属性不为成品的SKUListProduct productList this.getDao().queryByProductCodes(productCodes);if (CollectionUtil.isEmpty(productList)) {return ResponseBean.buildFail(SKU不存在,请确认数据是否正确!!!);}ListString filterResults productList.stream().filter(s - Integer.valueOf(s.getAttributeCode()) ! 1).map(Product::getProductCode).distinct().collect(Collectors.toList());if (CollectionUtil.isNotEmpty(filterResults)) {return ResponseBean.buildFail(SKU: String.join(,, filterResults) 属性不为成品!!!);}// 计算productCodes和filterResults的单差集ListString queryCodes productList.stream().map(Product::getProductCode).distinct().collect(Collectors.toList());ListString diff CollectionUtil.subtractToList(productCodes, queryCodes);if (CollectionUtil.isNotEmpty(diff)) {return ResponseBean.buildFail(SKU: String.join(,, diff) 不存在,请确认数据是否填写正确!!!);}// productList按照productCode分组MapString, Long productIdMap productList.stream().collect(Collectors.toMap(Product::getProductCode, Product::getId));// 更新产品信息updateCondition.forEach(x - {if (ObjectUtil.isNotEmpty(productIdMap.get(x.getProductCode()))) {Date now new Date();String nickName BaseContextHandler.getNickName();Product product new Product();product.setId(productIdMap.get(x.getProductCode()));product.setCountryGroupConfIds(x.getCountryGroupConfIds());product.setUpdateBy(nickName);product.setUpdateTime(now);getDao().update(product);// 日志ProductCodeLog codeLog new ProductCodeLog();codeLog.setProductId(product.getId());codeLog.setOperateType(批量更新);codeLog.setContent(修改可售国家/地区配置);codeLog.setCreateUser(nickName);codeLog.setCreateTime(now);productCodeLogMapper.insert(codeLog);}});return ResponseBean.buildSuccess();} }3.1.3 Listener /*** NoModelDataListener class.** author zs* program: naikai* description: EasyExcel_不创建对象的读_监听器* date 2024/10/21*/ Slf4j public class NoModelDataListener extends AnalysisEventListenerMapInteger, String {// 表头数据存储所有的表头数据private ListMapInteger, String headList new ArrayList();// 数据体private ListMapInteger, String dataList new ArrayList();Override // 这里会返回一行行的返回头public void invokeHeadMap(MapInteger, String headMap, AnalysisContext context) {// 存储全部表头数据log.info(获取表头Start);headList.add(headMap);log.info(获取表头End);}Override // 处理每一行数据public void invoke(MapInteger, String data, AnalysisContext analysisContext) {dataList.add(data);}Override // 全部处理结束执行public void doAfterAllAnalysed(AnalysisContext context) {}public ListMapInteger, String getHeadList() {return headList;}public ListMapInteger, String getDataList() {return dataList;}}3.1.4 Utils /*** ExcelDynamicHeadImportUtils class.** author zs* program: naikai* description: EasyExcel_动态表头导入_工具类* date 2024/10/21*/ Slf4j public class DynamicHeadImportUtils {/*** 动态表头导入功能_无实体** param file 文件* return*/public static ListMapString, String importExcel(MultipartFile file) {try {// Sept 1: 校验传入文件是否为空if (file null) {throw new CheckException(传入数据为空);}// Sept 2: 引入监听器(此处需注意,监听器不可被Spring管理)NoModelDataListener readListener new NoModelDataListener();// Sept 3: 开始处理excelEasyExcelFactory.read(file.getInputStream(), readListener).sheet(0).doRead();// 获取表头(判空)ListMapInteger, String headList readListener.getHeadList();if (CollectionUtil.isEmpty(headList)) {throw new CheckException(Excel表头不能为空);}// 获取表数据(判空)ListMapInteger, String dataList readListener.getDataList();if (CollectionUtil.isEmpty(dataList)) {throw new CheckException(Excel数据内容不能为空);}// 获取头部,取最后一次解析的列头数据MapInteger, String excelHeadIdxNameMap headList.get(headList.size() - 1);// 封装数据体ListMapString, String excelDataList new ArrayList();for (MapInteger, String dataRow : dataList) {HashMapString, String rowData new HashMap();excelHeadIdxNameMap.entrySet().forEach(columnHead - {rowData.put(columnHead.getValue(), dataRow.get(columnHead.getKey()));});excelDataList.add(rowData);}return excelDataList;} catch (Exception e) {log.error(解析文件失败{}, e.getMessage(), e);throw new RuntimeException(导入失败 e.getMessage());}} }3.1.5 无实体导入数据返回说明 参考3.1.2方法中的返回类型 3.2 无实体的导出 3.2.1 无实体导出数据(这里只贴出关键代码,Service代码处理) // 可用国家分组配置(动态表头数据来源)ListCountryGroupConf confs groupConfService.lambdaQuery().eq(CountryGroupConf::getDelFlag, com.smallrig.middleground.common.enums.DelFlagEnums.NORMAL.getCode()).orderByAsc(CountryGroupConf::getId).list();MapString, Long countryGroupConfMap confs.stream().collect(Collectors.toMap(CountryGroupConf::getGroupName, CountryGroupConf::getId));// 动态表头生成,第一列写死SKU,其他的根据查询的国家分组配置结果写入,查询结果是按照国家分组配置id排序(升序)ListListString dynamicHeads ListUtils.newArrayList();dynamicHeads.add(Arrays.asList(SKU));confs.forEach(x - dynamicHeads.add(Arrays.asList(x.getGroupName())));exportProduct.setCountryGroupConfDynamicHeads(dynamicHeads);// 商品的国家分组配置idsMapString, ListLong groupConfMap products.stream().filter(x - CollectionUtil.isNotEmpty(x.getCountryGroupConfIds())).collect(Collectors.toMap(Product::getProductCode, Product::getCountryGroupConfIds));// 动态数据的写入,注意表头和数据体的数据必须顺序一致没有数据用null代替否则就会出现错位问题ListListObject datas ListUtils.newArrayList();for (Map.EntryString, ListLong entry : groupConfMap.entrySet()) {String productCode entry.getKey();ListLong confIds entry.getValue();// 动态数据体ListObject dataList new ArrayList();// 表头第一列是SKU,所以对应数据体第一列必须是SKUdataList.add(productCode);// 根据表头的顺序依次写入对应数据,如果商品没有表头国家配置,则置空for (int i 1; i dynamicHeads.size(); i) {// 获取具体表头String head dynamicHeads.get(i).get(0);Long id countryGroupConfMap.get(head);// 判断商品的国家分组配置id是否包含了当前表头对应的国家分组配置id,如果有就写入可售,没有就置空if (confIds.contains(id)) {dataList.add(可售);}else {dataList.add(null);}}datas.add(dataList);}3.2.2 Controller RestController RequestMapping(/product) Slf4j Api(tags 产品控制器) public class ProductController { PostMapping(/v1/export)public ResponseBean export(HttpServletResponse response,HttpServletRequest requests, RequestBody ExportProductRequest request) throws Exception {try {//创建Excel文件File file new File(path / fileName);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}file.createNewFile();// 查询数据结果ExportProduct exportProduct productService.exportObjectV2(conversionRequest, Long.valueOf(currentUserId));// 使用EasyExcel写入数据try (OutputStream out new FileOutputStream(file)) {ExcelWriter excelWriter EasyExcel.write(out).build();// 可售国家地区WriteSheet sheet11 EasyExcel.writerSheet(10, 可售国家地区).head(exportProduct.getCountryGroupConfDynamicHeads()).build();excelWriter.write(exportProduct.getCountryGroupConfDatas(), sheet11);// 完成写入excelWriter.finish();// 修改down文件为成功downClient.updateDown(downId);log.info(成品编码异步导出end);}} catch (IOException e) {log.error(成品编码异步导出异常, e);throw new RuntimeException(e);}return ResponseBean.buildSuccess();} } 3.2.2 无实体导出总结 无实体导出关键在于表头和数据体的数据必须顺序一致没有数据用null代替否则就会出现错位问题 原创不易望一键三连 (^ _ ^)
http://www.dnsts.com.cn/news/56574.html

相关文章:

  • 网站建设免费视频教学网页设计尺寸单位
  • 营销型网站收费项目网源码
  • 南宁网站快深圳做网站的价格
  • 站长seo5118关键词查询工具
  • 财务管理做的好的门户网站怎么建立微信群
  • 电商网站开发书籍wordpress模版制作工具
  • 北京做百度网站建设专业做邯郸网站优化
  • 温州微网站制作公司哪家好怎样做班级网站
  • 免费主页空间申请网站电商网站建设费用价格
  • 网站虚拟机从头做有影响吗公司logo设计在线制作
  • 英茗网站建设北京效果好的网站推广
  • 做网站感想装饰公司 网站模板
  • 遵义网站建设公司招聘会议平台网站建设
  • 国内最好的网站建设公司wordpress 扫码登录
  • 网站的安全性建设网站建设系统设计报告
  • 模板建站服务器正规的计算机培训机构
  • 网站怎么做用户体验做网站图片要求
  • 店铺设计包含哪些内容seo网站快速排名
  • 建设部职业资格注册网站l网站建设
  • 做网站内页图片尺寸怎样做网站吸引客户
  • 网站能不能自己做丽江网站设计公司
  • 自己给公司做网站发文章用哪个平台比较好
  • 杭州网站建设 网站设计网站开发本地环境
  • 网站建设出现乱码网站登录人太多进不去怎么办
  • 做兼职的网站都有哪些工作学校官网网站建设的现状分析
  • 网站修改解析怎么做wordpress开发者模式
  • 网站虚拟空间购买移动网站二级域名m开头怎么做
  • 商务公司网站建设保险网站有哪些
  • 做网站建设拼多多货源一件代发平台
  • 如何组织公司做网站wordpress tinymce 字体