注:注意Excel中的格式,带格式的excel可能导入出错。
陷阱如:如某列值为:“1-1”,在excel中如果不设置为“文本格式”,极有可能会默认为:2017-1-1,注意调整格式。
aspx:
cs中:
protected void Export_Click(object sender, EventArgs e)
{
string fileName = fileId.FileName;
string savePath = Server.MapPath("~/file/");
FileOperatpr(fileName, savePath); //创建文件夹。
fileId.SaveAs(savePath + fileName); //保存(前台的FileUpload控件)
DataOperator(fileName, savePath); //开始传输数据
}
private void DataOperator(string fileName, string savePath)
{
//这里的Microsoft.ACE.OLEDB.12.0还有一种,这是由于EXCEL版本不同而定义的,具体可以搜索下。
string myString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + savePath + fileName + ";Extended Properties=Excel 8.0";
OleDbConnection oconn = new OleDbConnection(myString);
oconn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", oconn);
oda.Fill(ds);
oconn.Close();
DataSetOperator(ds, savePath + fileName); //DataSet的数据,然后封装进去。
}
private void DataSetOperator(DataSet ds, string filePath) //数据正式进行操作,并导入数据库
{
SqlConnection conn = new SqlConnection("Data Source=XXXXX;Database=XXXXXXXX;Uid=sa;Pwd=XXXXXXXXX;"); //数据库连接。
conn.Open();
SqlTransaction str = conn.BeginTransaction();//利用事务处理 防止中断
int k = 0;
if (ds.Tables[0].Rows.Count < 1)
{
Response.Write("");
return;
}
try
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string sqlStr = "insert into tb_bookinfo(bcsasfase,bsdofokqcfodde,tsbdh,bzIdSBN,boodkdname,srdm,authodr,pubndame,pdricde,csdm,bcdyc,sjakfb,pagfae,fjqgsk,nrjfsj,boffaqokddtype,type,imgUrl,yfdh,nd,cbzq,bcqh,zg,zb,bookmsg,bz,oper,inTime)values"; //我这里要上传的EXCEL有27列,都跟下边匹配上,然后进行填充就可以。少的可以自己定义
sqlStr += "('" + ds.Tables[0].Rows[i][0].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][1].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][2].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][3].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][4].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][5].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][6].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][7].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][8].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][9].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][10].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][11].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][12].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][13].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][14].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][15].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][17].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][18].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][19].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][20].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][21].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][22].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][23].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][24].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][25].ToString() + "',";
sqlStr += "'" + ds.Tables[0].Rows[i][26].ToString() + "',";
sqlStr += "'XXXX',";
sqlStr += "'2017-12-31')";
string d = sqlStr;
SqlCommand cmd = new SqlCommand(sqlStr, conn, str);
cmd.Transaction = str;
k += cmd.ExecuteNonQuery();
}
str.Commit();
}
catch (Exception ex)
{
Response.Write("发生异常,数据已回滚/n信息/n" + ex.Message);
str.Rollback();
}
finally
{
Response.Write("上传成功" + k + "条");
File.Delete(filePath);
}
}