Exemplo n.º 1
0
        private void butExcel_Click(object sender, EventArgs e)
        {
            string acur = System.Environment.CurrentDirectory;

            try
            {
                string afile = "";
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.Filter           = "Microsoft Excel Document|.xls";
                saveFileDialog1.CheckFileExists  = false;
                saveFileDialog1.ShowDialog();
                afile = saveFileDialog1.FileName;
                if (afile != "")
                {
                    if (afile.ToLower().LastIndexOf(".xls") != afile.Length - 4)
                    {
                        afile = afile + ".xls";
                    }
                }
                if (afile != "")
                {
                    if (System.IO.File.Exists(afile))
                    {
                        if (MessageBox.Show(this, lan.Change_language_MessageText("Đã tồn tại:") + "\n" + afile + "\n" + lan.Change_language_MessageText("Đồng ý lưu?"), m_v.s_AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                        {
                            saveFileDialog1.ShowDialog();
                            return;
                        }
                    }
                    CurrencyManager cm  = (CurrencyManager)(BindingContext[dataGridView1.DataSource, dataGridView1.DataMember]);
                    DataView        dv  = (DataView)(cm.List);
                    DataSet         ads = new DataSet();
                    ads.Tables.Add(dv.Table.Copy());
                    m_v.f_export_excel(ads.Tables[0], afile);
                }
                saveFileDialog1.Dispose();
            }
            catch
            {
            }
            finally
            {
                System.Environment.CurrentDirectory = acur;
            }
        }