Exemple #1
0
        private void ExportData()
        {
            try
            {
                btnExport.Enabled = false;
                string             pathFileSave = "";
                XtraSaveFileDialog fileSave     = new XtraSaveFileDialog();
                fileSave.Filter = "Excel files (.xlsx)|*.xlsx";
                fileSave.AutoUpdateFilterDescription = true;
                if (fileSave.ShowDialog() == DialogResult.OK)
                {
                    if (!fileSave.FileName.Contains(".xlsx"))
                    {
                        XtraMessageBox.Show("File save phải định dạng .xlsx", "Thông Báo");
                    }
                    else
                    {
                        //System.IO.File.Copy(PathFile, fileSave.FileName, true);
                        pathFileSave = fileSave.FileName;
                    }
                }
                else
                {
                    return;
                }

                //string sLink = Form_Main.URL_API + "/api/IPC247/sp_extension_GetDataByStore?sql_Exec=" + "sp_Get_Product_Export";
                //var json = API.API_GET(sLink);

                //var jsondata = JObject.Parse(json).GetValue("Data");
                //DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsondata.ToString(), (typeof(DataTable)));
                DataTable dt     = SQLHelper.ExecuteDataTable("sp_Get_Product_Export");
                string    Folder = AppDomain.CurrentDomain.BaseDirectory + "/AppData";
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/AppData"))
                {
                    Directory.CreateDirectory(Folder);
                }

                Folder = AppDomain.CurrentDomain.BaseDirectory + string.Format("/AppData/{0}/", DateTime.Now.ToString("yyyyMMdd"));
                if (!Directory.Exists(Folder))
                {
                    Directory.CreateDirectory(Folder);
                }

                #region Xóa File cũ
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/AppData/");
                foreach (System.IO.DirectoryInfo di in dir.GetDirectories())
                {
                    if (di.Name != DateTime.Today.ToString("yyyyMMdd"))
                    {
                        System.IO.Directory.Delete(di.FullName, true);
                    }
                }
                #endregion Xóa File cũ

                string PathFile = Folder + string.Format("/ExportData_{0}.xlsx", DateTime.Now.ToString("ddMMyyyyHHmmss"));
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFile(new Uri(Form_Main.URL_API + "/Uploads/Tmp/template/template.xlsx"), PathFile);
                }

                System.IO.FileInfo fnew = new System.IO.FileInfo(PathFile);
                using (ExcelPackage pck = new ExcelPackage(fnew))
                {
                    ExcelWorksheet ws = pck.Workbook.Worksheets["Sheet1"];
                    if (ws == null)
                    {
                        ws = pck.Workbook.Worksheets.Add("Sheet1");
                    }
                    ws.Cells["A2"].LoadFromDataTable(dt, false);
                    pck.Save();
                }
                System.IO.File.Copy(PathFile, fileSave.FileName, true);

                if (File.Exists(PathFile))
                {
                    File.Delete(PathFile);
                }
                try
                {
                    Process.Start(fileSave.FileName);
                }
                catch
                {
                    XtraMessageBox.Show("Bạn cần cài đặt Excel để mở file!", "Thông Báo");
                }
                btnExport.Enabled = true;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                API.API_ERRORLOG(new ERRORLOG(Form_Main.IPAddress, "Form_Product", "ExportData()", ex.ToString()));
                btnExport.Enabled = true;
                return;
            }
        }