Function: Add a method in the Java background, read the imported Excel content, and return the appropriate SQL statement as needed to complete the insert operation on the temporary table.
To read an Excel file using JXL
PackageCom.sixthf.bi.sapp.util;Importjava.io.IOException;ImportJava.io.InputStream;ImportJXL. Sheet;ImportJXL. Workbook;Importjxl.read.biff.BiffException;ImportOrg.apache.struts.upload.FormFile;ImportCom.got.util.uid.UUIDGener; Public classphoneutil{/*** Read the contents of the XLS file * *@paramfile object to read, IMPORTFILESN; * @returnSQL statements*/ Public StaticString readphoneexcel (formfile formfile,string importfilesn)throwsIOException, biffexception {inputstream stream=Formfile.getinputstream (); Workbook RWB=Workbook.getworkbook (stream); Sheet[] Sheets=rwb.getsheets (); StringBuffer Sbbuffer=NewStringBuffer (); for(inti=0;i<sheets.length;i++) {Sheet rs=Rwb.getsheet (i); String period= Rs.getcell (0, 1). getcontents (). Replace ("year", "-"). Replace ("month", "" ");//get the second row of the current worksheet The first column is the billing cycle for(intJ=1;j<rs.getrows (); j + +) {String First= Rs.getcell (0, J). getcontents (); String Phone= Rs.getcell (1, j). getcontents ();//gets the second column of the current row of the current worksheet, which is the access number if(!first.contains ("Total")) { if(!phone.equals ("") &&!phone.equals ("Subtotal:") {sbbuffer. Append (Insert into Cmmu_paperanswer_tmp (Subimportfilesn,importfilesn,answer1,answer2, Answer5) VALUES ("). Append ("‘"). Append (Uuidgener.getuuid (). ReplaceAll ("-", ""). Append ("‘,‘"). Append (IMPORTFILESN). Append ("‘,‘"). Append (Period). Append ("‘,‘"). Append (Phone). Append ("‘,‘"); for(intK=j+1;k<rs.getrows (); k++) {Phone= Rs.getcell (1, K). getcontents (); if(Phone.contains ("Subtotal") ) {String costnum= Rs.getcell (3, k). getcontents ();//gets the fourth column of the current worksheet, which is the charge.sbbuffer. Append (costnu m). Append ("‘);"). Append ("\ n"); J=K; Break; } Else Continue; } } Else Continue; } Else Break; }} String String=sbbuffer.tostring (); returnstring; } }
Use POI to read Excel
PackageCom.sixthf.bi.sapp.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjxl.read.biff.BiffException;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.struts.upload.FormFile;ImportCom.got.util.uid.UUIDGener; Public classphoneutil{/*** Read the contents of the XLS file * *@paramfile object to read, IMPORTFILESN; * @returnSQL statements*/ Public StaticString readphoneexcel (formfile formfile,string importfilesn)throwsIOException, biffexception {inputstream stream=Formfile.getinputstream (); Hssfworkbook RWB=NewHssfworkbook (stream); intNumsheet =rwb.getnumberofsheets (); StringBuffer Sbbuffer=NewStringBuffer (); for(inti=0;i<numsheet;i++) {Hssfsheet rs=Rwb.getsheetat (i); String period= Rs.getrow (1). Getcell ( Short) 0). toString (). Replace ("year", "-"). Replace ("month", "" "); for(intJ=1;j<rs.getphysicalnumberofrows (); j + +) {String First= Rs.getrow (j). Getcell ( Short) 0). toString (); String Phone= Rs.getrow (j). Getcell ( Short) 1). ToString ();//gets the second column of the current row of the current worksheet, which is the access number if(!first.contains ("Total")) { if(!phone.equals ("") &&!phone.equals ("Subtotal:") {sbbuffer. Append (Insert into Cmmu_paperanswer_tmp (Subimportfilesn,importfilesn,answer1,answer2, Answer5) VALUES ("). Append ("‘"). Append (Uuidgener.getuuid (). ReplaceAll ("-", ""). Append ("‘,‘"). Append (IMPORTFILESN). Append ("‘,‘"). Append (Period). Append ("‘,‘"). Append (Phone). Append ("‘,‘"); for(intK=j+1;k<rs.getphysicalnumberofrows (); k++) {Phone= Rs.getrow (k). Getcell ( Short) 1). toString (); if(Phone.contains ("Subtotal") ) {String costnum= Rs.getrow (k). Getcell ( Short) 3). ToString ();//gets the fourth column of the current worksheet, which is the charge.sbbuffer. Append (costnu m). Append ("‘);"). Append ("\ n"); J=K; System.out.println (k); Break; } Else Continue; } } Else Continue; } Else Break; }} String String=sbbuffer.tostring (); returnstring; } }
Jxl.jar package Download Link: Http://pan.baidu.com/s/1i3k6ccP
Poi.jar package Download Link: http://pan.baidu.com/s/1jGrVBCi
To read an Excel file using Jxl,poi