日期:2014-05-17 浏览次数:20819 次
package org.cupd.spdb.report.importexcel.dao.impl;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 org.cupd.spdb.report.importexcel.dao.CardBinDao;
import com.websurf.spdb.bo.DBConnBOImpl;
public class CardBinDaoImpl implements CardBinDao {
private static final Log log=LogFactory.getLog(CardBinDaoImpl.class);
private static DBConnBOImpl dbConn = new DBConnBOImpl();
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
private String filePath="E:\\Microsoft Excel.xlsx";
/**
* 插入数据
*/
public boolean insertDB(){
boolean flag=true;
try {
//文件流指向excel文件
// FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook=new XSSFWorkbook(filePath);//创建工作薄
XSSFSheet sheet=workbook.getSheetAt(0);//得到工作表
XSSFRow row=null;//对应excel的行
XSSFCell cell=null;//对应excel的列
int totalRow=sheet.getLastRowNum();//得到excel的总记录条数
log.info("得到excel的总记录数"+totalRow);
//以下的字段一一对应数据库表的字段
String cardName="";
String cardType="";
String cardBin="";
String cardLength="";
String activityCode="";
String note="";
String sql ="insert into BizCardBin(cardName,cardType,cardBin,cardLength,activityCode,note) values(?,?,?,?,?,?)";
for(int i=2;i<=totalRow;i++){
row=sheet.getRow(i);
cell=row.getCell((short) 0);
cardName=cell.getStringCellValue().toString();
cell=row.getCell((short) 1);
cardType=cell.getStringCellValue().toString();
cell=row.getCell((short) 2);
cardBin=cell.getStringCellValue().toString();