zoukankan      html  css  js  c++  java
  • c# 简单文件流读写CSV文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Reflection;
    using System.IO;
    using System.Data;
    using System.Web;
    using System.Data.Odbc;
    using System.Text.RegularExpressions;
    using System.Collections;
    
    namespace KernelClass
    {
        public class CSVHelper
        {
    
            /// <summary>
            /// The class CSVHelper read the data from CSV file and store it in a DataTable class,
            /// and allows to return a column with index or column name.
            /// </summary>
            private static readonly char[] FormatSplit = new char[] { ',' };
            private const string replaceDoubleQuotes = "$replaceDoubleQuotes$";
            private const string tableName = "csvTable";
    
            public static DataTable ReadCSV(string filePath)
            {
                FileInfo fi = new FileInfo(filePath);
                if (fi == null || !fi.Exists) return null;
    
                StreamReader reader = new StreamReader(filePath);
    
                string line = string.Empty;
                int lineNumber = 0;
    
                DataTable dt = new DataTable();
    
                while ((line = reader.ReadLine()) != null)
                {
                    if (lineNumber == 0)
                    {
                        dt = CreateDataTable(line);
                        if (dt.Columns.Count == 0) return null;
                    }
                    else
                    {
                        bool isSuccess = CreateDataRow(ref dt, line);
                        if (!isSuccess)
                        {
                            throw new FileLoadException("There are some data unconsistent in your file.");
                        }
                    }
                    lineNumber++;
                }
                    reader.Close();
    
                //如果最后一列是空的话就删除
                bool HasValueForLastestColumn = false;
                int indexLastestColumn = dt.Columns.Count -1;
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[indexLastestColumn][i].ToString().Trim())) {
                        HasValueForLastestColumn = true;
                        break;
                    }
                }
                if (HasValueForLastestColumn) {
                    dt.Columns.RemoveAt(indexLastestColumn);
                }
                return dt;
            }
    
            public static bool WriteCSV(DataTable _dataSourc, string filePath)
            {
                string data = ExportCSV(_dataSourc);
                return PhysicalFile.SaveFile(data, filePath);
            }
    
            public static string ExportCSV(DataTable _dataSource)
            {
                StringBuilder strbData = new StringBuilder();
                foreach (DataColumn column in _dataSource.Columns)
                {
                    strbData.Append(column.ColumnName + ",");
                }
                strbData.Append("
    ");
                foreach (DataRow dr in _dataSource.Rows)
                {
                    for (int i = 0; i < _dataSource.Columns.Count; i++)
                    {
                        string rowValue = dr[i].ToString().Replace(""", """");
                        if(rowValue.Contains(','))
                            strbData.Append(""" + dr[i].ToString().Replace(""", """") + "",");
                        else
                            strbData.Append(dr[i].ToString() + ",");
                    }
                    strbData.Append("
    ");
                }
                return strbData.ToString();
            }
    
            public static List<double> GetColumnWithName(string columnName, DataTable dt)
            {
                List<double> list = new List<double>();
    
                var index = dt.Columns.IndexOf(columnName);
                list = GetColumnWithIndex(index, dt);
    
                return list;
            }
    
            public static List<double> GetColumnWithIndex(int index, DataTable dt)
            {
                List<double> list = new List<double>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    var s = dr[index].ToString();
                    if (string.Compare(s, "") == 0)
                    {
                        break;
                    }
                    double value = Convert.ToDouble(s);
                    list.Add(value);
                }
    
                return list;
            }
    
            private static DataTable CreateDataTable(string line)
            {
                DataTable dt = new DataTable();
                foreach (string field in
                    line.Split(FormatSplit, StringSplitOptions.None))
                {
                    dt.Columns.Add(field);
                }
                return dt;
            }
    
            private static bool CreateDataRow(ref DataTable dt, string line)
            {
                DataRow dr = dt.NewRow();
                string src = string.Empty;
                Hashtable fields = new Hashtable();
    
                if (!string.IsNullOrEmpty(line))
                {
                    src = line.Replace("""", replaceDoubleQuotes);
                    //正则表达式找出用双引号包括的字符串, 下面循环是为了防止字符串中含有分隔符 ,
                    MatchCollection col = Regex.Matches(src, ""([^"]+)"", RegexOptions.ExplicitCapture);
                    IEnumerator ie = col.GetEnumerator();
    
                    while (ie.MoveNext())
                    {
                        string patn = ie.Current.ToString();
                        int key = src.Substring(0, src.IndexOf(patn)).Split(',').Length-1;
    
                        if (!fields.ContainsKey(key))
                        {
                            fields.Add(key, patn.Trim(new char[] { ',', '"' }));
                            src = src.Replace(patn, "");
                        }
                    }
    
                    string[] arr = src.Split(',');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (!fields.ContainsKey(i))
                            fields.Add(i, arr[i]);
                    }
                }
    
                if (fields.Count == 0 || fields.Count > dt.Columns.Count)
                {
                    return false;
                }
    
                for (int i = 0; i < fields.Count; i++)
                {
                    dr[i] = fields[i].ToString().Replace(replaceDoubleQuotes, """);
                }
    
                dt.Rows.Add(dr);
                return true;
            }
        }
    
    }
  • 相关阅读:
    在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好
    # 会员注册与登录模块
    文本文件从磁盘读取、写入
    简单的web三层架构系统【第五版】
    Nginx负载均衡中后端节点服务器健康检查的一种简单方式
    编译安装php-7.1.17及部分扩展
    wkhtmltopdf 安装过程不包含php扩展部分
    Centos6下安装中文字体
    xen 配置vm 跟随xen server一起启动
    CENTOS 升级Nodejs 到最新版本
  • 原文地址:https://www.cnblogs.com/dfg727/p/3150395.html
Copyright © 2011-2022 走看看