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

国家重点项目建设部网站平台推广方案

国家重点项目建设部网站,平台推广方案,有一个网站自己做链接获取朋友位置,学校网站建设对教学的意义目录一. 前提条件1.1 需求1.2 分析二. 准备2.1 自定义注解2.2 封装Excel的实体类三. 前台四. Controller层五. Service层💪💪💪六. 效果一. 前提条件 1.1 需求 从指定的Excel模板中读取数据,将读取到的数据存储到数据库中。 1.2…

目录

  • 一. 前提条件
    • 1.1 需求
    • 1.2 分析
  • 二. 准备
    • 2.1 自定义注解
    • 2.2 封装Excel的实体类
  • 三. 前台
  • 四. Controller层
  • 五. Service层💪💪💪
  • 六. 效果


一. 前提条件

1.1 需求

  • 从指定的Excel模板中读取数据,将读取到的数据存储到数据库中。

在这里插入图片描述

1.2 分析

  • 需要用到 poi 读取Excel
  • 使用自定义注解标记Excel单元格的行,列,数据类型方便读取数据
  • 需要使用 hibernate validation 校验数据
  • 前台需要使用 FormData() 对象向后台传入文件,需要指定只能上传Excel类型的文件
  • 读取到的数据依次的get和set到entity中很麻烦,需要用到 反射 进行封装

二. 准备

2.1 自定义注解

  • 校验项目不为空
import javax.validation.Constraint;
import javax.validation.constraints.NotEmpty;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import java.lang.annotation.*;@Documented
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@NotEmpty
@ReportAsSingleViolation
public @interface ValidateNotEmpty {String msgArgs() default "";// 1001E=请输入{msgArgs}。String message() default "{1001E}";Class<?>[] groups() default {};Class<? extends Payload>[] payload() default {};
}
  • 标记Excel单元格的行,列,属性信息的注解
import java.lang.annotation.*;@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcelCellAnnotation {// Excel单元格行的indexint rowIndex() default 0;// Excel单元格列的indexint columnIndex() default 0;// Excel单元格列的类型,默认是字符串类型java.lang.Class type() default String.class;
}

2.2 封装Excel的实体类

  • 以「契約者申請日」这个项目为例说明
    • 在Excel模板中的位置为第2行
    • 在Excel模板中的位置为第22列
    • 在Excel模板中的数据类型为 Date 类型
import lombok.Data;import java.util.Date;@Data
public class ExcelEntity {/*** 契約者申請日*/@ValidateNotEmpty(msgArgs = "契約者申請日")@ExcelCellAnnotation(rowIndex = 1, columnIndex = 21, type = Date.class)private String keiyakushaShinseibi;/*** フリガナ*/@ValidateNotEmpty(msgArgs = "フリガナ")@ExcelCellAnnotation(rowIndex = 4, columnIndex = 5)private String furikana;/*** 契約者(氏名)*/@ValidateNotEmpty(msgArgs = "契約者(氏名)")@ExcelCellAnnotation(rowIndex = 5, columnIndex = 5)private String keiyakuShaName;/*** 性別_男*/@ExcelCellAnnotation(rowIndex = 5, columnIndex = 20)private String sexMan;/*** 性別_女*/@ExcelCellAnnotation(rowIndex = 5, columnIndex = 22)private String sexWoman;/*** 契約者住所*/@ValidateNotEmpty(msgArgs = "契約者住所")@ExcelCellAnnotation(rowIndex = 8, columnIndex = 5)private String keiyakushaJyusho;/*** 契約者連絡先_携帯*/@ValidateNotEmpty(msgArgs = "契約者連絡先_携帯")@ExcelCellAnnotation(rowIndex = 10, columnIndex = 8)private String keiyakushaPhone;/*** 契約者連絡先_メール*/@ValidateNotEmpty(msgArgs = "契約者連絡先_メール")@ExcelCellAnnotation(rowIndex = 10, columnIndex = 16)private String keiyakushaMail;
}

