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); } }
  • 相关阅读:
    idea中如何返回上一个鼠标的焦点
    git-修改本地和远端分支名字
    git修改已经commit的注释记录
    《完全用Linux工作》
    C++ 网络爬虫实现
    计算两个YUV420P像素数据的PSNR---高等算法
    C++类对象大小的计算
    mp4文件格式解析
    BMP图片格式模型(2)
    BMP图片格式模型
  • 原文地址:https://www.cnblogs.com/rdchen/p/10245084.html
Copyright © 2011-2022 走看看