package com.whyc.controller; import com.github.pagehelper.PageInfo; import com.whyc.dto.Response; import com.whyc.pojo.MaterialCodeDictionary; import com.whyc.service.MaterialCodeDictionaryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.List; @RequestMapping("materialCodeDictionary") @RestController @Api(tags = "物料编码字典") public class MaterialCodeDictionaryController { @Autowired private MaterialCodeDictionaryService service; @GetMapping("codeList") @ApiOperation(value = "查询编码列表分页",notes = "level:0(查询所有),1(一级编码),2(二级编码)") public Response> getCodeList(@RequestParam int pageNum,@RequestParam int pageSize,@RequestParam int level){ PageInfo codeList = service.getCodeList(pageNum, pageSize, level); return new Response>().set(1,codeList); } //解析导入数据,只使用一次 @Deprecated @GetMapping("importData") public void importData() throws IOException, InvalidFormatException { List list = new LinkedList<>(); File fileTemp = new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\物料类别(1).xlsx"); Workbook workbook = null; workbook = WorkbookFactory.create(fileTemp); //取第一个sheet表 Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); //取第三行,并以第三行开始 Row rowTemp = sheet.getRow(1); short lastCellNum = rowTemp.getLastCellNum(); //先单独获取产品型号和版本号/分类 Row row = sheet.getRow(1); for (int l = 1; l < lastRowNum+1; l++) { Cell cellTemp = sheet.getRow(l).getCell(1); cellTemp.setCellType(Cell.CELL_TYPE_STRING); if(cellTemp.getStringCellValue().equals("")){ break; } MaterialCodeDictionary dictionary = new MaterialCodeDictionary(); for (int m = 0; m < lastCellNum; m++) { row = sheet.getRow(l); Cell cell = row.getCell(m); String cellValue = null; int cellValueInt = 0; Double cellValueDouble = null; /*if(m==0 || m==2){ cell.setCellType(Cell.CELL_TYPE_NUMERIC); cellValueDouble = cell.getNumericCellValue(); cellValueInt = cellValueDouble.intValue(); }else { cellValue = cell.getStringCellValue(); }*/ switch (m){ case 0:{dictionary.setCode((cell.getStringCellValue()));}break; case 1:{dictionary.setName(cell.getStringCellValue());}break; case 2:{dictionary.setParentCode((cell.getStringCellValue()));}break; } } list.add(dictionary); } service.insertBatch(list); } }