src/main/java/com/whyc/controller/SoftwareController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/pojo/Software.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/SoftwareService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/whyc/controller/SoftwareController.java
New file @@ -0,0 +1,42 @@ package com.whyc.controller; import com.whyc.dto.Response; import com.whyc.pojo.Software; import com.whyc.service.SoftwareService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; @Api(tags = "软件管理-新版") @RestController @RequestMapping("software") public class SoftwareController { @Autowired private SoftwareService service; @ApiModelProperty("excel解析") @PostMapping("excelParse") public Response excelParse(@RequestParam MultipartFile multipartFile) throws IOException, InvalidFormatException { Response<Object> response = new Response<>(); String name=multipartFile.getOriginalFilename(); assert name != null; if(!name.substring(name.length()-4).equals(".xls") && !name.substring(name.length()-5).equals(".xlsx")){ response.set(1,false,"文件解析错误:上传格式非excel格式"); }else{ List<Software> list = service.excelParse(multipartFile.getInputStream()); response.setII(1,true,list,"文件解析成功"); } return response; } } src/main/java/com/whyc/pojo/Software.java
New file @@ -0,0 +1,128 @@ package com.whyc.pojo; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModelProperty; import org.apache.ibatis.type.Alias; import java.util.Date; @TableName(schema = "db_doc",value = "tb_software") @Alias("Software") public class Software { private Integer id; private String fileName; @ApiModelProperty("软件类型") private String type; private String version; @ApiModelProperty("软件基于版本") private String basedVersion; @ApiModelProperty("负责人") private String owner; @ApiModelProperty("归档日期") private Date filingDate; @ApiModelProperty("适用机型-物料编码") private String applyMaterialCode; @ApiModelProperty("适用机型-规格型号") private String applyModel; @ApiModelProperty("发布说明") private String releaseNotes; private Date createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getBasedVersion() { return basedVersion; } public void setBasedVersion(String basedVersion) { this.basedVersion = basedVersion; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public Date getFilingDate() { return filingDate; } public void setFilingDate(Date filingDate) { this.filingDate = filingDate; } public String getApplyMaterialCode() { return applyMaterialCode; } public void setApplyMaterialCode(String applyMaterialCode) { this.applyMaterialCode = applyMaterialCode; } public String getApplyModel() { return applyModel; } public void setApplyModel(String applyModel) { this.applyModel = applyModel; } public String getReleaseNotes() { return releaseNotes; } public void setReleaseNotes(String releaseNotes) { this.releaseNotes = releaseNotes; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } } src/main/java/com/whyc/service/SoftwareService.java
New file @@ -0,0 +1,56 @@ package com.whyc.service; import com.whyc.pojo.Software; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; @Service public class SoftwareService { public List<Software> excelParse(InputStream inputStream) throws IOException, InvalidFormatException { List<Software> softwareList = new LinkedList<>(); Workbook workbook = null; workbook = WorkbookFactory.create(inputStream); inputStream.close(); //取第一个sheet表 Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); //固定5列 short cellNum = 5; //取固定部分值 Software common = new Software(); common.setFileName(sheet.getRow(2).getCell(2).getStringCellValue()); String typeStr = sheet.getRow(3).getCell(2).getStringCellValue(); String[] typeArr = typeStr.split(" "); //第8行开始,倒数第2行截止 for (int i = 0; i < lastRowNum+1; i++) { Software software = new Software(); //取第3列,第5列 for (int j = 1; j < cellNum; j++) { Row row = sheet.getRow(i); Cell cell = row.getCell(j); if(j == 2){ cell.setCellType(Cell.CELL_TYPE_STRING); } String cellValue = cell.getStringCellValue(); switch (j){ } } softwareList.add(software); } return softwareList; } }