Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Convert DataTable to CSV file

Shulou Source: shulou.com Published: 2022-06-01 06:09:14 10月04日 Update

DataTable is used in. Net projects to cache data, and DataTable represents a table of data in memory. CSV file was first used in a simple database. Because of its simple format and strong openness, it was initially used as a mark for one's own atlas by scanners. The CSV file is a plain text file, with each line representing many attributes of a picture.

Using C # to convert DataTable into CSV files in .net projects now provides a more general method, as follows:

/ / convert DataTable to CSV file / DataTable / File path public static void SaveCsv (DataTable dt, string filePath) {FileStream fs = null; StreamWriter sw = null Try {fs = new FileStream (filePath + dt.TableName + ".csv", FileMode.Create, FileAccess.Write); sw = new StreamWriter (fs, Encoding.Default); var data = string.Empty; / / write the column name for (var I = 0; I < dt.Columns.Count) Sw.WriteLine +) {data + = dt.Columns [I] .ColumnName; if (I < dt.Columns.Count-1) {data + = ",";}} ColumnName (data) / / write out each row of data for (var I = 0; I < dt.Rows.Count; iTunes +) {data = string.Empty; for (var j = 0; j < dt.Columns.Count; jacks +) {data + = dt.Rows [I] [j] .ToString () If (j < dt.Columns.Count-1) {data + = ",";}} sw.WriteLine (data) } catch (IOException ex) {throw new IOException (ex.Message, ex);} finally {if (sw! = null) sw.Close (); if (fs! = null) fs.Close ();}}

Tags: File data project one line code memory name picture atlas properties openness database text method tag format cache path caching Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Docker Microsoft MySQL