WinForm------如何将GridControl数据导出到Excel

时间:2021-06-15 07:11:57

转载:

http://www.cnblogs.com/xiaofengfeng/archive/2011/11/22/2258906.html

Gridcontrol中的BandedGridView导出多层行头

转载:

http://www.cnblogs.com/huldy520/p/5611736.html 

代码:

protected virtual void ExportToExcel(Object grid)
{
  //EditorContainer定义GridControl之类的容器
  SaveFileDialog dialog = new SaveFileDialog
  {
    Title = "导出Excel",
    Filter = "Excel文件(*.xls)|*.xls"
  };
  if (dialog.ShowDialog(this) == DialogResult.OK)
  {
    //XlsExportOptions
    XlsExportOptionsEx options = new XlsExportOptionsEx();
    if (grid is GridControl)
    {
      (grid as GridControl).ExportToXls(dialog.FileName, options);
    }
    else if (grid is BandedGridView)
    {
      options.ExportType = DevExpress.Export.ExportType.WYSIWYG;
      (grid as BandedGridView).OptionsPrint.AutoWidth = false;
      (grid as BandedGridView).OptionsPrint.PrintBandHeader = true;
      (grid as BandedGridView).ExportToXls(dialog.FileName, options);
    }
    else if (grid is TreeList)
    {
      (grid as TreeList).ExpandAll();
      (grid as TreeList).ExportToXls(dialog.FileName, options);
    }
    XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }
}