最近项目要做GridControl的数据输出(用了Dev控件),所以就贴出看晒晒。 把整个类贴出: /// <summary> /// GridControl導出Html、Xml、Excel、Text... /// </summary> public class GridControlExportTo { #region 获取GridView的选择行的对应的某单元的值:GetGridControlSelectValue /// <summary> /// 获取GridView的选择行的对应的某单元的值:GetGridControlSelectValue /// </summary> /// <param name="gridView"></param> /// <param name="dataColumnName"></param> /// <returns></returns> public object GetGridControlSelectValue(DevExpress.XtraGrid.Views.Grid.GridView gridView,string dataColumnName) { if (gridView.SelectedRowsCount > 0) { return gridView.GetDataRow(gridView.GetSelectedRows()[0])[dataColumnName]; } else return null; } #endregion #region 打开文件 /// <summary> /// 打开文件 /// </summary> /// <param name="fileName">文件名</param> public void OpenFile(string fileName) { if(MessageBox.Show("是否想要打开这个文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { Process process = new Process(); process.StartInfo.FileName = fileName; process.StartInfo.Verb = "Open"; process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.Start(); } catch { MessageBox.Show("您的系统不能打开该类型的文件!.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } #endregion #region 把GridControl的数据输出 /// <summary> /// 把GridControl的数据输出 /// </summary> /// <param name="provider">输出提供者</param> /// <param name="gridView1">DevExpress GridView</param> public void ExportTo(IExportProvider provider,GridView gridView1) { Cursor currentCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; BaseExportLink link = gridView1.CreateExportLink(provider); (link as GridViewExportLink).ExpandAll = false; link.ExportTo(true); Cursor.Current = currentCursor; } /// <summary> /// 把GridControl的数据输出输出成Html /// </summary> /// <param name="provider"></param> /// <param name="gridView1"></param> public void ExportToHtml(GridView gridView1) { string fileName = ShowSaveFileDialog("HTML 文檔", "HTML 文檔|*.html"); if(fileName != "") { ExportTo(new ExportHtmlProvider(fileName),gridView1); OpenFile(fileName); } } /// <summary> /// 把GridControl的数据输出输出成Xml /// </summary> /// <param name="gridView1"></param> public void ExportToXml(GridView gridView1) { string fileName = ShowSaveFileDialog("Xml 文檔", "Xml 文檔|*.xml"); if(fileName != "") { ExportTo(new ExportXmlProvider(fileName),gridView1); OpenFile(fileName); } } /// <summary> /// 把GridControl的数据输出输出成Excel /// </summary> /// <param name="gridView1"></param> public void ExportToExcel(GridView gridView1) { string fileName = ShowSaveFileDialog("Excel 文檔", "Excel 文檔|*.xls"); if(fileName != "") { ExportTo(new ExportXlsProvider(fileName),gridView1); OpenFile(fileName); } } /// <summary> /// 把GridControl的数据输出输出成Text文本 /// </summary> /// <param name="gridView1"></param> public void ExportToText(GridView gridView1) { string fileName = ShowSaveFileDialog("Text 文檔", "Text 文檔|*.txt"); if(fileName != "") { ExportTo(new ExportTxtProvider(fileName),gridView1); OpenFile(fileName); } } #endregion #region 提示保存窗口 /// <summary> /// 提示保存窗口 /// </summary> /// <param name="title"></param> /// <param name="filter"></param> /// <returns></returns> public string ShowSaveFileDialog(string title, string filter) { SaveFileDialog dlg = new SaveFileDialog(); string name = Application.ProductName; int n = name.LastIndexOf(".") + 1; if(n > 0) name = name.Substring(n, name.Length - n); dlg.Title = "Export To " + title; dlg.FileName = name; dlg.Filter = filter; if(dlg.ShowDialog() == DialogResult.OK) return dlg.FileName; return ""; } #endregion }