赞
踩
- @RequestMapping("importEsComPartsAndPhotoExcel")
- public ActionResult getDataFromExcel(@RequestParam(value = "filename") MultipartFile file) throws IOException {
-
- String filename = file.getOriginalFilename();
- FileInputStream fis = (FileInputStream) file.getInputStream();
- //判断是否为excel类型文件
- if (!filename.endsWith(".xls") && !filename.endsWith(".xlsx")) {
- System.out.println("文件不是excel类型");
- }
- Workbook wookbook = null;
- Sheet sheet = null;
- try {
- //2007版本的excel,用.xlsx结尾
- wookbook = new XSSFWorkbook(fis);//得到工作簿
- } catch (IOException e) {
- e.printStackTrace();
- }
- Map<String, PictureData> maplist = null;
- sheet = (Sheet) wookbook.getSheetAt(0);
- // 判断用07还是03的方法获取图片
- if (filename.endsWith(".xls")) {
- maplist = getPictures1((HSSFSheet) sheet);
- } else if (filename.endsWith(".xlsx")) {
- maplist = getPictures2((XSSFSheet) sheet);
- }
- Map<String,String> map = null;
- try {
- map = printImg(maplist);
- } catch (Exception e) {
- Log.error(e.getMessage(),e);
- } finally {
- //释放map
- if (maplist != null) {
- maplist = null;
- }
- }
- //得到一个工作表
- //获得表头
- // Row rowHead = sheet.getRow(0);
-
- //获得数据的总行数
- int totalRowNum = sheet.getLastRowNum();
- //要获得属性
- String code = "";
-
- try{
- //获得所有数据
- for (int i = 0; i <= totalRowNum; i++) {
- //获得第i行对象
- Row row = sheet.getRow(i);
-
- Cell cell = row.getCell(0);
- if (cell == null) {
- continue;
- }
- cell.setCellType(CellType.STRING);
- code = cell.getStringCellValue().toString();
- if (StringUtils.isEmpty(code)) {
- break;
- }
- EsComParts ecd = escompartsService.getPartsByCode(code);
- if(ecd != null) {
- String partsPhoto = StringUtils.changeToString(ecd.getFparts_photo());
- if(StringUtils.equals(partsPhoto, "")) {
- String photoPath = StringUtils.changeToString(map.get(i+""));
- if(!StringUtils.equals(photoPath, "")) {
- File filePhoto = new File(photoPath);
- MultipartFile Photo = getMultipartFile(filePhoto);
-
- MultipartFile[] Photos = {Photo};
- if(Photos != null) {
- ecd.setFparts_photo(PhotoForOSSUtils.changePhotoToString(Photos));
- escompartsService.update(ecd);
- }
- }
- }
- }
- }
- }catch (Exception e){
- System.out.println("数据不符合规范");
- }
- //使用完成关闭
- wookbook.close();
- if (fis != null) {
- fis.close();
- }
- return ActionResult.Succeed("导入成功!");
- }
- public static void inputStreamToFile(InputStream ins, File file) {
- try {
- OutputStream os = new FileOutputStream(file);
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
- os.write(buffer, 0, bytesRead);
- }
- os.close();
- ins.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @SuppressWarnings("unused")
- @RequestMapping("importEsComPartsAndPhotoExcel")
- public ActionResult getDataFromExcel(@RequestParam(value = "filename") MultipartFile file) throws IOException {
-
- String filename = file.getOriginalFilename();
- FileInputStream fis = (FileInputStream) file.getInputStream();
- //判断是否为excel类型文件
- if (!filename.endsWith(".xls") && !filename.endsWith(".xlsx")) {
- System.out.println("文件不是excel类型");
- }
- Workbook wookbook = null;
- Sheet sheet = null;
- try {
- //2007版本的excel,用.xlsx结尾
- wookbook = new XSSFWorkbook(fis);//得到工作簿
- } catch (IOException e) {
- e.printStackTrace();
- }
- Map<String, PictureData> maplist = null;
- sheet = (Sheet) wookbook.getSheetAt(0);
- // 判断用07还是03的方法获取图片
- if (filename.endsWith(".xls")) {
- maplist = getPictures1((HSSFSheet) sheet);
- } else if (filename.endsWith(".xlsx")) {
- maplist = getPictures2((XSSFSheet) sheet);
- }
- Map<String,String> map = null;
- try {
- map = printImg(maplist);
- } catch (Exception e) {
- Log.error(e.getMessage(),e);
- } finally {
- //释放map
- if (maplist != null) {
- maplist = null;
- }
- }
- //得到一个工作表
- //获得表头
- // Row rowHead = sheet.getRow(0);
-
- //获得数据的总行数
- int totalRowNum = sheet.getLastRowNum();
- //要获得属性
- String code = "";
-
- try{
- //获得所有数据
- for (int i = 0; i <= totalRowNum; i++) {
- //获得第i行对象
- Row row = sheet.getRow(i);
-
- Cell cell = row.getCell(0);
- if (cell == null) {
- continue;
- }
- cell.setCellType(CellType.STRING);
- code = cell.getStringCellValue().toString();
- if (StringUtils.isEmpty(code)) {
- break;
- }
- EsComParts ecd = escompartsService.getPartsByCode(code);
- if(ecd != null) {
- String partsPhoto = StringUtils.changeToString(ecd.getFparts_photo());
- if(StringUtils.equals(partsPhoto, "")) {
- String photoPath = StringUtils.changeToString(map.get(i+""));
- if(!StringUtils.equals(photoPath, "")) {
- File filePhoto = new File(photoPath);
- MultipartFile Photo = getMultipartFile(filePhoto);
-
- MultipartFile[] Photos = {Photo};
- if(Photos != null) {
- ecd.setFparts_photo(PhotoForOSSUtils.changePhotoToString(Photos));
- escompartsService.update(ecd);
- }
- }
- }
- }
- }
- }catch (Exception e){
- System.out.println("数据不符合规范");
- }
- //使用完成关闭
- wookbook.close();
- if (fis != null) {
- fis.close();
- }
- return ActionResult.Succeed("导入成功!");
- }
-
- /**
- * 获取图片和位置 (xls)
- *
- * @param sheet
- * @return
- * @throws IOException
- */
- public Map<String, PictureData> getPictures1(HSSFSheet sheet) throws IOException {
- Map<String, PictureData> map = new HashMap<String, PictureData>();
- List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
- for (HSSFShape shape : list) {
- if (shape instanceof HSSFPicture) {
- HSSFPicture picture = (HSSFPicture) shape;
- HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
- PictureData pdata = picture.getPictureData();
- String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
- String proName = sheet.getRow(cAnchor.getRow1()).getCell(0).getStringCellValue();
- if(cAnchor.getCol1()==1){
- key = proName+="01";
- }else if(cAnchor.getCol1()==11){
- key = proName+="02";
- }
- map.put(key, pdata);
- }
- }
- return map;
- }
-
- /**
- * 获取图片和位置 (xlsx)
- *
- * @param sheet
- * @return
- * @throws IOException
- */
- public Map<String, PictureData> getPictures2(XSSFSheet sheet) throws IOException {
- Map<String, PictureData> map = new HashMap<String, PictureData>();
- List<POIXMLDocumentPart> list = sheet.getRelations();
- for (POIXMLDocumentPart part : list) {
- if (part instanceof XSSFDrawing) {
- XSSFDrawing drawing = (XSSFDrawing) part;
- List<XSSFShape> shapes = drawing.getShapes();
- for (XSSFShape shape : shapes) {
- XSSFPicture picture = (XSSFPicture) shape;
- XSSFClientAnchor anchor = picture.getPreferredSize();
- CTMarker marker = anchor.getFrom();
- int rowNum = marker.getRow();
- int colNum = marker.getCol();
- String key = marker.getRow() + "";
- // String key = marker.getRow() + "-" + marker.getCol();
- System.out.println(colNum);
- if(colNum==1){
- if(picture.getPictureData().getData().length<120*1024){
- map.put(key, picture.getPictureData());
- }else{
- // 主图大小不符合要求
- System.out.println(String.format("{}行{}列主图大小不符合要求",rowNum,colNum));
- }
- map.put(key, picture.getPictureData());
- }
- }
- }
- }
- return map;
- }
-
-
- /**
- * 删除文件
- *
- * @param fileName 文件名
- * @return 删除成功返回true,失败返回false
- */
- public static boolean deleteFile(String fileName) {
- File file = new File(fileName);
- if (file.isFile() && file.exists()) {
- file.delete();
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * 图片写出
- *
- * @param sheetList
- * @throws Exception
- */
- public static Map<String,String> printImg(Map<String, PictureData> sheetList) throws Exception {
- Map<String,String> map = new HashMap<>();
- Object key[] = sheetList.keySet().toArray();
- String filePath = "";
- File delPath = new File("/work/poi");
- // File delPath = new File("D://poi");
- boolean flag = true;
- // 删除文件夹下的所有文件和文件夹
- File[] fileStr = delPath.listFiles();
- for (int i = 0; i < fileStr.length; i++) { // 循环删除所有的子文件
- // 删除子文件
- if (fileStr[i].isFile()) {
- flag = deleteFile(fileStr[i].getAbsolutePath());
- if (!flag) {
- break;
- }
- }
- }
-
- delPath.delete();
- File files = new File("/work/poi");
- // File files=new File("D://poi");
- files.mkdirs();
- for (int i = 0; i < sheetList.size(); i++) {
- // 获取图片流
- PictureData pic = sheetList.get(key[i]);
- // 获取图片索引
- String picName = key[i].toString();
- // 获取图片格式
- String ext = pic.suggestFileExtension();
- byte[] data = pic.getData();
-
- filePath = "/work/poi/" + picName + "." + ext;
- // filePath = "D://poi/" + picName + "." + ext;
- map.put(picName, filePath);
-
- FileOutputStream out = new FileOutputStream(filePath);
- out.write(data);
- out.close();
- }
- return map;
- }
- public static MultipartFile getMultipartFile(File file) {
- FileItem item = new DiskFileItemFactory().createItem("file"
- , MediaType.MULTIPART_FORM_DATA_VALUE
- , true
- , file.getName());
- try (InputStream input = new FileInputStream(file);
- OutputStream os = item.getOutputStream()) {
- // 流转移
- IOUtils.copy(input, os);
- } catch (Exception e) {
- throw new IllegalArgumentException("Invalid file: " + e, e);
- }
-
- return new CommonsMultipartFile(item);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。