给个十分简单的例子
protected void Button1_Click(object sender, EventArgs e)
{
//EXCEL路径
string path = FileUpload1.PostedFile.FileName
//把EXCEL读到DataTable 的方法
DataTable dt = ExcelToTable(path)
int count = 0
//下面就是读出所有的行,自己选择哪几项插入SQL就行了!
for (int i = 0i <dt.Rows.Counti++)
{
string jinshouren = dt.Rows[i][1].ToString()
string company = dt.Rows[i][2].ToString()
string people = dt.Rows[i][3].ToString()
string duty = dt.Rows[i][4].ToString()
string tel = dt.Rows[i][5].ToString()
string molible = dt.Rows[i][6].ToString()
string fanwei = dt.Rows[i][7].ToString()
string add = dt.Rows[i][8].ToString()
string QQ = dt.Rows[i][9].ToString()
string email = dt.Rows[i][10].ToString()
string laiyuan = dt.Rows[i][11].ToString()
string qingkuang = dt.Rows[i][12].ToString()
string test = dt.Rows[i][13].ToString()
string luxian = dt.Rows[i][14].ToString()
string jilu = dt.Rows[i][15].ToString()
string sql_select = string.Format("select count(*) from customer where Customername='{0}' and Customercompany='{1}' and Customermoblie='{2}'", people, company, tel)
string num = help.ExcuteDataSet(sql_select, CommandType.Text).Tables[0].Rows[0][0].ToString()
if (num != "1")
{
string sql = string.Format(@"insert into customer (Customername,Customersex,Customerduty,Customercompany,Customerbusiness,Customermoblie,Customerfax,Customertel,Customeraddress,CustomerQQ,CustomerEmail,roadline,Customernotes,teamwork,tracking)
values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')", people, jinshouren, duty, company, fanwei, tel, laiyuan, molible, add, QQ, email, luxian, test, qingkuang, jilu)
count = help.ExcuteNoneQuery(sql, CommandType.Text)
}
}
if (count >0)
{
ShowMsg("导入成功!")
}
}
public static DataTable ExcelToTable(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0" +
"Data Source='" + Path + "'" + "Extended Properties='Excel 8.0HDR=YesIMEX=1'"
OleDbConnection conn = new OleDbConnection(strConn)
conn.Open()
string strExcel = ""
OleDbDataAdapter myCommand = null
DataSet ds = null
strExcel = "select * from [sheet1$]"//EXCEL表名(不是文件名)
myCommand = new OleDbDataAdapter(strExcel, strConn)
ds = new DataSet()
myCommand.Fill(ds, "table1")
conn.Dispose()
return ds.Tables[0]
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)