我如何在 C# 中处理这个文件?
我有一个文本文件,其中包含数百个条目,格式如下,我需要在数据网格或其他用户友好的控件“正确格式化”上显示此文件,按照您的建议。 (我的目的是将其导出到 excel 虽然不是最佳选择以进行进一步处理。所有列都是以毫秒为单位的值,所以大多数时候这些值都很大,长达 3 小时,所以我需要将它们转换为小时/分钟/秒。)完成此任务的最佳方法是什么?这可以用 StreamReader 来完成吗?转换是否可以在代码中进行,从而无需导出到 Excel?有样品吗?谢谢你!
ENTRY DAY ENTRY TIME IN.WORK TEST % TRY CORRER CORTA CICLO MAQUI
O0.TXT
11/07/28 13:39:13 0 0 105 0 0 0 0
O0.TXT
11/07/20 00:00:00 0 0 19145 0 0 0 0
O0.TXT
11/07/19 15:04:10 0 0 32151 0 0 0 0
我想将其格式化为这样:
FILE ENTRY DAY ENTRY TIME IN.WORK TEST % TRY CORRER CORTA CICLO MAQUI
O0.TXT 11/07/28 13:39:13 0 0 105 0 0 0 0
O0.TXT 11/07/20 00:00:00 0 0 19145 0 0 0 0
O0.TXT 11/07/19 15:04:10 0 0 32151 0 0 0 0
O0.TXT 11/07/07 05:22:40 0 0 508 0 0 0 0
我尝试使用:
string fileName = "Myfile.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { " " };
while (!sr.EndOfStream)
{
string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
........
但我根本无法保留我想要的格式,也无法将其加载到数据网格等中。
I have a text file with hundreds of entries formatted like this, I need to display this file on a data grid, or other user friendly control "properly formatted" as per your advise. (my intention is to then export it to excel not an optimal choice though for further processing. all the columns are values in milliseconds so most of times these values are quite big up to 3 hrs, so I need to convert them into hours/minutes/seconds.) What is the best way to approach this task? Can this be done with the StreamReader? Can the conversion take place in code so there is no need to export to excel? Any samples? thank you!
ENTRY DAY ENTRY TIME IN.WORK TEST % TRY CORRER CORTA CICLO MAQUI
O0.TXT
11/07/28 13:39:13 0 0 105 0 0 0 0
O0.TXT
11/07/20 00:00:00 0 0 19145 0 0 0 0
O0.TXT
11/07/19 15:04:10 0 0 32151 0 0 0 0
I want to format it as such:
FILE ENTRY DAY ENTRY TIME IN.WORK TEST % TRY CORRER CORTA CICLO MAQUI
O0.TXT 11/07/28 13:39:13 0 0 105 0 0 0 0
O0.TXT 11/07/20 00:00:00 0 0 19145 0 0 0 0
O0.TXT 11/07/19 15:04:10 0 0 32151 0 0 0 0
O0.TXT 11/07/07 05:22:40 0 0 508 0 0 0 0
I tried using the:
string fileName = "Myfile.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { " " };
while (!sr.EndOfStream)
{
string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
........
But I was not able to hold the format I wanted at all, nor to load it into a datagrid etc. for that matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一切都确定了吗?例如,日期是否始终为 6 个字符且长度为 8 个字符(或任何情况)?如果是这样,我会拆分“\r\n”而不是“”,然后单独处理每一行。您可以这样做:
当然,您必须想出一些需要处理哪些行的逻辑。
Is everything fixed? For example, is the date always 6 characters in and 8 characters long (or whatever the case may be)? If so I would split on "\r\n" as opposed to " ", and then process each line individually. You could do:
Of course, you'll have to come up with some logic for which lines you need to process.