Dan Harris
Dan Harris

Reputation: 1386

Importing a TXT file to a DataTable so it matches how Excel imports it - C# .NET

I have a txt file, and I can import it into Excel with the following settings perfectly:

Type: Delimited

Delimiters: TAB

Text Qualifier: None

I tried loading it into a DataTable by reading the input file into a String[] array, and then splitting that String[] array.

However how do you represent a TAB when splitting a string? Is this even possible?

How can I import my TAB delimited txt file into a DataTable? If it were comma separated I think my code would work, but not a clue how to represent a TAB?...

Any ideas or code samples showing how to do this?

Any help greatly appreciated.

Dan

Upvotes: 0

Views: 2523

Answers (3)

Dan Harris
Dan Harris

Reputation: 1386

I have found the answer to this here:

Efficient function for reading a delimited file into DataTable

Sorry for creating something similar. Should have searched more thoroughly.

Upvotes: 0

Wyatt Barnett
Wyatt Barnett

Reputation: 15663

We are tool using apes. I'm just sayin'

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 55019

Just use '\t' for a tab.

So something like this would probably work (though I might be mixing up VB.Net and C# syntax right now):

string rows[]; // read in file here
foreach(string row in rows)
{
     string values[] = row.Split('\t');
     foreach(string value in values)
     {
          // do something
     }
 }

Upvotes: 1

Related Questions