As you know, when I use poi to read xls, sometimes I have to read XLSX. Now I will share my testing demo with you.
Package COM. lt. main; import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import Java. SQL. connection; import Java. SQL. preparedstatement; import Java. util. arraylist; import Java. util. list; import Org. apache. poi. hssf. usermodel. hssfcell; import Org. apache. poi. hssf. usermodel. hssfrow; import Org. apache. poi. hssf. usermodel. hssfsheet; import Org. apache. poi. hssf. usermodel. hssfworkbook; impor T Org. apache. poi. xssf. usermodel. xssfcell; import Org. apache. poi. xssf. usermodel. xssfrow; import Org. apache. poi. xssf. usermodel. xssfsheet; import Org. apache. poi. xssf. usermodel. xssfworkbook; import COM. lt. util. dbutil; import COM. lt. util. oracle; public class readmain {public static void readfile (File file) {connection con = NULL; preparedstatement PS = NULL; string SQL = "insert into t_xls (mobile) values (?) "; Int COUNT = 0; try {con = dbutil. getconnection (); PS = con. preparestatement (SQL); inputstream is = new fileinputstream (File); xssfworkbook WB = new xssfworkbook (is); xssfcell cell = NULL; For (INT sheetindex = 0; sheetindex <WB. getnumberofsheets (); sheetindex ++) {xssfsheet ST = WB. getsheetat (sheetindex); For (INT rowindex = 0; rowindex <= ST. getlastrownum (); rowindex ++) {xssfrow ROW = ST. getrow (rowindex); cell = row. getcell (0); cell. setcelltype (xssfcell. cell_type_string); count ++; PS. setstring (1, cell. getstringcellvalue (); PS. addbatch (); If (count % 10000 = 0) {ps.exe cutebatch () ;}} ps.exe cutebatch ();} catch (exception e) {e. printstacktrace ();} finally {dbutil. close (PS); dbutil. close (CON) ;}} public static void main (string [] ARGs) {dbutil. startproxool (); readfile (new file ("C: \ Documents and Settings \ Administrator \ Desktop \ 22.xlsx "));}}
It will involve writing content to the database