市长热线演示版

callplanintophone.aspx.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Data.SqlClient;
  11. using HySoft.DBUtility;
  12. using System.Text;
  13. namespace HySoft.BaseCallCenter.Web.calloutmanage
  14. {
  15. public partial class callplanintophone : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (!IsPostBack)
  20. {
  21. if (!string.IsNullOrEmpty(Request.QueryString["taskid"]))
  22. {
  23. txtId.Value = Request.QueryString["taskid"].Trim();
  24. }
  25. }
  26. }
  27. protected void btnSubmit_Click(object sender, EventArgs e)
  28. {
  29. string res = "";
  30. int i = 0;
  31. int count = 0;
  32. System.Text.StringBuilder strBuilder = null;
  33. string strSelName = "";//计划选择的客户资料项
  34. if (this.txtFilePath.Value != "")
  35. {
  36. if (txtId.Value != "")
  37. {
  38. DataTable dtPlan = new BLL.T_Call_PageField().GetList(" TaskId=" + txtId.Value).Tables[0];
  39. if (dtPlan != null && dtPlan.Rows.Count > 0)
  40. {
  41. foreach (DataRow dr in dtPlan.Rows)
  42. {
  43. strSelName += "," + dr["F_DBFieldlName"];
  44. }
  45. }
  46. if (strSelName != "")
  47. {
  48. strSelName = strSelName.Substring(1);
  49. }
  50. if (strSelName != "")
  51. {
  52. #region 将文件中的数据导入数据库
  53. try
  54. {
  55. //************************将文件中的数据导入数据库*************************//
  56. DataSet dt = ReadEcxel(this.txtFilePath.Value, 0);
  57. try
  58. {
  59. if (dt != null && dt.Tables[0].Rows.Count != 0)
  60. {
  61. strBuilder = new System.Text.StringBuilder();//用于如果导入失败,删除已经导入过的
  62. DataTable dtInputData = dt.Tables[0];//要导入的电话号码
  63. List<string> lstsql = new List<string>();
  64. int ilns = dt.Tables[0].Columns.Count;
  65. #region MyRegion
  66. using (SqlConnection coon = new SqlConnection(DbHelperSQL.connectionString))
  67. {
  68. try
  69. {
  70. coon.Open();
  71. #region MyRegion
  72. if (dtInputData.Rows.Count > 0)
  73. {
  74. string sql = "";
  75. int mmcount = 0;
  76. string strValue = "";//选择exce列的值
  77. string[] ArryClm = strSelName.Split(',');
  78. SqlCommand cmd = new SqlCommand();
  79. foreach (DataRow dr in dtInputData.Rows)
  80. {
  81. sql = "";
  82. strValue = "";
  83. #region MyRegion
  84. //是否存在此电话号码;
  85. sql = "select count(1) from T_Call_TaskTelNum where TelNmb='" + dr[Request.Form["slTelNmb"].ToString()].ToString() + "'";
  86. cmd = new SqlCommand(sql, coon);
  87. try
  88. {
  89. mmcount = int.Parse(cmd.ExecuteScalar().ToString());
  90. }
  91. catch { }
  92. lstsql.Clear();
  93. strValue = "";
  94. string ClientName = "";
  95. if (mmcount == 0)
  96. {
  97. //获取选择对应的列信息
  98. foreach (string str in ArryClm)
  99. {
  100. if (Request.Form["sl" + str] != null && Request.Form["sl" + str].ToString() != "空" && Request.Form["sl" + str].ToString() != "")
  101. {
  102. strValue += ",'" + dr[Request.Form["sl" + str].ToString()] + "'"; //
  103. if (str == "ClientName")
  104. {
  105. ClientName = dr[Request.Form["sl" + str].ToString()].ToString();
  106. }
  107. }
  108. else
  109. {
  110. strValue += ",''"; //没有选择为空
  111. }
  112. }
  113. if (strValue != "")
  114. {
  115. strValue = strValue.Substring(1);//sql value后边的值
  116. lstsql.Add("INSERT INTO T_Call_TaskTelNum (" + strSelName + ",FPBZ) values(" + strValue + ",1)");//电话表
  117. //lstsql.Add("INSERT INTO T_CTI_CallHistory ([TelNum],[State],[y_TaskId],[y_TelNmbTableName],[y_TelNmbId],[AddTime],y_CallSource) values('" + dr[Request.Form["slTelNmb"].ToString()].ToString() + "',0," + this.txtId.Value + ",'T_Call_TaskTelNum',@@IDENTITY,getdate()," + callSource + ")");
  118. }
  119. else
  120. {
  121. i++;
  122. res += i.ToString() + "没有选择对应的导入列;\n";
  123. }
  124. try
  125. {
  126. if (DBUtility.DbHelperSQL.ExecuteSqlTran(lstsql, coon) > 0)
  127. {
  128. count += 1;//导入成功,+1
  129. }
  130. }
  131. catch (Exception ex)
  132. {//导入失败
  133. strBuilder.Append(dr[Request.Form["slTelNmb"].ToString()].ToString().Trim() + "、");
  134. }
  135. }
  136. else
  137. {
  138. sql = "select top 1 F_Id from T_Call_TaskTelNum where TelNmb='" + dr[Request.Form["slTelNmb"].ToString()].ToString() + "'";
  139. string telId = DbHelperSQL.GetSingle(sql).ToString();
  140. //获取选择对应的列信息
  141. foreach (string str in ArryClm)
  142. {
  143. if (Request.Form["sl" + str] != null && Request.Form["sl" + str].ToString() != "空" && Request.Form["sl" + str].ToString() != "")
  144. {
  145. strValue += "," + str + "='" + dr[Request.Form["sl" + str].ToString()] + "'"; //
  146. if (str == "ClientName")
  147. {
  148. ClientName = dr[Request.Form["sl" + str].ToString()].ToString();
  149. }
  150. }
  151. else
  152. {
  153. // strValue += "," + str + "=''"; //没有选择为空
  154. }
  155. }
  156. if (strValue != "")
  157. {
  158. lstsql.Add(" update T_Call_TaskTelNum set FPBZ=1" + strValue + " where Id=" + telId);//电话表
  159. //lstsql.Add("INSERT INTO " + strTbJGName + "([TelNmb],[Pid],[bdbz],[AddDate],[TbName],[TbId]) values('" + dr[Request.Form["slTelNmb"].ToString()].ToString() + "'," + this.hdPlanid.Value + ",0,getdate(),'" + strTbName + "'," + telId + ")");
  160. //lstsql.Add("INSERT INTO T_CTI_CallHistory ([TelNum],[State],[y_TaskId],[y_TelNmbTableName],[y_TelNmbId],y_CallSource) values('" + dr[Request.Form["slTelNmb"].ToString()].ToString() + "',0," + this.txtId.Value + ",'T_Call_TaskTelNum'," + telId + "," + callSource + ")");
  161. }
  162. else
  163. {
  164. i++;
  165. res += i.ToString() + "没有选择对应的导入列;\n";
  166. }
  167. try
  168. {
  169. if (DBUtility.DbHelperSQL.ExecuteSqlTran(lstsql, coon) > 0)
  170. {
  171. count += 1;//导入成功,+1
  172. }
  173. }
  174. catch (Exception ex)
  175. {//导入失败
  176. strBuilder.Append(dr[Request.Form["slTelNmb"].ToString()].ToString().Trim() + "、");
  177. }
  178. }
  179. #endregion
  180. }
  181. }
  182. else
  183. {
  184. res += "该文档的内容为空";
  185. }
  186. #endregion
  187. }
  188. catch
  189. {
  190. if (coon.State == ConnectionState.Open)
  191. {
  192. coon.Close();
  193. }
  194. i++;
  195. res += i.ToString() + ".导入出现异常,共导入" + count + "条数据,请从第" + (count + 2).ToString() + "行开始导入;\n";
  196. if (strBuilder.Length > 0)
  197. {
  198. res += "失败的号码为:" + strBuilder.ToString() + "\n";
  199. }
  200. }
  201. finally
  202. {
  203. if (coon.State == ConnectionState.Open)
  204. {
  205. coon.Close();
  206. }
  207. }
  208. }//****************using*********
  209. #endregion
  210. }
  211. else
  212. {
  213. i++;
  214. res += i.ToString() + ".Excel中无数据;\n";
  215. }
  216. }
  217. catch
  218. {
  219. i++;
  220. res += i.ToString() + ".导入出现异常,导入失败;\n";
  221. File.Delete(this.txtFilePath.Value); //导入不成功时删除上传的excel文件
  222. }
  223. finally
  224. {
  225. dt.Clear();
  226. dt.Dispose();
  227. }
  228. KillProcess("Excel");//杀死已经打开的Excel进程
  229. if (File.Exists(this.txtFilePath.Value))
  230. {
  231. File.Delete(this.txtFilePath.Value);
  232. }
  233. }
  234. catch
  235. {
  236. i++;
  237. res += i.ToString() + ".导入出现异常,导入失败;\n";
  238. if (File.Exists(this.txtFilePath.Value))
  239. {
  240. File.Delete(this.txtFilePath.Value);
  241. }
  242. }
  243. #endregion
  244. }
  245. }
  246. }
  247. try
  248. {
  249. DBUtility.DbHelperSQL.ExecuteSql("update T_CTI_Task set y_HMCount=(y_HMCount+" + count + ") where TaskID=" + txtId.Value);
  250. }
  251. catch { }
  252. ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success','导入成功,共导入" + count + "条;" + res + "')</script>");
  253. }
  254. #region 杀死Excel进程
  255. /// <summary>
  256. /// 杀死Excel进程
  257. /// </summary>
  258. /// <param name="processName"></param>
  259. private void KillProcess(string processName)
  260. {
  261. System.Diagnostics.Process myproc = new System.Diagnostics.Process();
  262. //得到所有打开的进程
  263. try
  264. {
  265. foreach (Process thisproc in Process.GetProcessesByName(processName))
  266. {
  267. if (!thisproc.CloseMainWindow())
  268. {
  269. thisproc.Kill();
  270. }
  271. }
  272. }
  273. catch (Exception Exc)
  274. {
  275. throw new Exception("", Exc);
  276. }
  277. }
  278. #endregion
  279. #region 读取Excel文件,返一个DataTable
  280. /// 功能:读取Excel文件,返一个DataTable
  281. /// 日期:2004-10-20
  282. /// </summary>
  283. /// <param name="FileName">文件</param>
  284. /// <param name="Rows">第一行是否为列名,一般为0或1</param>
  285. /// <returns></returns>
  286. private DataSet ReadEcxel(string FileName, int Rows)
  287. {
  288. string _filePath = FileName;
  289. string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + "";
  290. //HDR=NO; 读取数据表头(第一行);HDR=yes 不读取表头
  291. excelconnstring += @";Extended Properties=""Excel 8.0;HDR=yes;IMEX=1""";
  292. System.Data.OleDb.OleDbConnection excelconn = new System.Data.OleDb.OleDbConnection(excelconnstring);
  293. string sql = "select * from [sheet1$]";
  294. System.Data.OleDb.OleDbDataAdapter mycomm = new System.Data.OleDb.OleDbDataAdapter(sql, excelconn);
  295. DataSet myds = new DataSet();
  296. mycomm.Fill(myds, "DataListTable");
  297. return myds;
  298. }
  299. #endregion
  300. }
  301. }