一、写一类:
using System;
using System.Windows.Forms;
namespace WinUI.XQSF
{
/// <summary>
/// PubUtil 用于导出Grid数据。
/// </summary>
public class PubUtil
{
public PubUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void ExportTo(DevExpress.XtraGrid.Views.Base.BaseView bv, DevExpress.XtraExport.IExportProvider provider)
{
Cursor currentCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
DevExpress.XtraGrid.Export.BaseExportLink link = bv.CreateExportLink(provider);
link.ExportTo(true);
Cursor.Current = currentCursor;
}
public static string ShowSaveFileDialog(string title, string filter)
{
SaveFileDialog dlg = new SaveFileDialog();
string name = "导出文件";
dlg.Title = "导出到 " + title;
dlg.FileName = name;
dlg.Filter = filter;
if(dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
return "";
}
}
}
二、在要调用的地方事件里编写:
private void btnExportGrid_Click(object sender, System.EventArgs e)
{
try
{
string fileName = PubUtil.ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
if(fileName != "")
{
PubUtil.ExportTo(gridView1, new DevExpress.XtraExport.ExportXlsProvider(fileName));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
using System;
using System.Windows.Forms;
namespace WinUI.XQSF
{
/// <summary>
/// PubUtil 用于导出Grid数据。
/// </summary>
public class PubUtil
{
public PubUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void ExportTo(DevExpress.XtraGrid.Views.Base.BaseView bv, DevExpress.XtraExport.IExportProvider provider)
{
Cursor currentCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
DevExpress.XtraGrid.Export.BaseExportLink link = bv.CreateExportLink(provider);
link.ExportTo(true);
Cursor.Current = currentCursor;
}
public static string ShowSaveFileDialog(string title, string filter)
{
SaveFileDialog dlg = new SaveFileDialog();
string name = "导出文件";
dlg.Title = "导出到 " + title;
dlg.FileName = name;
dlg.Filter = filter;
if(dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
return "";
}
}
}
二、在要调用的地方事件里编写:
private void btnExportGrid_Click(object sender, System.EventArgs e)
{
try
{
string fileName = PubUtil.ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
if(fileName != "")
{
PubUtil.ExportTo(gridView1, new DevExpress.XtraExport.ExportXlsProvider(fileName));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
本文介绍了一个名为PubUtil的类,该类包含用于从DevExpress Grid视图导出数据的方法。通过使用提供的静态方法ExportTo,可以将数据导出为Excel格式,并通过ShowSaveFileDialog方法指定保存位置。
258

被折叠的 条评论
为什么被折叠?



