程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#導出csv、excel格式文件

c#導出csv、excel格式文件

編輯:C#入門知識

This class library is fully implemented in C#.NET. This dll doesn't need to be registered. Simply copy the assembly"RKLib.ExportData.dll" into your project folder and add it to references. You can also include the "ExportData" project and reference it in your project.

This can be used in WebForms as well as in WinForms by simply passing a parameter to the export object's constructor.

For WebForms:
 Collapse | Copy Code
RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Web")For WinForms:
 Collapse | Copy Code
RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Win")The ExportDetails method has three types of overloads. You can call whichever best suits your requirement.

The following are the overload types:

 Collapse | Copy Code
public void ExportDetails(DataTable DetailsTable,
  ExportFormat FormatType, string FileName) Collapse | Copy Code
public void ExportDetails(DataTable DetailsTable, int[]
  ColumnList, ExportFormat FormatType, string FileName) Collapse | Copy Code
public void ExportDetails(DataTable DetailsTable, int[]
  ColumnList, string[] Headers, ExportFormat FormatType, string FileName)Have a glance at the parameters
DetailsTable - DataTable to be exported.
FormatType - Export File Format. Use Export.ExportFormat.CSV for the CSV file orExport.ExportFormat.Excel for the Excel file.
FileName - Export File Name.
- For WebForms simply pass the filename. e.g. EmployeeInfo.xls
- For WinForms pass the filename along with path. e.g. C:\\EmployeeInfo.csv
ColumnList - DataFields of the DataTable to be exported. Specify their ordinals in an integer array.
Headersrs - Custom Headers List for the specified columns in the export file. Specify the names in a string array.
You can call this method in the: ASPButton_Click event, LinkButton_Click event or Page_Load event or wherever you want. All you need to do is set the values to parameters and give a simple call to the required overload type of ExportDetails method.

The following is the code block that demonstrates "exporting specified columns" of a DataTable as an Excel file from WebForm.

 Collapse | Copy Code
[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;
    }
}The following is the code block that demonstrates "exporting specified columns" of a DataTable as a CSV file from WindowsForm.

 Collapse | Copy Code
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;
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved