コード例 #1
0
        public static void ExportGridViewData(DataTable data_source, GridView grid_view, string file_name = "")
        {
            if (data_source != null && data_source.Rows.Count > 0)
            {
                SaveFileDialog sDialog = new SaveFileDialog();
                sDialog.Filter = "Microsoft Excel (*.xls)|*.xls|Microsoft Excel 2007 (*.xlsx)|*.xlsx|PDF (*.pdf)|*.pdf|Rich Text Format (*.rtf)|*.rtf|Webpage (*.html)|*.html|Rich Text File (*.rtf)|*.rtf|Text File (*.txt)|*.txt";
                sDialog.Title  = LanguageEngine.GetMessageCaption("000007", ConfigEngine.Language);
                if (!string.IsNullOrEmpty(file_name))
                {
                    sDialog.FileName = FileEngine.CreateUniqueFileName(file_name);
                }
                if (sDialog.ShowDialog() == DialogResult.OK)
                {
                    switch (sDialog.FilterIndex)
                    {
                    case 1:
                        grid_view.ExportToXls(sDialog.FileName);
                        break;

                    case 2:
                        grid_view.ExportToXlsx(sDialog.FileName);
                        break;

                    case 3:
                        grid_view.ExportToPdf(sDialog.FileName);
                        break;

                    case 4:
                        grid_view.ExportToText(sDialog.FileName);
                        break;

                    case 5:
                        grid_view.ExportToHtml(sDialog.FileName);
                        break;

                    case 6:
                        grid_view.ExportToRtf(sDialog.FileName);
                        break;

                    case 7:
                        grid_view.ExportToText(sDialog.FileName);
                        break;
                    }
                    if (XtraMessageBox.Show(LanguageEngine.GetMessageCaption("000006", ConfigEngine.Language).Replace("$FileName$", sDialog.FileName), (ConfigEngine.Language == "vi") ? "Thông Báo" : "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        Process.Start(sDialog.FileName);
                    }
                }
            }
            else
            {
                return;
            }
        }