Java reads Excel through JXL

Source: Internet
Author: User

 PackageCom.hd.all.test.testjava;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook;Importjxl.read.biff.BiffException;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;Importjxl.write.WriteException; Public classREADEXCELJXL { Public Static voidMain (string[] args)throwsbiffexception, IOException {//Exception in thread ' main ' jxl.read.biff.BiffException:Unable to recognize OLE stream//1. Cannot read 2007 version of Excel;2.java generated and cannot read//File File = new file ("E:/students2017-10-19_10_55_55.xls");        /*** Read Excel content*/File File=NewFile ("c:\\users\\administrator\\desktop\\ New Folder \\1.xls"); Workbook WB=workbook.getworkbook (file); Sheet s= Wb.getsheet ("Sheet1"); /*** Write Excel content*/Writableworkbook Book= Workbook.createworkbook (NewFile ("c:\\users\\administrator\\desktop\\ New Folder \\2.xls")); Writablesheet sheet= Book.createsheet ("Sheet1", 0); String Reg= "1[0-9][a-z]{2,3}[0-9]{5,6}";//Match 15kf111111Pattern p =Pattern.compile (REG); String REG1= "100[0-9]{5}";//Match 10011111Pattern P1 =Pattern.compile (REG1); String REG2= "2[0-9]{11}-{0,1}[0-9]{0,1}";//Match 211111111111 (-N)-->211111111111-2Pattern P2 =Pattern.compile (REG2); Cell C=NULL; inti = 1;  while(true) {C= S.getcell (0,i);//row I second column, 0 is the first columnMatcher m = P.matcher (C.getcontents ());//Match 15kf111111Matcher m1 = P1.matcher (C.getcontents ());//Match 10011111Matcher m2 = p2.matcher (c.getcontents ());//Match 211111111111            Try {                if(M.find ()) {Sheet.addcell (NewLabel (2,i,m.group (0)));//If it is the Ding Jie material number output}Else{                    if(M2.find ()) {Sheet.addcell (NewLabel (2,i,m2.group (0))); }Else{                        if(M1.find ()) {Sheet.addcell (NewLabel (3,i,m1.group (0)));//if the output is in 10011111 format                        }                    }                }            } Catch(Exception e) {} I++; if(I > 753)//number of lines in Excel minus 1                 Break;        } book.write (); Try{book.close (); } Catch(WriteException e) {e.printstacktrace (); }    }}

Java reads Excel through JXL

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.