首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

excel数据导入到指定的表中(姓名与表字段不对应)

2013-03-01 
excel数据导入到指定的表中(名称与表字段不对应)package com.service.compareimport java.io.Fileimport

excel数据导入到指定的表中(名称与表字段不对应)
package com.service.compare;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.eventmodel.EventRecordFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.service.upfile.Link_Database;

public class Com_import {

private Connection conn = null;
PreparedStatement pstmt = null;

public void connect() {
try {
Link_Database demo = new Link_Database();
conn = demo.connect();
} catch (Exception e) {
e.printStackTrace();
}
}

public void close() {
try {
this.conn.close();
} catch (Exception e) {

}
}


public void test(String path,String xmb_bm,String charge_date){

List<Integer> list = new ArrayList<Integer> ();
try {
//OPCPackage pkg = OPCPackage.open(path);
InputStream is = new FileInputStream(path);
Workbook wb = WorkbookFactory.create(new File(path));
Sheet sheet = wb.getSheetAt(0);

for(int i = 0; i<sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
if(row != null){
if(i==0){
String str = "电话号码,客户名称,来显,月租,宽带月租,铜缆租用,维护费";
for(int j=0; j<row.getLastCellNum();j++){
Cell cell = row.getCell(j);
if(cell==null || cell.toString().trim().length()==0){
}else if(str.indexOf(cell.toString())!=-1){
list.add(j);
}
}
}else{
StringBuffer str_charge=new StringBuffer();
for(int w=0;w<list.size();w++){
Cell cell = row.getCell(list.get(w));
if(cell==null|| cell.toString().trim().length()==0){
str_charge.append("'0',");
}else{
str_charge.append("'"+cell.toString()+"',");
}
}
if(!str_charge.toString().substring(0, 7).equals("'0','0'")){
String str = str_charge.toString().substring(0, str_charge.lastIndexOf(","));
this.import_date(str,xmb_bm,charge_date);
}
}
}
}
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

public void import_date(String args, String xmb_bm,String charge_date){

try {
String sql_bdth = "insert into CAIWU_COMPARE(TELNUMBER,CUSTOMER,CID_CHARGE,JB_YZF,KD_YZF,TL_YZF,WH_CHARGE,XMB_BM,CHARGE_DATE) values ("+args+",'"+xmb_bm+"','"+charge_date+"')";
pstmt = conn.prepareStatement(sql_bdth);
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void delete(String xmb_bm,String charge_date){

try {
String sql = "delete from CAIWU_COMPARE where CHARGE_DATE='"+charge_date+"' and XMB_BM='"+xmb_bm+"'";
pstmt = conn.prepareStatement(sql);
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args){
Com_import demo = new Com_import();
demo.connect();
demo.test("D:/desktop/charge_off_data/新新新.xls","TD","2013-01");

//demo.test("D:/apache-tomcat-6.0.30/webapps/yzxt/data/2013-01/话费清单.xlsx","TD","2013-01");

demo.close();
}

}

热点排行