Dataset export CSV format (ASP. NET, C #)

Source: Internet
Author: User
Tags sql server query
There are already several complete SQL Server query results set export to excel Technology Article :

Http://www.cnblogs.com/meyer/archive/2004/10/18/6977.html

Http://dev.csdn.net/develop/article/18/18623.shtm

Below is the CSV format for exportCode:

Dataset export CSV format (ASP. NET, C #)

This article references the following Microsoft. NET Framework class library namespace:
System. Data;
System. Web. UI. webcontrols;

Summary
This article solves the problem of exporting dataset to CSV format and exporting data in the DataGrid to CSV format.

Export all columns in dataset to CSV format
Traverse dataset tables
Traverse the rows of a table
Traverse columns of rows

Code
/// <Summary>
/// Export dataset to CSV format
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Returns> CSV string data </returns>
Public static string exportcsv (Dataset DS)
{
String data = "";
// Data = Ds. datasetname + "\ n ";

Foreach (datatable TB in DS. Tables)
{
Data + = Tb. tablename + "\ n ";

// Write the column name
Foreach (datacolumn column in TB. columns)
{
Data + = column. columnname + ",";
}
Data + = "\ n ";

// Write data
Foreach (datarow row in TB. Rows)
{
Foreach (datacolumn column in TB. columns)
{
Data + = row [column]. tostring () + ",";
}
Data + = "\ n ";
}
Data + = "\ n ";
}

Return data;
}

Dataset and DataGrid Problems
Generally, when we use a DataGrid to display dataset, only the required columns are displayed, and not all columns of dataset are displayed. Therefore, when exporting data to CSV format, we only need to export columns in the DataGrid.

Solution:
Mark the columns to be exported in dataset. During export, only the marked columns are exported.

Code
/// <Summary>
/// Mark datacolumn as exported
/// </Summary>
/// <Param name = "column"> datacolumn </param>
Public static void setexport (datacolumn column)
{
If (column! = NULL)
{
If (column. extendedproperties ["isexport"] = NULL)
Column. extendedproperties. Add ("isexport", "true ";
Else
Column. extendedproperties ["isexport"] = "true ";
}
}

/// <Summary>
/// Mark some columns in the datatable to Be Exported
/// </Summary>
/// <Param name = "TB"> datatable </param>
/// <Param name = "columns"> column </param>
Public static void setexport (datatable TB, Params string [] columns)
{
Foreach (string column in columns)
{
Setexport (Tb. Columns [column]);
}
}

/// <Summary>
/// Mark some columns in the datatable to Be Exported
/// </Summary>
/// <Param name = "TB"> datatable </param>
/// <Param name = "columns"> column of the DataGrid </param>
Public static void setexport (datatable TB, datagridcolumncollection columns)
{
Foreach (datagridcolumn column in columns)
{
If (column. getType (). name = "boundcolumn" | column. getType (). name = "hyperlinkcolumn" | column. getType (). name = "templatecolumn ")
Setexport (Tb. Columns [column. headertext]);
}
}

/// <Summary>
/// Determine whether datacolumn is exported
/// </Summary>
/// <Param name = "column"> datacolumn </param>
/// <Returns> bool result </returns>
Public static bool isexport (datacolumn column)
{
If (column. extendedproperties ["isexport"]! = NULL & Column. extendedproperties ["isexport"]. tostring (). Trim (). tolower () = "true ")
Return true;
Else
Return false;
}

Example:
Export. setexport (Ds. Supplier List View, dg. columns );

String data = export. exportcsv (DS );

String temp = string. Format ("attachment; filename = {0}", "exportdata.csv ";
Response. clearheaders ();
Response. appendheader ("content-disposition", temp );
Response. Write (data );
Response. End ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.