Excle导入

package db;

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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Excel {
public static DbConn DB = new DbConn();
public static void main(String[] args) throws Exception {
int num = 5;
int i = 0;
int b = 0;
int j = 0;
try {
Connection con = DB.getConnection();
String sql = "insert into b(v_area_name,v_phonenum,v_month) values(?,?,?)";
PreparedStatement pre = null;
pre = con.prepareStatement(sql);
List list = new ArrayList();
InputStream in = new FileInputStream("d:/c.xls");
HSSFWorkbook jlw = new HSSFWorkbook(in);
HSSFSheet sh = jlw.getSheetAt(1);
if (sh == null) {
in.close();
} else {
int number = sh.getPhysicalNumberOfRows();
if (number < 1) {
in.close();
} else {
HSSFRow row = null;
try{
while (i < number) {
for(;i<number;i++){
row = sh.getRow(i);
pre.setString(1, row.getCell((short)0).getStringCellValue());
pre.setString(2, String.valueOf((long)row.getCell((short)1).getNumericCellValue()));
pre.setString(3,"200909");
pre.addBatch();
j++;
if (j % num == 0) {
pre.executeBatch();
pre.clearParameters();
pre.clearBatch();
b = number - j;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
if (b < num) {
System.out.println(b);
pre.executeBatch();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java,apache,sql,Excel,J#)