前台代码:
<td class="formLabel"> 批量修改: </td> <td class="formInput"> <asp:FileUpload ID="FileUpload1" runat="server"/> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="上傳" CssClass="button3"/> </td>
后台代码
protected void Button3_Click(object sender, EventArgs e) { if (FileUpload1.HasFile == false) { WebUtils.Alert(this, "請選擇Excel文件!", "StorMaintain.aspx"); return; } string IsXLS = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower(); if (IsXLS != ".xls") { WebUtils.Alert(this, "只能選擇Excel文件!", "StorMaintain.aspx"); return;//当选择的不是Excel文件时,返回 } SqlConnection conn = new SqlConnection("server=10.56.10.21;database=MTLBPM;uid=sa;pwd=@seshMTLK2"); conn.Open(); //string strpath = FileUpload1.PostedFile.FileName.ToString(); //string filename = FileUpload1.FileName; //存取的文件路径 string strpath = this.Server.MapPath("..\..\Upload\") + FileUpload1.FileName; string filename = FileUpload1.FileName; FileUpload1.PostedFile.SaveAs(strpath); FileUpload1.Dispose(); DataSet ds = ExcelDs(strpath, filename); DataRow[] dr = ds.Tables[0].Select(); int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { WebUtils.Alert(this, "Excel表为空!", "StorMaintain.aspx"); //当Excel表为空时,对用户进行提示并跳转到相应页面 } else { for (int i = 0; i < dr.Length; i++) { //Excel表所对应的栏位,只能是英文,中文栏位报错(不知道为什么?) string tFacPartNum, tPrice, tPN3, tCustomer; tFacPartNum = dr[i]["FacPartNum"].ToString().Trim(); tPrice = dr[i]["price"].ToString().Trim(); tPN3 = dr[i]["3PN"].ToString().Trim(); tCustomer = dr[i]["Customer"].ToString().Trim(); string sqlcheck = "select count(0) num from dbo.SD_StorAmount where FacPartNum='" + tFacPartNum + "'"; bool ch = check(sqlcheck); //判断厂内料号是否存在 if (ch) { //为false则插入数据 string insertStr1 = "insert into dbo.SD_StorAmount(FacPartNum,Price,PN3,Customer) values('" + tFacPartNum + "','" + tPrice + "','" + tPN3 + "','" + tCustomer + "')"; SqlCommand cmd = new SqlCommand(insertStr1, conn); cmd.ExecuteNonQuery(); } else { string updateStr = "update dbo.SD_StorAmount set Price='" + tPrice + "' where FacPartNum='" + tFacPartNum + "'"; SqlCommand cmd = new SqlCommand(updateStr, conn); cmd.ExecuteNonQuery(); } } try { WebUtils.Alert(this, "保存成功!", "StorMaintain.aspx"); } catch (Exception ee) { WebUtils.Alert(this, ee.Message); } } conn.Close(); } //导入功能 public DataSet ExcelDs(string filenameurl, string table) { // 此语句为Excel2003适用 string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn); DataSet ds = new DataSet(); odda.Fill(ds, table); return ds; } //Check 方法判断是否存在记录 public bool check(string str) { // using (SqlConnection conn = new SqlConnection("server=10.56.10.21;database=MTLBPM;uid=sa;pwd=@seshMTLK2")) { using (SqlCommand cmd = new SqlCommand(str, conn)) { conn.Open(); //通过ExecuteScalar()方法返回count值 int n = (int)cmd.ExecuteScalar(); return n > 0 ? false : true; } } }
在服务器上要确保已安装了Excel,并且具有读写目标文件夹的权限。