关于DEV 控件pagedGridControl和gridControl导出Excel文件及。
一、导出
公用部分
public class XtraGridExportHelper
{
private static SaveFileDialog _dlg = new SaveFileDialog();
public static string ShowSaveFileDialog(string title, string filter, string fileName)
{
_dlg.Title = "导出到" + title;
_dlg.FileName = fileName;
_dlg.Filter = filter;
if (_dlg.ShowDialog() == DialogResult.OK)
return _dlg.FileName;
return "";
}
public static void OpenFile(string fileName)
{
if (XtraMessageBox.Show("导出完毕,要打开文件吗?", "导出", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = fileName;
process.StartInfo.Verb = "Open";
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.Start();
}
catch (Exception ex)
{
XtraMessageBox.Show("打开文件失败。" + ex.Message,
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
pagedGridControl 导出
string fileName = XtraGridExportHelper.ShowSaveFileDialog("Microsoft Excel", "Microsoft Excel|*.xls", this.Text);
if (fileName != String.Empty)
{
pagedGridControl1.GridView.OptionsPrint.AutoWidth = false;
pagedGridControl1.GridView.ExportToXls(fileName);
XtraGridExportHelper.OpenFile(fileName);
}
gridControl导出
string fileName = XtraGridExportHelper.ShowSaveFileDialog("Microsoft Excel", "Microsoft Excel|*.xls", this.Text);
if (fileName != String.Empty)
{
gridControl.OptionsPrint.AutoWidth = false;
gridControl.ExportToXls(fileName);
XtraGridExportHelper.OpenFile(fileName);
}