テキスト/ datファイルのデータをC#のデータテーブルにロードするには、ここで私は動的にする必要がありますテキストファイルのデータに基づいて列を生成します。これは何ですかテキストファイル/データファイルからデータを読み込む方法c#
0
A
答えて
0
private static System.Data.DataTable SplitColumns()
{
System.Data.DataTable table = new System.Data.DataTable("dataFromFile");
string file="textfile.txt" ==>Get file which you want to split into columns
using (StreamReader sr = new StreamReader(file))
{
string line;
int rowsCount = 0;
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(new string[] { "\t"," " }, StringSplitOptions.RemoveEmptyEntries);==>here i'm using the tab delimeter to split the row line
==>in the file to columns data,You can use your own delimeter
if(table.Columns.Count==0)
{
for (int i = 1; i <= data.Length; i++)
{
table.Columns.AddRange(new DataColumn[] { new DataColumn("col"+(i).ToString(), typeof(string)) });==>here i'm dynamically creating the column headers
==> based on the strings in the line
}
}
table.Rows.Add();
for (int i = 0; i < data.Length; i++)
{
if (data[i].Contains(" "))
data[i] = data[i].Replace(" ", "");
if (!data[i].Equals(""))
table.Rows[rowsCount][i] = data[i];
}
rowsCount++;
}
}
return table;
}
+1
?あなたのコードは動作していませんか?もしそうなら、それを質問と共に掲示する必要があります。 – user3185569
+0
入力テキストファイルの外観は? –
+0
"次のテキストをテキストファイルに追加し、上記のコードを適用する" h1 h2 h3 1 2 3 –
関連する問題
- 1. テキストエリアにデータを読み込む方法
- 2. 外部JSONからのデータをカルーセルに読み込む方法
- 3. データから独立したUIを読み込む方法
- 4. AppDelegate.mからTableViewデータを読み込む方法は?
- 5. sqliteテーブルからデータを読み込む方法は?
- 6. ajaxからzabutoカレンダープラグインにデータを読み込む方法は?
- 7. Cassandraテーブルからデータを読み込む方法
- 8. jsonパーサからデータを読み込む方法
- 9. テキストファイルからデータを高速に読み込む方法
- 10. Google BigQueryからGoogle Cloud Bigtableにデータを読み込む方法
- 11. OpenCV 3.1のファイルからSVMデータを読み込む方法は?
- 12. テキストボックスからリストボックスにデータを読み込む方法は?
- 13. Rubyでデータベースからデータを読み込む方法
- 14. Tableau - URLから直接CSVデータを読み込む方法
- 15. xmlからデータを読み込む
- 16. バーコードからデータを読み込む
- 17. MongoDBからデータを読み込む
- 18. C++の大きなテキストファイルから部分データを読み込む方法
- 19. C++プログラムを外部ファイルから読み込む方法は?
- 20. C#のリソース辞書からDockpanelを読み込む方法
- 21. テーブルを.txtファイルからC++に読み込む方法
- 22. ANSI C - stdinから単語を読み込む方法は?
- 23. C++のポインタからバイトシーケンスを読み込む方法は?
- 24. テキストファイルから読み込むときにApache Ignite Cacheを読み込む方法
- 25. xmlデータをC#でDataSetに読み込む方法は?
- 26. ローカルデータベースから列を読み込むC#
- 27. objective-c excelのデータを読み込む
- 28. excelファイルからデータを読み込むc#をmongodbデータベース
- 29. C++でRAMからファイルに直接データを読み込む
- 30. C#RocksmithのRealtoneケーブルからデータを読み込む
[データテーブルにテキストファイルを読み取る方法]の可能な重複(http://stackoverflow.com/questions/20860101/how-to-read-text-file-to-datatable) – Draken