Know that everyone knows to read XLS with a POI at the time, I had to read xlsx. Now I'm sharing my test 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;
Import 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 (?)";
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 (sh
Eetindex);
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.executebatch ();
}}} ps.executebatch ();
}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 involves writing content to the database.