码迷,mamicode.com
首页 > 其他好文 > 详细

把报表的数据导出Excel

时间:2016-10-06 17:20:51      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

//导出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 "";
}

把报表的数据导出Excel

标签:

原文地址:http://www.cnblogs.com/miku-lai/p/5933873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!