protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//Response.Write(Server.MapPath(@"/LogisticRpt/"));
//Response.End();
FileUpload1.SaveAs(Server.MapPath("/LogisticRpt/Accounting/") + FileUpload1.FileName);
Label1.Text = "上传成功!";
string strsuf = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".")).ToString().Trim().ToLower();
if (strsuf != ".xls")
{
Label1.Text = strsuf;
return;
}
else
{
string filepath = Server.MapPath("/LogisticRpt/Accounting/" + FileUpload1.FileName);
// string filepath = "/LogisticRpt/Accounting/" + FileUpload1.FileName;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
Response.Write(filepath);
string strSheetName = "sheet1";
string strExcel = "select 单据号 as id,发票号码 as num from [sheet1$]";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strExcel, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "data");
conn.Close();
gridview1.DataSource = ds;
gridview1.DataBind();
string constratpacc = "data source=.;database=ATP_ACC;uid=sa";
string strsqlupdata;
SqlConnection conn2 = new SqlConnection(constratpacc);
conn2.Open();
SqlCommand cmd2;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strsqlupdata = "update ATP_ACC.DBO.DNOTE_CNOTE set TAXINV_NUM='" + ds.Tables[0].Rows[i]["num"].ToString() + "'where ID= '" + ds.Tables[0].Rows[i]["id"].ToString()+"'";
try
{
cmd2 = new SqlCommand(strsqlupdata, conn2);
cmd2.ExecuteNonQuery();
Label1.Text = "数据导入成功";
}
catch
{
Label1.Text = "数据导入失败";
}
}
conn2.Close();
}
}