DataSet Export CSV Format (ASP.NET, C #)

zhaozj2021-02-16  47

DataSet Export CSV Format (ASP.NET, C #)

Author: Not

MSN: NOTKING@hotmail.com

Email: nots@sohu.com

This article refers to the following Microsoft .NET framework class library namespace: system.data; system.web.ui.webControls;

SUMMARY This article resolves the DataSet to the CSV format problem, export data in DataGrid to the CSV format

Export DataSet All Columns to CSV Format Traversing Dataset Tabletes Traversing Table Rows Traversing Rows Columns

Code ///

/// Export DataSet into CSV format /// /// dataset /// CSV string data < / returns> public static string exportCSV (DataSet DS) {String Data = ""; // Data = DS.DataSetName "/ N";

Foreach (DataBle TB in ds.tables) {data = tb.tablename "/ n";

// Write a list of 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 Issues Under the case where we use DataGrid to display the DataSet, only the columns we need are displayed, and the DataSet is not displayed, so it may only need to export the columns in the DataGrid when exporting into a CSV format.

This article works: Mark the columns you need to export in the DataSet, only the tagged columns are only exported.

Code ///

/// Mark DataColumn is an acceptable /// /// Datacolumn public static void setExport (Datacolumn Column) {IF ( ! column = null) {if (column.ExtendedProperties [ "IsExport"] == null) column.ExtendedProperties.Add ( "IsExport", "true"); else column.ExtendedProperties [ "IsExport"] = "true";} }

///

/// Tags some of the lists in DataTable as acceptable /// /// dataable /// Column public static void setExport (DataTable TB, Params String [] Column (String Column In Column) {setExport (tb.columns [color ";}} /// / / / Mark some of the listed /// /// dataable /// dataGrid in DataTable / 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 [colorertext]);}}

///

/// determines whether Datacolumn is an acceptable /// /// Datacolumn /// BOOL type results < / 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. Vendor 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 ();

转载请注明原文地址:https://www.9cbs.com/read-25021.html

New Post(0)