Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Convert DataTable to CSV file

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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 ();}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report