C#利用LumenWorks.Framework.IO.Csv读取CSV文件

 static DataTable GetData(Stream stream)
        {
            using (stream)
            {
                using (StreamReader input = new StreamReader(stream, Encoding.GetEncoding("shift_jis")))
                {
                    using (CsvReader csv = new CsvReader(input, false))
                    {
                        DataTable dt = new DataTable();
                        int columnCount = csv.FieldCount;
                        for (int i = 0; i < columnCount; i++)
                        {
                            dt.Columns.Add("col" + i.ToString());
                        }

                        while (csv.ReadNextRecord())
                        {
                            DataRow dr = dt.NewRow();
                            for (int i = 0; i < columnCount; i++)
                            {
                                if (!string.IsNullOrWhiteSpace(csv[i]))
                                {
                                    dr[i] = csv[i];
                                }
                            }
                            dt.Rows.Add(dr);
                        }
                        return dt;
                    }

                }
            }
        }

  需要引用LumenWorks.Framework.IO.dll,读取的时候编码格式要选对,否则会乱码,表头自己设置

C#利用LumenWorks.Framework.IO.Csv读取CSV文件

上一篇:[WPF系列]-参考文献


下一篇:C#中new和override的区别