首页 新闻 搜索 专区 学院

C#DevExpress导出数据控件中GridView表格中的数据到excel

0
[已解决问题] 解决于 2016-07-13 11:09

C#DevExpress导出数据控件GridView表格中的数据到excel

问题补充:

 #region 导出员工信息
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable datasource = _EmployeesServices.get_V_EmployeeDataTable("");
            if (datasource == null )
            {
                XtraMessageBox.Show("无法导出数据,请检查");
            }
            Export(datasource);
          
        }
        public void Export(DataTable datasource)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.DefaultExt = "xls";
            saveFileDialog.Filter = "Excel文件|*.xls";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                GridControl grid = new GridControl(); ;
                GridView view = new GridView();
                grid.ViewCollection.Add(view);
                grid.MainView = view;
                view.GridControl = grid;

                foreach (DataColumn dc in datasource.Columns)
                {
                    GridColumn gridColumnNumber = view.Columns.AddVisible(dc.ColumnName);
                    gridColumnNumber.FieldName = dc.ColumnName;
                }


                grid.DataSource = datasource;
                this.Controls.Add(grid);//重要  
                grid.ForceInitialize();//重要  

                view.BestFitColumns();
                view.ExportToXls(saveFileDialog.FileName);
                XtraMessageBox.Show("导出成功");
            }
        }  
        #endregion

塔姆爱吃泡泡糖的主页 塔姆爱吃泡泡糖 | 菜鸟二级 | 园豆:210
提问于:2016-07-12 15:56
< >
分享
最佳答案
-1

_EmployeesServices.get_V_EmployeeDataTable("");

get_V_EmployeeDataTable() 这个方法查询的是视图。如果查表的话 导出来的数据列头是英文

塔姆爱吃泡泡糖 | 菜鸟二级 |园豆:210 | 2016-07-13 11:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册