POI parsing xlsx

Source: Internet
Author: User
Tags readfile

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.

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.