[reprint] C ා tool class: Csv file conversion class

csv is a comma separated value format file that stores table data (numbers and text) in plain text. A csv file consists of any number of records separated by a line break character. Each record consists of fields. The separators between fields are other characters or strings, the most common being commas or tabs. In C ා, it is sometimes necessary to read and write the csv file. Therefore, a tool class CsvHelper is encapsulated. In a word, C ා exports data to csv files at a relatively fast speed. Sometimes when exporting Excel files at a very slow speed, you can choose to export the. csv file first, and then open it locally with Excel software.

CsvHelper class mainly uses C ා to operate the Csv file, which mainly contains two methods.

Dt2csv (omitted parameter): import data into CSV file

Csv2dt (omitted parameter): read CSV into DataTable

The specific tool class implementation is as follows:

 /// <summary>
    /// CSV File conversion class
    /// </summary>
    public static class CsvHelper
    {
        /// <summary>
        /// Export report as Csv
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="strFilePath">Physical path</param>
        /// <param name="tableheader">Header</param>
        /// <param name="columname">Field title,Comma separation</param>
        public static bool dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)
        {
            try
            {
                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
                strmWriterObj.WriteLine(tableheader);
                strmWriterObj.WriteLine(columname);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dt.Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// take Csv Read in DataTable
        /// </summary>
        /// <param name="filePath">csv File path</param>
        /// <param name="n">Express the first n Row is field title,The first n+1 Line is record start</param>
        public static DataTable csv2dt(string filePath, int n, DataTable dt)
        {
            StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);
            int i = 0, m = 0;
            reader.Peek();
            while (reader.Peek() > 0)
            {
                m = m + 1;
                string str = reader.ReadLine();
                if (m >= n + 1)
                {
                    string[] split = str.Split(',');
                    System.Data.DataRow dr = dt.NewRow();
                    for (i = 0; i < split.Length; i++)
                    {
                        dr[i] = split[i];
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
    }

 

Note: This article is reproduced from C ා tool class: Csv file conversion class ා IT technology fun house.

Tags: C# Excel encoding

Posted on Mon, 02 Dec 2019 10:11:09 -0500 by JeDi58