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