如何在C#windows应用程序中以编程方式打开用'\t'分隔的CSV文件 [英] How to open a CSV file separated with '\t' programmatically in C# windows application

查看:232
本文介绍了如何在C#windows应用程序中以编程方式打开用'\t'分隔的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个CSV文件,其中的列用'\t'分隔。我需要通过适当的列分隔以编程方式打开文件。请帮我在运行时在Windows应用程序中查看该文件。试过下面的代码,但对我不起作用。它仍然没有达到要求的格式。



提前致谢。



我是什么尝试过:



Hi,
I have a CSV file in which the columns are separated with '\t'. I need to open the file programmatically with proper separation of columns. Please help me to view the file in windows application in runtime. Tried the below code but didn't work for me. It is still not in the required format.

Thanks in advance.

What I have tried:

Excel.Application xlApp;
              object misValue = System.Reflection.Missing.Value;
              xlApp = new Excel.Application();
              xlApp.Workbooks.OpenText(@"c:\polsamples\POLList_201633.csv", Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited);
              xlApp.Visible = true;

推荐答案

我认为你正在使用Interop。避免使用它。



您可以使用此处提到的开源项目编写自己的逻辑 - C#将csv转换为xls(使用现有的csv文件) [ ^ ]
I think you are using Interop. Avoid using that.

You can write your own logic using open source projects mentioned over here - C# convert csv to xls (using existing csv file)[^]


我最近发布了一篇关于CSV文件解析器的文章。现在,它只使用逗号作为分隔符,但是使分隔符可配置是一个小练习。



CSV文件解析器 [ ^ ]
I recently posted an article for a CSV file parser. Right now, it only uses a comma as the separator, but it would be a minor exercise to make the delimiter configurable.

CSV File Parser[^]


Excel知道如何打开这些文件。

使用宏录制功能,你可以记录打开文件的步骤,当记录完成后,你可以看到excel打开了什么。

翻译你的代码不应该太复杂。
Excel know how to open such files.
By using the macro recording feature, you can record steps to open the file and when record is finished, you can see what excel did to open.
It should not be too complicated to translate in your code.


这篇关于如何在C#windows应用程序中以编程方式打开用'\t'分隔的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