我想讀取 csv 檔案并保存到串列或陣列或任何東西,但CsvHelper要求將其保存為特定型別的集合。問題是我的 csv 有這么多列,將其映射到自定義類需要幾周時間。
我怎樣才能閱讀它而不保存為特定型別?使用類似的東西訪問特定值row[1][2]對我來說已經足夠了。
uj5u.com熱心網友回復:
將其添加到資料表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = @"c:\temp\test.csv";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
DataTable dt = new DataTable();
int rowCount = 0;
while((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
string[] splitArray = line.Split(new char[] { ',' });
if (rowCount == 0)
{
foreach (string col in splitArray)
{
dt.Columns.Add(col, typeof(string));
}
}
else
{
dt.Rows.Add(splitArray);
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/494957.html
上一篇:該用戶帳戶所有者的權限
