我如何在 C# 中处理这个文件?

发布于 2024-11-27 14:09:19 字数 1555 浏览 3 评论 0原文

我有一个文本文件,其中包含数百个条目,格式如下,我需要在数据网格或其他用户友好的控件“正确格式化”上显示此文件,按照您的建议。 (我的目的是将其导出到 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 技术交流群。

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

发布评论

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

评论(2

假情假意假温柔 2024-12-04 14:09:19
string fileName = "Myfile.txt";

string[] delimiter = new string[] { " " };

List<string[]> rows = new List<string[]>();

string[] liens = File.ReadAllLines(fileName);

foreach (string line in liens)
{
    rows.Add(line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries));
}

//Now if you want to display them each one row item separated by tab "`\t"` or csv `","`
string separator = "\t";

List<string> output = new List<string>();

foreach (string[] row in rows)
{
    output.Add(string.Join(separator, row));
}

string newFile = "result.txt";
File.WriteAllLines(newFile, output.ToArray());//save the output to new file.
string fileName = "Myfile.txt";

string[] delimiter = new string[] { " " };

List<string[]> rows = new List<string[]>();

string[] liens = File.ReadAllLines(fileName);

foreach (string line in liens)
{
    rows.Add(line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries));
}

//Now if you want to display them each one row item separated by tab "`\t"` or csv `","`
string separator = "\t";

List<string> output = new List<string>();

foreach (string[] row in rows)
{
    output.Add(string.Join(separator, row));
}

string newFile = "result.txt";
File.WriteAllLines(newFile, output.ToArray());//save the output to new file.
心碎无痕… 2024-12-04 14:09:19

一切都确定了吗?例如,日期是否始终为 6 个字符且长度为 8 个字符(或任何情况)?如果是这样,我会拆分“\r\n”而不是“”,然后单独处理每一行。您可以这样做:

string fileName = "Myfile.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { "\r\n" };
while (!sr.EndOfStream)
{
    string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
    foreach (string line in lines)
    { 
        date = line.Substring(6, 8);
        // Do the other processing here.
    }
}

当然,您必须想出一些需要处理哪些行的逻辑。

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:

string fileName = "Myfile.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { "\r\n" };
while (!sr.EndOfStream)
{
    string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
    foreach (string line in lines)
    { 
        date = line.Substring(6, 8);
        // Do the other processing here.
    }
}

Of course, you'll have to come up with some logic for which lines you need to process.

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