Visual Studio .NET 2002Visual C++ 7.0.NET 1.0Windows 2003Windows 2000Windows XP中级开发Visual StudioWindowsC++.NETASP.NETC#
将 DataGrid 导出到 Excel






3.23/5 (81投票s)
2003年6月20日

391550

10014
将整个 DataGrid 导出到 Excel 文件的代码。
引言
本示例演示了“如何将整个 DataGrid 导出到 Excel 文件”。
如果您正在寻找一种将 DataGrid
导出到 Excel 文件的简单方法,那么您现在有了解决方案。
对整个 DataGrid
进行 HTML 编码。 代码遍历表行中的每个单元格,确保显示的所有文本都经过 HTML 编码,无论它们只是纯文本、按钮、超链接、多个控件等。
将此代码包含在您的应用程序/项目中,并在您需要的地方使用它。 此代码仅接受一个参数,即 DataGrid
对象本身。 以下是使用此代码的示例调用代码
private void ExportLinkButton_Click(object sender, System.EventArgs e)
{
string strTitle = “put the grid title here”;
new DataGridExcelExporter(this.YourGridName , this.Page).Export(strTitle);
}
其中,
strTitle
是DataGrid
的标题。DataGridExcelExporter
是主类的名称。this.YourGridName
是DataGrid
对象。this.Page
是当前的Page
对象。
您只需要传递您的 DataGrid
对象和网格的漂亮标题,其余的将由此代码处理。 毕竟,请看一下代码,它非常容易理解。
先决条件:客户端机器上应安装 Microsoft Excel 97 或更高版本,才能使此功能正常工作。