zoukankan      html  css  js  c++  java
  • 按格式读取csv文件内容

        string path = @"C:Userskeen_DownloadsuploaduploadUpload20140701141934_export.csv";
        ImportDataTable(path);
    
            //2014-07-01
            //get csv file to datatable
            private static DataTable ImportDataTable(string filepath)
            {
                DataTable mydt = new DataTable("myTableName");
                mydt.Columns.Add("Data ID", System.Type.GetType("System.String"));
                mydt.Columns.Add("Field Name", System.Type.GetType("System.String"));
                mydt.Columns.Add("New Value", System.Type.GetType("System.String"));
                DataRow mydr;
                using (System.IO.StreamReader mysr = new System.IO.StreamReader(filepath))
                {
                    int data;
                    char current;
                    StringBuilder text = new StringBuilder();
    
                    IDictionary<int, List<string>> results = new Dictionary<int, List<string>>();
                    bool isInYinHao = false; ;
                    int lineId = 1;
                    int index = 0;
                    while (true)
                    {
                        data = mysr.Read();
                        if (data != -1)
                        {
                            current = (char)data;
                            if (current == '"')
                            {
                                if (isInYinHao)
                                {
                                    isInYinHao = false;
                                }
                                else
                                {
                                    if (index > 0)
                                    {
                                        text.Append(current);
                                    }
    
                                    isInYinHao = true;
                                }
                            }
                            else if (current == ',')
                            {
                                if (isInYinHao)
                                {
                                    text.Append(current);
                                }
                                else
                                {
    
                                    SaveResult(results, lineId, text);
                                    index = 0;
                                    continue;
                                }
                            }
                            else if (current == '
    ')
                            {
                                if (isInYinHao)
                                {
                                    text.Append(current);
                                }
                            }
                            else if (current == '
    ')
                            {
                                if (isInYinHao)
                                {
                                    text.Append(current);
                                }
                                else
                                {
                                    SaveResult(results, lineId, text);
                                    index = 0;
                                    lineId++;
                                    continue;
                                }
                            }
                            else if (current == '')
                            {
                            }
                            else
                            {
                                text.Append(current);
                            }
    
                            index++;
                        }
                        else
                        {
                            //Read to file end.
                            SaveResult(results, lineId, text);
                            break;
                        }
                    }
    
                    foreach (int id in results.Keys)
                    {
                        mydr = mydt.NewRow();
                        for (int i = 0; i < results[id].Count; i++)
                        {
                            if (i > 2)
                            {
                                break;
                            }
    
                            mydr[i] = results[id][i];
                        }
    
                        mydt.Rows.Add(mydr);
                    }
    
                }
    
                return mydt;
            }
            private static void SaveResult(IDictionary<int, List<string>> results, int lineId, StringBuilder text)
            {
                if (!results.ContainsKey(lineId))
                {
                    results.Add(lineId, new List<string>());
                }
    
                results[lineId].Add(text.ToString());
                text.Remove(0, text.Length);
            }
  • 相关阅读:
    【转载】Lua中实现类的原理
    游戏资源压缩
    lua中的继承
    lua滚动文字效果
    【转】IOS版本自定义字体步骤
    luaj luaoc 回调函数传递的一些小总结
    cocos2dx中启用lua脚本
    Lua中调用C++方法
    cocos2dx 某缩放的页面 CCTableView最后一个标签无法点中
    C++ Vector 中自定义对象的排序
  • 原文地址:https://www.cnblogs.com/kennyliu/p/3839911.html
Copyright © 2011-2022 走看看