将 TXT 文件导入到 DataTable,使其与 Excel 的导入方式相匹配 - C# .NET

发布于 2024-09-17 06:36:50 字数 386 浏览 3 评论 0原文

我有一个 txt 文件,我可以使用以下设置完美地将其导入 Excel:

类型: 分隔

分隔符: TAB

文本限定符:

我尝试通过将输入文件读入 String[] 数组,然后拆分该 String[] 数组,将其加载到 DataTable 中。

但是,在分割字符串时如何表示 TAB 呢?这可能吗?

如何将 TAB 分隔的 txt 文件导入到 DataTable 中?如果它是逗号分隔的,我认为我的代码可以工作,但不知道如何表示 TAB?...

任何想法或代码示例显示如何做到这一点?

非常感谢任何帮助。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情域 2024-09-24 06:36:51

只需使用 '\t' 作为选项卡即可。

所以类似这样的东西可能会起作用(尽管我现在可能会混淆 VB.Net 和 C# 语法):

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

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
     }
 }
羁〃客ぐ 2024-09-24 06:36:51

我们是使用猿的工具。我只是说说而已

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

夏日落 2024-09-24 06:36:51

我在这里找到了答案:

用于读取文件的高效函数将分隔文件放入 DataTable

抱歉创建了类似的内容。应该更彻底地搜索。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文