Recent projects need to read encrypted Excel, there are two types of XLS and xlsx, for the encryption and decryption Apache official website has a brief description
Required jar Package Poi-3.9,jar Poi-ooxml-3.9.jar Poi-ooxml-schemas-3.9.jar Xmlbeans-2.3.0.jar Dom4j-1.6.1.jar
Note that the 3 Jar version number of the POI should correspond, or there will be some problems.
On the Code ~
/** * Excel Import */@SuppressWarnings ("Unchecked") @RequestMapping (value = {"Importexcel"}) @ResponseBod Y public Result Importexcel (@RequestParam (value = "ym") String ym, @RequestParam (value = "Filedata") multipartfile file) { int count=0; result = NULL; try {if (file! = null) {String password = "5151"; String Filename=file.getoriginalfilename (); String prefix = filename.substring (Filename.lastindexof (".") + 1); String yyyymm=ym.replace ("-", ""); Workbook Workbook; Decrypt Excel if (Prefix.touppercase (). Equals ("XLS")) {Org.apache.poi.hssf.record.crypto.Biff8EncryptionKey . Setcurrentuserpassword (password); Workbook = Workbookfactory.create (File.getinputstream ()); File.getinputstream (). Close (); } else {//Poifsfilesystem PFS = Newpoifsfilesystem (Poidatasamples.getpoifsinstance (). Openresourceasstream ("* *. Xlsx ")); PoifsfilEsystem PFS = new Poifsfilesystem (File.getinputstream ()); File.getinputstream (). Close (); Encryptioninfo encinfo = new Encryptioninfo (PFS); Decryptor decryptor = decryptor.getinstance (Encinfo); Decryptor.verifypassword (password); workbook = new Xssfworkbook (Decryptor.getdatastream (PFS))//Excel with 2007+ not supported//Xssfworkbook workbook = new XSSF Workbook (Decryptor.getdatastream (PFS));//support for 2007+ Excel}//Read Excel data Sheet Sheet = WORKBOOK.G Etsheetat (0); <span style= "font-family:arial, Helvetica, Sans-serif;" >//does not support 2007+ excel</span>//xssfsheet sheet = workbook.getsheetat (0); Excel that supports 2007+
POI read encrypted Excel (compatible with XLS,XLSX format)