通过Excel表格上传数据
private void SaveData(string state) { if (Session["CustomerDataTable"] == null) { if (fldCustomersInfo.PostedFile.FileName != "") { if ((fldCustomersInfo.PostedFile.ContentType.ToUpper() == "application/vnd.ms-excel".ToUpper()) || (fldCustomersInfo.PostedFile.ContentType.ToUpper() == "application/octet-stream".ToUpper())) { WX.Utils.WebTools.UploadFile(Page, fldCustomersInfo, ref txtCustomerFileName, ref txtCustomerFilePath); string strPath = WX.Utils.Config.GetValue("UpLoadPath"); string filePath = Server.MapPath(strPath) + @"\" + txtCustomerFilePath.Value; if (File.Exists(filePath)) { DataTable customerDt = new DataTable(); //try { //try //{ ExcelFile ef = new ExcelFile(); ef.LoadXls(filePath); if (ef.Worksheets.Count > 0) { ExcelWorksheet ew = ef.Worksheets[0]; if (ew.Rows.Count > 1) { //第一行为表头 if (ew.Rows[0].Cells[0].Value != null && ew.Rows[0].Cells[1].Value != null) { customerDt.Columns.Add(ew.Rows[0].Cells[0].Value.ToString()); customerDt.Columns.Add(ew.Rows[0].Cells[1].Value.ToString()); } else { ShowMessage("格式错误!请严格按照模板!"); return; } for (int i = 1; i < ew.Rows.Count; i++) { DataRow drXLS = customerDt.NewRow(); if (ew.Rows[i].Cells[1].Value != null) { drXLS[0] = ""; if (ew.Rows[i].Cells[0].Value != null) { drXLS[0] = ew.Rows[i].Cells[0].Value.ToString(); } drXLS[1] = ew.Rows[i].Cells[1].Value.ToString(); customerDt.Rows.Add(drXLS); } } } } //} //catch (Exception ex) //{ // SimpleLog.Write(ex.StackTrace.ToString() + ex.Message); // ShowMessage("读取文件信息失败,请确保文件格式正确!"); // return; //} } // catch (Exception ex) //{ // ShowMessage("读取客户信息失败!" + ex.Message.Replace("'", "")); // return; //} Session["CustomerDataTable"] = customerDt; Session["CustomerFileName"] = txtCustomerFileName.Value; try { File.Delete(filePath); } catch { } } else { ShowMessage("所选择的文件格式错误!正确格式为:.xls文件!" + fldCustomersInfo.PostedFile.ContentType); return; } } } } int ttlCount = 0; //SqlStr = service.GetPageSqlStr(TableName, 1, 1, string.Format("RowID={0}", id), "", "RowID DESC", out ttlCount); //DataTable dtOne = service.GetDataTable(SqlStr); object[] args = new object[] { TableName, 1, 1, string.Format("RowID={0}", id), "", "RowID DESC", 0 }; object obj = service.GetServiceMethod("GetPageSqlStr", args); string SqlStr = ""; if (obj != null) { SqlStr = obj.ToString(); if (!int.TryParse(args[6].ToString(), out ttlCount)) ttlCount = 0; } DataTable dtOne = null; object objDt = service.GetServiceMethod("GetDataTable", new object[] { SqlStr }); if (objDt != null) dtOne = (DataTable)objDt; if (dtOne == null) return; DataRow dr = null; if (dtOne != null && dtOne.Rows.Count > 0) { dr = dtOne.Rows[0]; } else { dr = dtOne.NewRow(); } if (dr != null) { BizOperator.GetEditData(dr, this); //dr["MessageContent"] = fldMessageContent.Text; //dr["MessageTitle"] = fldMessageTitle.Text; //dr["Remark"] = fldRemark.Text; //dr["SenderName"] = fldSenderName.Text; //dr["StartTime"] = fldStartTime.Text; //dr["TaskName"] = fldTaskName.Text; } dr["LastModifiedUser"] = UserName; dr["LastModifiedTime"] = WX.Util.DBExtension.GetDateTimeNowString(); dr["CustomerFileName"] = Session["CustomerFileName"]; if (fldCorpID.SelectedItem != null) { dr["CorpID"] = fldCorpID.SelectedItem.Value; dr["CorpName"] = fldCorpID.SelectedItem.Text; } if (state == "D") { if (fldState.SelectedItem != null) { dr["State"] = fldState.SelectedItem.Value; dr["StateName"] = fldState.SelectedItem.Text; } } else { dr["State"] = "S"; dr["StateName"] = "提交"; } if (fldIsRebackMsg.Checked) dr["IsRebackMsg"] = "0"; else dr["IsRebackMsg"] = "1"; try { if (id == 0) { dr["CreateUser"] = UserName; dr["CreateTime"] = WX.Util.DBExtension.GetDateTimeNowString(); dtOne.Rows.Add(dr); } //service.Update(SqlStr, dtOne); service.GetServiceMethod("Update", new object[] { SqlStr, dtOne }); } catch (Exception exc) { SimpleLog.Write("短信群发保存数据失败,原因:" + exc.Message + exc.StackTrace); ShowMessage("保存任务信息时出错,请确保所选择的文件的格式与模板相符!"); return; } try { int detailForTtlCount = 0; int parentID = 0; //string sql = SqlStr = service.GetPageSqlStr(TableName, 1, 1, "", "Top 1 RowID", "RowID DESC", out detailForTtlCount); //DataTable dtOneForDetail = service.GetDataTable(sql); object[] args2 = new object[] { TableName, 1, 1, "", "Top 1 RowID", "RowID DESC", 0 }; string sql = (string)service.GetServiceMethod("GetPageSqlStr", args2); if (!int.TryParse(args[6].ToString(), out detailForTtlCount)) detailForTtlCount = 0; DataTable dtOneForDetail = (DataTable)service.GetServiceMethod("GetDataTable", new object[] { sql }); if (dtOneForDetail != null && dtOneForDetail.Rows.Count > 0) { if (dtOneForDetail.Rows[0][0] != null) { int.TryParse(dtOneForDetail.Rows[0][0].ToString(), out parentID); } } SaveDetailInfo(parentID); } catch (Exception ex) { dtOne.Rows[0].Delete(); //service.Update(SqlStr, dtOne); service.GetServiceMethod("Update", new object[] { SqlStr, dtOne }); SimpleLog.Write("保存客户信息时出错,原因:" + ex.Message + ex.StackTrace); ShowMessage("保存客户信息时出错,原因是:" + ex.Message.Replace("'", "")); return; } if (Request.QueryString["from"] == null) { StringBuilder sp = new StringBuilder(); sp.Append("<script language='javascript'>\r\n"); sp.Append(" window.returnValue=true;\r\n"); sp.Append(" window.close();\r\n"); sp.Append("</script>"); #pragma warning disable 612,618 Page.RegisterClientScriptBlock("ok", sp.ToString()); } else Server.Transfer("Manage.aspx"); #pragma warning restore 612,618 }