65.9K
CodeProject 正在变化。 阅读更多。
Home

用于将数据导出到 CSV/Excel 文件的 C# 类库

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.62/5 (137投票s)

2004年9月30日

4分钟阅读

viewsIcon

1916341

downloadIcon

53307

免费源代码和教程,用于将 Web/Win Forms 中的数据导出到 CSV/Excel 文件

Screenshot - ExportCode_ScreenShot.jpg

引言

将数据从 DataTable 导出到 Excel 或 CSV 是 ASP.NET 页面中最常见的功能之一。用户可以下载 DataGrid 中的数据到 Excel 电子表格或 CSV 文件进行离线验证和/或计算。本文包含此功能的源代码。

从 DataGrid 导出数据时常见的几个问题是:

  1. 清除格式并仅导出数据。
  2. Excel 中的标题行总是包含数据库列名,这对普通用户来说有时难以理解。
  3. 数据集通常包含 DataGrid 生成所需的数据,例如内部引用 ID,但我们不希望最终用户看到这些。

为了解决所有这些问题,我提出了一种更简单、更易于适应的方法,可以将 DataGrid 本身导出到 Excel 或 CSV 文件。

我将这个概念保留在一个 DLL 中,以便它可以在应用程序中轻松使用。

使用 ExportData 类库

这个类库完全用 C#.NET 实现。此 DLL 不需要注册。只需将程序集 "RKLib.ExportData.dll" 复制到您的项目文件夹中,然后将其添加到引用中。您也可以包含 "ExportData" 项目并将其引用到您的项目中。

此功能可用于 WebFormsWinForms,只需向导出对象的构造函数传递一个参数即可。

  • 对于 WebForms
    RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Web")
  • 对于 WinForms
    RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Win")

ExportDetails 方法有三种重载。您可以根据您的需求调用最适合的方法。

以下是重载类型:

  • public void ExportDetails(DataTable DetailsTable, 
      ExportFormat FormatType, string FileName)
  • public void ExportDetails(DataTable DetailsTable, int[] 
      ColumnList, ExportFormat FormatType, string FileName)
  • public void ExportDetails(DataTable DetailsTable, int[] 
      ColumnList, string[] Headers, ExportFormat FormatType, string FileName)

请注意参数:

  • DetailsTable - 要导出的 DataTable。
  • FormatType - 导出文件格式。使用 Export.ExportFormat.CSV 用于 CSV 文件,或 Export.ExportFormat.Excel 用于 Excel 文件。
  • FileName - 导出文件名。
    - 对于 WebForms,只需传递文件名。例如:EmployeeInfo.xls
    - 对于 WinForms,传递文件名以及路径。例如:C:\\EmployeeInfo.csv
  • ColumnList - 要导出的 DataTable 的数据字段。在整数数组中指定它们的序号。
  • Headersrs - 导出文件中指定列的自定义标题列表。在字符串数组中指定名称。

您可以在 ASPButton_Click 事件、LinkButton_Click 事件或 Page_Load 事件中调用此方法,或者在您想要的任何地方调用。您需要做的就是为参数设置值,并简单地调用 ExportDetails 方法的所需重载类型。

以下代码块演示了如何从 WebForm 中将 DataTable 的“指定列”导出为 Excel 文件。

[Code Behind]

private void btnExport2_Click(object sender, System.EventArgs e)
{
    // Export the details of specified columns

    try
    {
        // Get the datatable to export

        DataTable dtEmployee = ((DataSet) 
             Session["dsEmployee"]).Tables["Employee"].Copy();

        // Specify the column list to export

        int[] iColumns = {1,2,3,5,6};

        // Export the details of specified columns to Excel

        RKLib.ExportData.Export objExport = new 
            RKLib.ExportData.Export("Web");
        objExport.ExportDetails(dtEmployee, 
             iColumns, Export.ExportFormat.Excel, "EmployeesInfo2.xls");
    }
    catch(Exception Ex)
    {
        lblError.Text = Ex.Message;
    }
}

以下代码块演示了如何从 WindowsForm 中将 DataTable 的“指定列”导出为 CSV 文件。

private void btnExportCSV_Click(object sender, System.EventArgs e)
{
    // Export the details of specified columns

    try
    {
        lblMessage.Text = "";
        
        // Get the datatable to export

        DataTable dtEmployee = dsEmployee.Tables["Employee"].Copy();

        // Specify the column list to export

        int[] iColumns = {1,2,3,5,6};

        // Export the details of specified columns to CSV

        RKLib.ExportData.Export objExport = new 
            RKLib.ExportData.Export("Win");
        objExport.ExportDetails(dtEmployee, 
            iColumns, Export.ExportFormat.CSV, 
            "C:\\EmployeesInfo.csv");
        lblMessage.Text = "Successfully exported to 
            C:\\EmployeesInfo.csv";
    }
    catch(Exception Ex)
    {
        lblMessage.Text = Ex.Message;
    }
}

使用演示项目

使用 C#.NET 的 WebForms 项目

  1. 将 ExportDemo_CSharpNet.zip 文件解压到指定目录。
  2. 在 IIS 中创建一个名为 "ExportDemo_CSharpNet" 的虚拟目录。
  3. 在 Visual Studio 2003 中打开演示解决方案 ExportDemo_CSharpNet.sln

使用 VB.NET 的 WebForms 项目

  1. 将 ExportDemo_VBNet.zip 文件解压到指定目录。
  2. 在 IIS 中创建一个名为 "ExportDemo_VBNet" 的虚拟目录。
  3. 在 Visual Studio 2003 中打开演示解决方案 ExportDemo_VBNet.sln

使用 C#.NET 的 WindowsForms 项目

  1. 将 ExportDemo_WindowsForms.zip 文件解压到指定目录。
  2. 在 Visual Studio 2003 中打开演示解决方案 ExportDataGrid.sln

关注点

  • 这不需要任何像页面注册标签这样的东西。
  • 这可以同时用于 WebForms 和 WinForms。
  • 它使用数据集的 XML 功能和 XSLT 来实现导出功能。
  • 没有遍历数据元素的循环。
  • 由于导出功能只需要一次简单地调用 Export 对象,因此可以根据需要使用导出对象任意次数。
  • 您可以在单个页面上导出任意数量的 DataGrid。
  • 您可以指定要导出的列列表。
  • 您甚至可以自定义导出文件的标题。

历史

  • 发布于 2004 年 9 月 29 日。
  • 更新于 2004 年 10 月 11 日。更新的文件解决了从 WinForms 导出数据的功能。
  • 更新于 2007 年 4 月 10 日。更新的文件解决了列名中存在特殊字符的问题。
© . All rights reserved.