zoukankan      html  css  js  c++  java
  • 导入报版本不匹配问题

    -----------------------------关于excel-----------------------------

    xls与xlsx的区别

    xls是excel2003及以前版本生成的文件格式。
    xlsx是excel2007及以后版本生成的文件格式(excel 2007之后版本可以打开xls格式的文件)。

    HSSF类,只支持2007以前的excel(文件扩展名为xls),而XSSH支持07以后的




    @RequestMapping({"importExcel"}) @Action(description="导入Excel") public void importExcel(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception { MultipartFile fileLoad = request.getFile("xmlFile"); ResultMessage resultMessage = null; String fileType = fileLoad.getOriginalFilename().substring( fileLoad.getOriginalFilename().lastIndexOf(".") + 1, fileLoad.getOriginalFilename().length()); Workbook wb = null; if (fileType.equals("xls")) { try { //07+版本 wb = new HSSFWorkbook(fileLoad.getInputStream()); } catch (Exception e) { //03版 wb = new XSSFWorkbook(fileLoad.getInputStream()); } } else if (fileType.equals("xlsx")) { wb = new XSSFWorkbook(fileLoad.getInputStream()); } else { throw new Exception("读取的不是excel文件"); } }

    使用如下代码,就不用区分新旧版,一行代码就生成了Workbook

    Workbook wb = WorkbookFactory.create(fileLoad.getInputStream());
    -----------------------------关于word----------------------------- @RequestMapping(
    "importWord") public void importWord(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception { MultipartFile fileLoad = request.getFile("wordFile"); String fileType = fileLoad.getOriginalFilename(); InputStream inputStream = fileLoad.getInputStream(); if(StringUtil.endsWith(fileType.toLowerCase(), ".doc")){ POIFSFileSystem pfs = new POIFSFileSystem(inputStream); HWPFDocument hwpf = new HWPFDocument(pfs); }else if(StringUtil.endsWith(fileType.toLowerCase(), ".docx")){ XWPFDocument xwpf = new XWPFDocument(inputStream); POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf); } }
  • 相关阅读:
    【转】C++多继承的细节
    【转】CVE-2010-4258 漏洞分析
    【转】cve-2013-2094 perf_event_open 漏洞分析
    android CVE 漏洞汇总
    ExecutorService中submit和execute的区别
    线程池之ThreadPoolExecutor使用
    postman接口自动化,环境变量的用法详解(附postman常用的方法)转
    件测试专家分享III GUI自动化测试相关
    Linux上运行Jmeter
    时间复杂度和空间复杂度计算
  • 原文地址:https://www.cnblogs.com/rdchen/p/10245084.html
Copyright © 2011-2022 走看看