POI read encrypted Excel (compatible with XLS,XLSX format)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.