-----------------------------关于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); } }