我需要從CSV /製表符分隔的文件中讀取數據,並從.net中寫入這樣的文件。在c#中讀取/寫入CSV /製表符分隔文件
難度在於我不知道每個文件的結構,並且需要將cvs/tab文件寫入數據表,FileHelpers庫似乎不支持。
我已經使用OLEDB爲Excel編寫了它,但無法真正看到爲此寫入選項卡文件的方法,因此將返回到庫。
任何人都可以提供建議嗎?
我需要從CSV /製表符分隔的文件中讀取數據,並從.net中寫入這樣的文件。在c#中讀取/寫入CSV /製表符分隔文件
難度在於我不知道每個文件的結構,並且需要將cvs/tab文件寫入數據表,FileHelpers庫似乎不支持。
我已經使用OLEDB爲Excel編寫了它,但無法真正看到爲此寫入選項卡文件的方法,因此將返回到庫。
任何人都可以提供建議嗎?
這裏有幾個CSV閱讀器的實現:
http://www.codeproject.com/KB/database/CsvReader.aspx
http://www.heikniemi.fi/jhlib/(只是一個庫的一部分,包括CSV作家太)
我懷疑有到CSV轉換的標準方式DataTable或數據庫'自動',你將不得不編寫代碼來做到這一點。如何做到這一點是一個單獨的問題。
爲什麼不使用.NET框架附帶的那個? – 2010-06-29 04:18:33
您將在代碼中創建數據表,並且(假定標題行)可以根據文件中的第一行創建列。之後,它將只是讀取文件並根據其中的數據創建新行。
你可以使用這樣的事情:
DataTable Tbl = new DataTable();
using(StreamReader sr = new StreamReader(path))
{
int count = 0;
string headerRow = sr.Read();
string[] headers = headerRow.split("\t") //Or ","
foreach(string h in headers)
{
DataColumn dc = new DataColumn(h);
Tbl.Columns.Add(dc);
count++;
}
while(sr.Peek())
{
string data = sr.Read();
string[] cells = data.Split("\t")
DataRow row = new DataRow();
foreach(string c in cells)
{
row.Columns.Add(c);
}
Tbl.Rows.Add(row);
}
}
上面的代碼沒有被編譯,所以它可能有一些錯誤,但它應該讓你在正確的軌道上。
您的分割代碼是錯誤的,因爲您可能在引用字符串中包含解構器。 – 2010-06-29 04:14:41
@喬納森艾倫:是的,你的確可以。它的設計不是完整的解決方案......只是指向可能的正確軌道的指針。 – AllenG 2010-06-29 13:05:45
.NET帶有一個稱爲TextFieldParser類的CSV /選項卡分隔文件解析器。
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx
它支持CSV文件,真是好錯誤報告的完整的RFC。
你必須喜歡Microsoft.VisualBasic.dll中的一般實用程序類。 – MgSam 2013-01-11 22:20:04
不幸的是,這似乎並不支持指定您自己的文本包裝字符,它假定這是一個雙引號,只是允許您打開/關閉。 – ChrisProsser 2016-02-05 16:01:46
你能提供一個簡單的例子嗎?鏈接沒有足夠的答案。如果它打破了這個迴應對任何一個Google員工都沒用。 – 2016-08-29 18:59:30
你可以讀寫csv文件.. 這可能會有幫助。
通裂焦炭這個參數 「serparationChar」
例子: -
private DataTable dataTable = null;
private bool IsHeader = true;
private string headerLine = string.Empty;
private List<string> AllLines = new List<string>();
private StringBuilder sb = new StringBuilder();
private char seprateChar = ',';
public DataTable ReadCSV(string path, bool IsReadHeader, char serparationChar)
{
seprateChar = serparationChar;
IsHeader = IsReadHeader;
using (StreamReader sr = new StreamReader(path,Encoding.Default))
{
while (!sr.EndOfStream)
{
AllLines.Add(sr.ReadLine());
}
createTemplate(AllLines);
}
return dataTable;
}
public void WriteCSV(string path,DataTable dtable,char serparationChar)
{
AllLines = new List<string>();
seprateChar = serparationChar;
List<string> StableHeadrs = new List<string>();
int colCount = 0;
using (StreamWriter sw = new StreamWriter(path))
{
foreach (DataColumn col in dtable.Columns)
{
sb.Append(col.ColumnName);
if(dataTable.Columns.Count-1 > colCount)
sb.Append(seprateChar);
colCount++;
}
AllLines.Add(sb.ToString());
for (int i = 0; i < dtable.Rows.Count; i++)
{
sb.Clear();
for (int j = 0; j < dtable.Columns.Count; j++)
{
sb.Append(Convert.ToString(dtable.Rows[i][j]));
if (dataTable.Columns.Count - 1 > j)
sb.Append(seprateChar);
}
AllLines.Add(sb.ToString());
}
foreach (string dataline in AllLines)
{
sw.WriteLine(dataline);
}
}
}
private DataTable createTemplate(List<string> lines)
{
List<string> headers = new List<string>();
dataTable = new DataTable();
if (lines.Count > 0)
{
string[] argHeaders = null;
for (int i = 0; i < lines.Count; i++)
{
if (i > 0)
{
DataRow newRow = dataTable.NewRow();
// others add to rows
string[] argLines = lines[i].Split(seprateChar);
for (int b = 0; b < argLines.Length; b++)
{
newRow[b] = argLines[b];
}
dataTable.Rows.Add(newRow);
}
else
{
// header add to columns
argHeaders = lines[0].Split(seprateChar);
foreach (string c in argHeaders)
{
DataColumn column = new DataColumn(c, typeof(string));
dataTable.Columns.Add(column);
}
}
}
}
return dataTable;
}
我已經找到最好的解決辦法
http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp
只是我不得不重新寫
void ReadTest()
{
// Read sample data from CSV file
using (CsvFileReader reader = new CsvFileReader("ReadTest.csv"))
{
CsvRow row = new CsvRow();
while (reader.ReadRow(row))
{
foreach (string s in row)
{
Console.Write(s);
Console.Write(" ");
}
Console.WriteLine();
row = new CsvRow(); //this line added
}
}
}
對不起..感覺有點離我很遠,但我想我寫csv文件的主要考慮是劃定字符串(這是一段時間,因爲我不得不做csv文件)。 – 2010-06-28 17:33:27
我添加了一個CSV寫入鏈接,你可以用你喜歡的分隔符替換一些常量字符串,它應該沒問題。閱讀遠比寫作複雜得多... – jdehaan 2010-06-28 20:54:05
但是編寫包含任意字符串的CSV文件並不重要。記住轉義序列! – 2010-06-29 04:04:54