从CSV文件中获取数据的最佳方法是什么? [英] What is the best way of getting the data out of CSV file?
本文介绍了从CSV文件中获取数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要打开一个CSV文件,这样做的最佳方式是什么?
var reader = new StreamReader( File.OpenRead( @ C:\ MeterDataCSV.CSV));
我相信这会打开CSV。现在我打开它来阅读如何获取数据,我确信分配阅读器到文本框或富文本框?我可以......
我找到了:
快速CSV阅读器 [ ^ ]
解决方案
我使用这个方便的方法来解析.csv文件:
这是我调用方法然后迭代数据的地方:
List< string []> data = new List< string []>();
data = parseCSV(path);
foreach(数据中的字符串[]项目)
{
}
path是你文件的位置。
public List< string []> parseCSV(字符串路径)
{
List< string []> parsedData = new List< string []>();
尝试
{
使用(StreamReader readFile = new StreamReader(path))
{
string line;
string [] row;
while((line = readFile.ReadLine())!= null)
{
row = line.Split(',');
parsedData.Add(row);
}
}
}
catch(例外e)
{
MessageBox.Show(e.Message);
}
返回parsedData;
}
我认为textbox或richtextbox不适合像数据一样显示csv文件表
您可以使用快速CSV阅读器并将数据直接绑定到下面的数据网格
< pre lang =cs> 使用 System.IO;
使用 LumenWorks.Framework.IO.Csv;
void ReadCsv()
{
// 打开文件data.csv,这是一个带文件的CSV文件
使用(CachedCsvReader csv = < span class =code-keyword> new
CachedCsvReader( new StreamReader( @ C:\ MeterDataCSV.CSV), true ))
{
// 字段标题将自动用作列名
myDataGrid.DataSource = CSV;
}
}
Hi All,
I need to open a CSV file, what is the best way of doing this?
var reader = new StreamReader(File.OpenRead(@"C:\MeterDataCSV.CSV"));
This I am sure opens the CSV. Now I have it open for reading how can I get the data out, I am sure that a assigning reader to a text box or a rich text box? can I...
I have found:
A Fast CSV Reader[^]
解决方案
I use this handy method to parse a .csv file:
Here is where I call the method and then iterate the data:
List<string[]> data = new List<string[]>(); data = parseCSV(path); foreach (string[] item in data) { }
"path" is the location of your file.
public List<string[]> parseCSV(string path) { List<string[]> parsedData = new List<string[]>(); try { using (StreamReader readFile = new StreamReader(path)) { string line; string[] row; while ((line = readFile.ReadLine()) != null) { row = line.Split(','); parsedData.Add(row); } } } catch (Exception e) { MessageBox.Show(e.Message); } return parsedData; }
i don't think textbox or richtextbox suitable for display csv file table like data
you can use Fast CSV reader and bind data directly to a data grid like below
using System.IO; using LumenWorks.Framework.IO.Csv; void ReadCsv() { // open the file "data.csv" which is a CSV file with headers using (CachedCsvReader csv = new CachedCsvReader(new StreamReader(@"C:\MeterDataCSV.CSV"), true)) { // Field headers will automatically be used as column names myDataGrid.DataSource = csv; } }
这篇关于从CSV文件中获取数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文