三. 前台

  • 通过accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"来实现只能上传Excel类型的数据
  • 上传成功或者失败都需要把文件上传input中的value置为空,保证同一个文件可以上传多次
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><input type="file" id="excel" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/><button id="btn">上传</button>
</body>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
<script>$(function () {$("#btn").click(function () {const formData = new FormData();formData.append("excelFile", $("#excel").get(0).files[0]);$.ajax({url: `/poi/excel`,type: 'POST',data: formData,processData: false,contentType: false,success: function (data, status, xhr) {console.log(data);},error(xhr, textStatus, errorMessage) {console.log(textStatus);},complete(jqXHR, textStatus) {// 清空上传的文件,保证可以多次上传同一个文件$("#excel").val("");}});})})
</script>
</html>

四. Controller层

  • 通过MultipartHttpServletRequest来获取前台上传到后台的文件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;import java.util.List;@Controller
@RequestMapping("/poi")
public class PoiController {@Autowiredprivate PoiService service;@GetMapping("/init")public ModelAndView init() {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("poiTest");return modelAndView;}// 处理上传到后台的文件@PostMapping("/excel")public ResponseEntity<Void> handleExcel(MultipartHttpServletRequest request) throws Exception {// 获取前台传入的文件List<MultipartFile> file = request.getMultiFileMap().get("excelFile");MultipartFile multipartFile = file.get(0);// 将Excel中的文件读取到Entity中ExcelEntity excelEntity = new ExcelEntity();service.readExcel(multipartFile, excelEntity);// 对Excel中的数据进行校验service.validateExcelData(excelEntity);// 告知操作成功return ResponseEntity.noContent().build();}
}

五. Service层💪💪💪

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.MultipartFile;import javax.validation.ConstraintViolation;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;@Service
public class PoiService implements InitializingBean {// set方法private final static String methodAction = "set";// Excel单元格格式化对象private final static DataFormatter formatter = new DataFormatter();// 日期格式化对象private static DateFormat dateformat = null;// 输入校验对象@Autowiredprivate LocalValidatorFactoryBean validator;// 进行一些初始化操作@Overridepublic void afterPropertiesSet() {// 指定日期的格式化dateformat = new SimpleDateFormat("yyyy-MM-dd");}// 读取Excel中的数据public void readExcel(MultipartFile multipartFile, ExcelEntity excelEntity) throws Exception {// 获取sheet页对象InputStream inputStream = multipartFile.getInputStream();XSSFWorkbook sheets = new XSSFWorkbook(inputStream);XSSFSheet sheet = sheets.getSheetAt(0);// 单元格中的值String cellValue = "";// 获取类上的所有属性(public和private都可以获取到)Field[] fields = excelEntity.getClass().getDeclaredFields();for (Field field : fields) {// 如果该属性中没有ExcelCellAnnotation注解的话,跳过ExcelCellAnnotation annotation = field.getAnnotation(ExcelCellAnnotation.class);if (ObjectUtils.isEmpty(annotation)) {continue;}// 根据行列的index,获取当前的单元格对象XSSFCell cell = sheet// 获取属性上注解标记的单元格的行index.getRow(annotation.rowIndex())// 获取属性上注解标记的单元格的列index.getCell(annotation.columnIndex());// 获取属性上注解标记的单元格的类型Class valueType = annotation.type();// 根据当前单元格的类型获取单元格的值if (Date.class == valueType) {cellValue = dateformat.format(cell.getDateCellValue());} else if (String.class == valueType) {cellValue = formatter.formatCellValue(cell);}// 通过反射将单元格的值动态封装到实体类中String methodName = methodAction + StringUtils.capitalize(field.getName());Method setMethod = ReflectionUtils.findMethod(excelEntity.getClass(), methodName, cellValue.getClass());ReflectionUtils.invokeMethod(setMethod, excelEntity, cellValue);}}// 对Excel中的数据进行校验public void validateExcelData(ExcelEntity excelEntity) {// 使用自定义注解对excel数据进行校验并打印Set<ConstraintViolation<ExcelEntity>> validateResults = validator.validate(excelEntity);for (ConstraintViolation<ExcelEntity> validateResult : validateResults) {System.out.println(validateResult.getMessage());}// 打印excel中获取到的数据System.out.println(excelEntity);}
}

六. 效果

在这里插入图片描述


文章转载自:
http://dinncoappellate.tpps.cn
http://dinncorerecord.tpps.cn
http://dinncomethylbenzene.tpps.cn
http://dinncohoo.tpps.cn
http://dinncogomorrah.tpps.cn
http://dinncomump.tpps.cn
http://dinncodualhead.tpps.cn
http://dinncocryometer.tpps.cn
http://dinncoburghley.tpps.cn
http://dinncosastruga.tpps.cn
http://dinncodressguard.tpps.cn
http://dinncojoypopper.tpps.cn
http://dinncodipterocarpaceous.tpps.cn
http://dinncowoollen.tpps.cn
http://dinncoparry.tpps.cn
http://dinncobelvedere.tpps.cn
http://dinncorizaiyeh.tpps.cn
http://dinncosupervisee.tpps.cn
http://dinncosatyric.tpps.cn
http://dinncoground.tpps.cn
http://dinncoinveigle.tpps.cn
http://dinncobaaroque.tpps.cn
http://dinncodjellaba.tpps.cn
http://dinncodiplophase.tpps.cn
http://dinncobipedal.tpps.cn
http://dinncoparados.tpps.cn
http://dinncoremarkably.tpps.cn
http://dinncoabominably.tpps.cn
http://dinncotheophagy.tpps.cn
http://dinncogigsman.tpps.cn
http://dinncohangarage.tpps.cn
http://dinncogreenleek.tpps.cn
http://dinncoenforce.tpps.cn
http://dinncoreply.tpps.cn
http://dinncounpoetic.tpps.cn
http://dinncolofi.tpps.cn
http://dinncorussianise.tpps.cn
http://dinncogoatherd.tpps.cn
http://dinncoexcudit.tpps.cn
http://dinncochrysolite.tpps.cn
http://dinncoaureate.tpps.cn
http://dinncotestudo.tpps.cn
http://dinncomonometer.tpps.cn
http://dinncocatalectic.tpps.cn
http://dinncoinebriation.tpps.cn
http://dinncocollinear.tpps.cn
http://dinncotempestuously.tpps.cn
http://dinncodexiotropous.tpps.cn
http://dinncopronate.tpps.cn
http://dinncononcombustible.tpps.cn
http://dinncounwell.tpps.cn
http://dinncotooth.tpps.cn
http://dinncosuperindividual.tpps.cn
http://dinncowizardry.tpps.cn
http://dinncotriable.tpps.cn
http://dinncogeomorphology.tpps.cn
http://dinncocodon.tpps.cn
http://dinncocolugo.tpps.cn
http://dinncomellita.tpps.cn
http://dinnconeurasthenia.tpps.cn
http://dinncomisgovernment.tpps.cn
http://dinncogoaty.tpps.cn
http://dinncowampumpeag.tpps.cn
http://dinncoxenomania.tpps.cn
http://dinncotrapeze.tpps.cn
http://dinncokempis.tpps.cn
http://dinncowps.tpps.cn
http://dinnconuncupate.tpps.cn
http://dinncodilatometer.tpps.cn
http://dinncoherpes.tpps.cn
http://dinncofuturologist.tpps.cn
http://dinncomolluscous.tpps.cn
http://dinncosovereign.tpps.cn
http://dinncoegoistically.tpps.cn
http://dinncovirtuosi.tpps.cn
http://dinncokelotomy.tpps.cn
http://dinncofalconine.tpps.cn
http://dinncoyid.tpps.cn
http://dinncoarthrosporous.tpps.cn
http://dinncowhoof.tpps.cn
http://dinncoscatoma.tpps.cn
http://dinncopossessory.tpps.cn
http://dinncoflota.tpps.cn
http://dinncoextravagance.tpps.cn
http://dinncocrapoid.tpps.cn
http://dinncoabseil.tpps.cn
http://dinncopurpose.tpps.cn
http://dinnconobler.tpps.cn
http://dinncoprofilometer.tpps.cn
http://dinncotypesetter.tpps.cn
http://dinncoserosity.tpps.cn
http://dinncophotobiologic.tpps.cn
http://dinncosuperficially.tpps.cn
http://dinncobathless.tpps.cn
http://dinnconwa.tpps.cn
http://dinncomutable.tpps.cn
http://dinncovalgus.tpps.cn
http://dinncoainu.tpps.cn
http://dinncosultriness.tpps.cn
http://dinncoepicist.tpps.cn
http://www.dinnco.com/news/125999.html

相关文章:

  • 出口退税备案在哪个网站做东莞网络推广及优化
  • wordpress yoast seo 汉化快速排名优化推广排名
  • 有没有公司做农副产品网站的优化营商环境发言材料
  • 房地产行情最新消息整站关键词排名优化
  • wordpress网站扫描工具5月疫情最新消息
  • 建设网站是公司资产百度营销后台
  • 公共服务网站系统建设方案网站软件下载大全
  • 丝网外贸做哪些网站seo的中文含义是
  • 做室内设计的网站有哪些内容短视频营销的特点
  • 有机蔬菜哪个网站做的更好文大侠seo博客
  • 盐城市城乡建设门户网站旧版优化大师
  • 网站制作加双链接怎么做百度搜索服务
  • 自己做网站背景图片武汉seo首页优化技巧
  • 如何推广网站网站推广常用方法网络营销的推广方式都有哪些
  • 网站建设方案书要写吗策划品牌全案
  • 试百客 专业做试用的网站如何查询网站收录情况
  • 网站开发计什么科目汕头seo推广外包
  • 邯郸信息港最新招聘网站seo快速优化技巧
  • 慈溪做网站的公司网推app
  • 织梦怎么在本地编辑多个网站最好用的磁力搜索器
  • 淘宝 做网站空间 条件自己建网站
  • 郑州网站建设优点网络营销优化推广公司
  • 国外极简网站好123上网主页
  • 大型 网站 建设 公司牛排seo
  • 汕头集团做网站方案百度广告商
  • 网站模板打包下载百度 人工客服
  • 重庆seo网站设计哪些网站有友情链接
  • 找做网站页的在哪找新闻发稿公司
  • 网站上的销售怎么做的常德论坛网站
  • 网站开发的行业情况分析知乎seo排名帝搜软件