public W30222(string programID, string programName) : base(programID, programName) { InitializeComponent(); this.Text = _ProgramID + "─" + _ProgramName; GridHelper.SetCommonGrid(gvMain); dao30222 = new D30222(); dao30203 = new D30203(); dao30221 = new D30221(); }
protected override ResultStatus Export() { string showMsg = ""; try { lblProcessing.Text = "開始轉檔..."; lblProcessing.Visible = true; dao30221 = new D30221(); //判斷是否有檔案,決定是否要寫入DB. showMsg = "讀取既有計算資料錯誤"; string cpYmd = txtDate.DateTimeValue.ToString("yyyyMMdd"); DataTable dtPLS1 = dao30221.d_30221_pls1(cpYmd); if (dtPLS1.Rows.Count > 0) { DialogResult result = MessageDisplay.Choose("已有計算資料,是否要更新資料庫資料?"); if (result == DialogResult.No) { cbxDB.Checked = false; } } string rptId, rptName = "個股部位限制計算表", file, stkYmd = txtStkoutYmd.DateTimeValue.ToString("yyyyMMdd"), sMonth = txtSMonth.Text.Replace("/", ""), eMonth = txtEMonth.Text.Replace("/", ""); rptId = "30221"; //讀取資料 showMsg = "讀取資料錯誤"; DataTable dt30221 = dao30221.d_30221(cpYmd, sMonth, eMonth, stkYmd); if (dt30221.Rows.Count == 0) { MessageDisplay.Info(eMonth + "," + rptId + '-' + rptName + ",無任何資料!"); lblProcessing.Visible = false; return(ResultStatus.Fail); } //複製檔案 showMsg = "複製檔案錯誤"; file = PbFunc.wf_copy_file(rptId, rptId); if (file == "") { return(ResultStatus.Fail); } //開啟檔案 showMsg = "開啟檔案錯誤"; Workbook workbook = new Workbook(); workbook.LoadDocument(file); //切換Sheet showMsg = "切換Sheet錯誤"; Worksheet ws30221 = workbook.Worksheets[0]; //寫入資料 showMsg = "寫入資料錯誤"; int rowNum = 2, rowTol; #region wf_30221 lblProcessing.Text = rptId + "-" + rptName + " 轉檔中..."; rowTol = 500; if (cbxDB.Checked) { ws30221.Cells[0, 0].Value = ws30221.Cells[0, 0].Value.AsString() + "(試算)"; } //(一) ws30221.Cells[2, 4].Value = txtSMonth.Text + "~" + txtEMonth.Text; //(二) ws30221.Cells[2, 5].Value = txtStkoutYmd.Text; rowNum = 4; //從A5開始填資料 ws30221.Import(dt30221, false, rowNum, 0); //存檔 ws30221.ScrollToRow(0); workbook.SaveDocument(file); lblProcessing.Text = "轉檔成功"; #endregion if (!cbxDB.Checked) { return(ResultStatus.Success); } #region wf_30221_write bool dbCommit = false; //刪除PL0的資料 showMsg = "PL0刪除失敗"; dbCommit = dao30221.DeletePL0ByDate(cpYmd); if (!dbCommit) { MessageDisplay.Error(showMsg); return(ResultStatus.Fail); } //新增PL0的資料 string etfFileName; showMsg = "PL0新增失敗"; dbCommit = dao30221.InsertPL0(cpYmd, sMonth, eMonth, stkYmd, GlobalInfo.USER_ID); if (!dbCommit) { MessageDisplay.Error(showMsg); return(ResultStatus.Fail); } //刪除PLS2的資料 showMsg = "PLS2刪除失敗"; dbCommit = dao30221.DeletePLS2ByDate(cpYmd); if (!dbCommit) { MessageDisplay.Error(showMsg); return(ResultStatus.Fail); } //刪除PLS1資料 dtPLS1已在前面讀取過 if (dtPLS1.Rows.Count > 0) { while (dtPLS1.Rows.Count > 0) { dtPLS1.Rows.RemoveAt(dtPLS1.Rows.Count - 1); } showMsg = "PLS1刪除失敗"; dbCommit = dao30221.DeletePLS1ByDate(cpYmd); } if (!dbCommit) { MessageDisplay.Error("刪除資料庫中舊資料錯誤! "); return(ResultStatus.Fail); } if (cbxWriteTxt.Checked) { //把刪除結果存成txt etfFileName = Path.Combine(GlobalInfo.DEFAULT_REPORT_DIRECTORY_PATH, "30221_AfterDel.txt"); ExportOptions txtref = new ExportOptions(); txtref.HasHeader = false; txtref.Encoding = System.Text.Encoding.GetEncoding(950);//ASCII Common.Helper.ExportHelper.ToText(dtPLS1, etfFileName, txtref); } dtPLS1.Clear(); dtPLS1.AcceptChanges(); //寫入PLS1資料 //dt30221 資料重撈因為欄位比報表多 dt30221 = dao30221.d_30221_all(cpYmd, sMonth, eMonth, stkYmd); for (int f = 0; f < dt30221.Rows.Count; f++) { dtPLS1.Rows.Add(); for (int g = 0; g < 16; g++) { dtPLS1.Rows[dtPLS1.Rows.Count - 1][g] = dt30221.Rows[f][g]; } dtPLS1.Rows[dtPLS1.Rows.Count - 1][16] = DateTime.Now; dtPLS1.Rows[dtPLS1.Rows.Count - 1][17] = GlobalInfo.USER_ID; } try { ResultData myResultData = dao30221.updatePLS1(dtPLS1); } catch (Exception ex) { MessageDisplay.Error("計算結果新增至資料庫錯誤! "); WriteLog(ex); return(ResultStatus.Fail); } if (cbxWriteTxt.Checked) { //把寫入結果存成txt etfFileName = Path.Combine(GlobalInfo.DEFAULT_REPORT_DIRECTORY_PATH, "30221.txt"); ExportOptions txtref = new ExportOptions(); txtref.HasHeader = false; txtref.Encoding = System.Text.Encoding.GetEncoding(950);//ASCII Common.Helper.ExportHelper.ToText(dtPLS1, etfFileName, txtref); } #endregion } catch (Exception ex) { MessageDisplay.Error(showMsg); throw ex; } return(ResultStatus.Success); }