Exemplo n.º 1
0
 private void btnOpen_Click(object sender, RoutedEventArgs e)
 {
     OpenFileDialog open = new OpenFileDialog();
     open.Title = "打开文件";
     open.Filter = "文件(.txt)|*.txt|所有文件|*.*";
     if((bool)open.ShowDialog().GetValueOrDefault())
     {
          csvFilePath = open.FileName;
         lblReulstMsg.Content = "文件导入成功,请查看导入文件内容";
         using (SimpleCSVReader reader = new SimpleCSVReader(csvFilePath)) {
             reader.Splitter = '\t';
             reader.ReadHeader();
             Dictionary<string, int> dict = reader.HeaderMap;
             comBoxList.Items.Clear();
             LogHelper.WriteLog("原文件中的列名:");
             LogHelper.WriteLog("####################################################################");
             foreach (var item in dict)
             {
                 int index = (int)item.Value - 1;
                 comBoxList.Items.Add(index.ToString() + "<<-->>" + item.Key.ToString());
                 LogHelper.WriteLog(item.Key.ToString()); 
             }
             LogHelper.WriteLog("####################################################################");
         }
        
     } 
 }
Exemplo n.º 2
0
        public CSVHelper(string filePath, char spliter)
        {
            try
            {
                reader = new SimpleCSVReader(filePath);

                reader.Splitter = spliter;
                reader.ReadHeader();
                Header = new List<string>();
                foreach (var item in reader.HeaderMap)
                {
                    Header.Add(item.Key.ToString());
                }

                DataTable dt = new DataTable();
                foreach (var item in Header)
                {
                    if (!dt.Columns.Contains(item))
                        dt.Columns.Add(item, Type.GetType("System.String"));
                }
                while (reader.ReadLine())
                {
                    DataRow dr = dt.NewRow();
                    foreach (var item in Header)
                    {
                        dr[item] = reader[item];
                    }
                    dt.Rows.Add(dr);
                }
                CsVTable = dt;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message,ex);
                
            }
           
        }