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); } }
  • 相关阅读:
    ggplot2绘图入门系列之二:图层控制与直方图
    机器学习与数据挖掘中的十大经典算法
    mysql使用存储过程执行定时任务
    使用hbase-shaded-client解决google包冲突问题
    vue 表单校验及气泡清除
    druid配置
    如何修改maven jar包源码
    jar包冲突最新解决方式
    Hive安装
    Hbase
  • 原文地址:https://www.cnblogs.com/rdchen/p/10245084.html
Copyright © 2011-2022 走看看