zoukankan      html  css  js  c++  java
  • 解压zip并解析excel

    <dependency>
    <groupId>poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
    </dependency>
    <dependency>
    <groupId>poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
    </dependency>
    <dependency>
    <groupId>poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.15</version>
    </dependency>
    public static ParseLicenseReturn uploadLicense(File file)  {
            String filename = file.getName();
            ParseLicenseReturn ret = new ParseLicenseReturn();
            Map<String, FileInfoManageM> fileInfos = new HashMap<String, FileInfoManageM>();
            List<String> esns = new ArrayList<String>();
            String fileType = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(Locale.US);
            // 上传的文件格式不正确
            int total = 0;
            int failNum = 0;
            // 如果是zip文件
            if(fileType.equals("zip")){
                String desPath = TEMP_DIR + File.separator + UUID.randomUUID().toString().replaceAll("-", "");
                try{
                    FileCommonUtils.unZipFile(file, desPath);
                    File desFile = new File(desPath);
                    List<File> fileList = FileCommonUtils.getSubFiles(desFile);
                    for (File oneFile : fileList){
                        if (oneFile.getName().toLowerCase().endsWith(".xls") || oneFile.getName().toLowerCase().endsWith(".xlsx") ) {
                            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
                            try {
                                // 同时支持Excel 2003、2007
                                FileInputStream is = new FileInputStream(oneFile); // 文件流
                               /* Workbook wb = null;
                                if(file.getName().endsWith("xls")){  //Excel 2003
                                    wb = new HSSFWorkbook(is);
                                }else if(file.getName().endsWith("xlsx")){  // Excel 2007/2010
                                    wb = new XSSFWorkbook(is);
                                }*/
                                Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel2003/2007/2010都是可以处理的
    
                                int sheetCount = workbook.getNumberOfSheets(); // Sheet的数量
                                /**
                                 * 设置当前excel中sheet的下标:0开始
                                 */
                                Sheet sheet = workbook.getSheetAt(0);   // 遍历第一个Sheet
    
                                // 为跳过第一行目录设置count
                                int count = 0;
    
                                for (Row row : sheet) {
                                    // 跳过第一行的目录
                                    if(count == 0){
                                        count++;
                                        continue;
                                    }
                                    // 如果当前行没有数据,跳出循环
                                    if(row.getCell(0)==null ||  row.getCell(0).toString().equals("")){
                                        continue ;
                                    }
                                    String rowValue = "";
                                    //将数据放到数据库
                                    System.out.println(row.getCell(0)+".."+row.getCell(0));
                                    //第x个位置只存放word的连接,所以将名字取出来再进行对比
                                    if(!row.getCell(2).toString().equals("")){
                                        String wordName = row.getCell(2).toString();
                                        for (File wordFile : fileList){
                                            if (wordFile.getName().equals(wordName)) {
                                                String str = "";
                                                try {
                                                    FileInputStream fis = new FileInputStream(wordFile);
                                                    XWPFDocument xdoc = new XWPFDocument(fis);
                                                    XWPFDocument doc = new XWPFDocument(is);
                                                    XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                                                    String doc1 = extractor.getText();
                                                    System.out.println(doc1);
                                                    fis.close();
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                    System.out.println();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally{
                            }
    
                        }
    
    
    
    
                    }
                }
                catch(Exception e){
                    ret.setMessage(FileUploadConstant.ERROR_FILE_CONTENT_NOATCH);
                    return ret;
                }
                finally{
                    FileCommonUtils.delete(desPath);
                }
    
            }
            ret.setMessage(FileUploadConstant.ERROR_FILE_CONTENT_NOATCH);
            return ret;
        }
    

      

  • 相关阅读:
    在一台Linux服务器上安装多个MySQL实例(二)--使用单独的MySQL配置文件
    在一台Linux服务器上安装多个MySQL实例(一)--使用mysqld_multi方式
    MySQL复制(四)--多源(主)复制
    MySQL复制(三)--基于全局事物标识符(GTID)配置复制
    类加载机制详解
    字符串常量池理解
    JVM内存模型
    Java设计模式之单例模式
    forkjoin及其性能分析,是否比for循环快?
    集合排序Comparable和Comparator有什么区别?
  • 原文地址:https://www.cnblogs.com/sg9527/p/8568905.html
Copyright © 2011-2022 走看看