码迷,mamicode.com
首页 > 其他好文 > 详细

poi读取xlsx

时间:2014-07-16 13:22:18      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:poi   数据库   测试   

知道 大家都知道用poi读取xls  当时有时候 必须要读取xlsx  现在我把我做测试的demo分享给大家


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(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.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\\桌面\\22.xlsx"));
		
	}
}

里面会涉及把内容写入到数据库的

poi读取xlsx,布布扣,bubuko.com

poi读取xlsx

标签:poi   数据库   测试   

原文地址:http://blog.csdn.net/zjy660358/article/details/37872137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!