标签:
//导出Excel
private void button3_Click(object sender, EventArgs e)
{
StringBuilder SB = new StringBuilder();
DataRow dr;
ExportToExcel("固定资产");
}
string _FileName;
public void ExportToExcel(string pfileName)
{
_FileName = pfileName == null ? "未命名" : pfileName.Trim();
string fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
if (fileName != "")
{
try
{
ExportTo(new DevExpress.XtraExport.ExportXlsProvider(fileName));
}
catch (System.Exception err)
{
MessageBox.Show(err.Message);
}
}
}
/// <summary>
/// 导出到
/// </summary>
/// <param name="provider">供应者</param>
private void ExportTo(DevExpress.XtraExport.IExportProvider provider)
{
Cursor currentCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
DevExpress.XtraGrid.Export.BaseExportLink link = gridView1.CreateExportLink(provider);
(link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
link.ExportCellsAsDisplayText = true;
link.ExportTo(true);
Cursor.Current = currentCursor;
}
/// <summary>
/// 显示保存文件对话框,并返回选择的文件路径
/// </summary>
/// <param name="title">对话框的标题</param>
/// <param name="filter">过滤器</param>
/// <returns>选择的文件路径</returns>
private string ShowSaveFileDialog(string title, string filter)
{
SaveFileDialog dlg = new SaveFileDialog();
string name = _FileName;
int n = name.LastIndexOf(".") + 1;
if (n > 0) name = name.Substring(n, name.Length - n);
dlg.Title = "导出到" + title;
dlg.FileName = name;
dlg.Filter = filter;
if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
return "";
}
标签:
原文地址:http://www.cnblogs.com/miku-lai/p/5933873.html