private void ClientFileToServerTemp() { string s_AP_FileName; ErrorMsgLabel.Text = ""; if (FileUpload1.FileName != "") { try { //設定頁面狀態 this.ErrorMsgLabel.Text = ""; System.Threading.Thread.Sleep(2000); string s_UploadPath = string.Empty; string s_BackPath = string.Empty; ArrayList arl_FileUpload_Return = null; ArrayList arl_FileToTmp_Return = null; System.Text.StringBuilder sb = new System.Text.StringBuilder(); CAAModel.CAA32_BCO BCO = new CAAModel.CAA32_BCO(ConntionDB); //上傳檔案到AP端 //取得要上傳的路徑 s_UploadPath = Server.MapPath("..\\") + System.Configuration.ConfigurationManager.AppSettings["UploadPath"] + "\\CAA\\"; string strOrg_FileName = FileUpload1.FileName;//原始的檔名 arl_FileUpload_Return = BCO.FileUpload(s_UploadPath, this.FileUpload1, Session["UID"].ToString()); if (arl_FileUpload_Return[0].ToString() == "FALSE")//檔案上傳至AP端錯誤 { sb.Append("檔案上傳至AP端錯誤<br/>"); this.ErrorMsgLabel.Text = sb.ToString(); //清空 UploadStatusPanel 的值 //上傳結果 this.UploadStatusPanel1.UploadSum = 0; this.UploadStatusPanel1.UploadOK = 0; this.UploadStatusPanel1.UploadNG = 0; //匯入結果 this.UploadStatusPanel1.ImportSum = 0; this.UploadStatusPanel1.ImportOK = 0; this.UploadStatusPanel1.ImportNG = 0; return; } else if (arl_FileUpload_Return[0].ToString() == "TRUE")//檔案上傳至AP端正常 { // 讀取 Excel 資料 OleDbConnection oleConnection = null; DataSet ds_Excel = new DataSet(); try { String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + arl_FileUpload_Return[1].ToString() + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; oleConnection = new OleDbConnection(connString); oleConnection.Open(); OleDbCommand oleCommand = new OleDbCommand("SELECT * FROM [Sheet1$] where 序號 is not null", oleConnection); OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand); oleAdapter.Fill(ds_Excel); //請注意如果EXCEL檔欄位有值,但到DB卻沒有資料 //請修改,機碼 HKEY_LOCAL_MACHINE\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows 值 //預設是 8, 表示會先讀取前 8 列來決定每一個欄位的型態, 所以如果前 8 列的資料都是數字, 到了第 9 列以後出現的文字資料都會變成 null //如果要解決這個問題, 只要把 TypeGuessRows 機碼值改成 0 } catch (Exception ex) { this.ErrorMsgLabel.Text = ex.Message; } finally { if (oleConnection != null) { oleConnection.Close(); oleConnection.Dispose(); } } //將檔案匯入TmpTable DateTime d_CreateDate = Convert.ToDateTime(arl_FileUpload_Return[2]); s_AP_FileName = string.Empty; s_AP_FileName = arl_FileUpload_Return[3].ToString(); arl_FileToTmp_Return = BCO.FileToTmp(ds_Excel, d_CreateDate, Session["UID"].ToString(), ConntionDB, strOrg_FileName); if (arl_FileToTmp_Return[0].ToString() == "FALSE") { ErrorMsgLabel.Text = arl_FileToTmp_Return[1].ToString(); this.btn_Temp_To_DB.Enabled = false; } else if (arl_FileToTmp_Return[0].ToString() == "TRUE") { // 將資料從 UploadPath 移至 BackPath s_BackPath = Server.MapPath("..\\") + System.Configuration.ConfigurationManager.AppSettings["BackPath"] + "\\CAA\\"; // 檢查檔案路徑是否存在,如不存在新增相關路徑 if (Directory.Exists(s_BackPath) == false) { try { Directory.CreateDirectory(s_BackPath); } catch (Exception except) { throw new System.Exception(String.Format("不能建立文件目錄{0},錯誤訊息為{1}", s_BackPath, except.Message)); } } s_UploadPath += s_AP_FileName; s_BackPath += s_AP_FileName; System.IO.File.Copy(s_UploadPath, s_BackPath); System.IO.File.Delete(s_UploadPath); this.btn_Temp_To_DB.Enabled = false; this.btn_Unusual_Report.Enabled = false; // 將結果顯示在 UploadStatusPanel 中 //上傳結果 this.UploadStatusPanel1.UploadSum = ((int)arl_FileToTmp_Return[1]); this.UploadStatusPanel1.UploadOK = ((int)arl_FileToTmp_Return[2]); this.UploadStatusPanel1.UploadNG = ((int)arl_FileToTmp_Return[3]); if ((int)arl_FileToTmp_Return[1] == 0) { ErrorMsgLabel.Text = "[匯入]失敗,請檢查匯入檔案是否為空檔!"; this.btn_Temp_To_DB.Enabled = false; this.btn_Unusual_Report.Enabled = false; } else if ((int)arl_FileToTmp_Return[1] == (int)arl_FileToTmp_Return[2]) { ErrorMsgLabel.Text = "[匯入]成功,請繼續[執行匯入]的功能。"; this.btn_Temp_To_DB.Enabled = true; this.btn_Unusual_Report.Enabled = false; } else if ((int)arl_FileToTmp_Return[1] != (int)arl_FileToTmp_Return[2]) { ErrorMsgLabel.Text = "[匯入]失敗,請檢視異常!"; this.btn_Temp_To_DB.Enabled = false; this.btn_Unusual_Report.Enabled = true; } } } } catch (Exception ex) { this.ErrorMsgLabel.Text = ex.Message; } } }