如何将txt文件中的数据拆分为Excel?

How to split data in txt file to Excel?

提问人:Mehmet Ali Karabulut 提问时间:11/15/2023 更新时间:11/18/2023 访问量:83

问:

我有数百个带有错误日志的.txt文件,如下所示。我的目标是通过选择我想要的文件(可以选择多个文件)在 Excel 上创建一个报告,其中包含文件中每个错误的名称、日期、重复次数和错误内容。如何使用 C# 做到这一点?

示例数据(我们必须假设此数据位于 txt 文件中,由数千行组成):

2023-11-14 00:00:00,123 |1|ERROR||Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 00:00:00,546 |2|ERROR|Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 00:00:00,624 |1|ERROR|Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 00:00:00,555 |111|ERROR|||ExCode: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 00:00:00,666 |111|ERROR|ExCode: Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.
156156156 xyz Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 01:00:00,199 |1|ERROR|Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2023-11-14 01:00:00,266 |2|ERROR|Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.

我尝试了如下所示的 C#。但我不能拆分数据。我该怎么做?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            String line;
            List<string> sList = new List<string>();
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("deneme.txt");
                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    string[] pieces = line.Split('|');
                    //write the line to console window
                    //Console.WriteLine(line);
                    for (int i = 0; i < line.Length; i++)
                    {
                        Console.WriteLine(pieces[i]);
                    }
                   
                    //Read the next line
                    line = sr.ReadLine();
                }
                //close the file
                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
        
    }
}
C# Excel

评论

0赞 devlin carnate 11/15/2023
您是否看到您的代码产生了错误:“索引超出了数组的边界”?尝试遍历而不是 : ,因为您的数组已被拆分。 只是未拆分的文本行。pieceslinefor (int i = 0; i < pieces.Length; i++)piecesline
0赞 Ihdina 11/18/2023
在一行日志中,有时字符“|”不止一个。根据“|”字符进行解析,并删除空条目。像这样:String[] words = line。Split(new String[] { “|” }, StringSplitOptions.RemoveEmptyEntries);–

答:

-2赞 devlin carnate 11/15/2023 #1

您的代码产生了错误:“索引超出了数组的边界”,并且它产生了该错误,因为您正在使用 的长度来确定要在 中使用的索引。这将给你一个无效的索引。长度 of 是将字符串拆分为数组之前的长度,该值与 的索引没有关系。line'pieceslinepieces

您可以看到差异:

Console.WriteLine(line.Length);
Console.WriteLine(pieces.Length);

在下面的示例中,是 94 和 5。line.Lengthpieces.Length

您的问题是您正在尝试使用 的长度进行迭代。您需要使用 的长度来确保循环中的索引边界不超过。pieceslinepieces

var line = "2023-11-14 00:00:00,123 |1|ERROR||Ex: Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

string[] pieces = line.Split('|');

for (int i = 0; i < pieces.Length; i++)
{
     Console.WriteLine(pieces[i]);
}

您的代码也不会在文件中的所有行上进行交互。它只读取一行。要跨所有线路进行交互,请执行以下操作:

using (StreamReader sr = File.OpenText(("deneme.txt"))
{
     var line = String.Empty;
     //this will loop over each line in the file
     while ((line = sr.ReadLine()) != null)
     {
         //split your line into an array
         string[] pieces = line.Split('|');

         //iternate over the array
         for (int i = 0; i < pieces.Length; i++)
         {
              Console.WriteLine(pieces[i]);
         }   
     }

}

注意:实现了 iDisposable,所以用 see here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using?redirectedfrom=MSDNStreamReaderusing

另外,我的答案是解决您提供的代码中的问题。您没有显示或解释您在将拆分线写入 Excel 时是否有问题,因此我假设这不是您问题的一部分。

评论

0赞 Mehmet Ali Karabulut 11/15/2023
但是我想用 C# 读取 txt 文件。每个 txt 文件包含数百行。如何在源代码上实现代码?
0赞 devlin carnate 11/15/2023
@MehmetAliKarabulut 我理解这一点,但我给你举个例子,说明你读到的每一行都发生了什么。替换为代码中的 。for (int i = 0; i < line.Length; i++)for (int i = 0; i < pieces.Length; i++)
0赞 devlin carnate 11/15/2023
@MehmetAliKarabulut,看起来您只在代码中读取了一行文件。我将更新我的答案,以展示如何遍历文件中的所有行。
0赞 devlin carnate 11/15/2023
@MehmetAliKarabulut - 我已经更新了。
-1赞 Ihdina 11/15/2023 #2
  1. 首次安装Microsoft.Office.Interop.Excel

    从 > > > > 然后键入>然后输入。VS2022ToolsNuget Package ManagerPackage Manager ConsoleNuGet\Install-Package Microsoft.Office.Interop.Excel -Version 15.0.4795.1001

  2. 尝试 C# 代码,如下所示:

using Microsoft.Office.Interop.Excel;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace LogParsing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // create excel
                Application logApp = new Application();
                if (logApp == null)
                {
                    return;
                }

                // collect all log files
                String[] files = Directory.GetFiles(@"d:\mylog", "*.txt");
                if (files == null || files.Length == 0)
                {
                    return;
                }

                foreach (String file in files)
                {
                    if (String.IsNullOrEmpty(file))
                    {
                        // next file
                        continue;
                    }

                    // read file content
                    String[] lines = File.ReadLines(file).ToArray();
                    if (lines == null)
                    {
                        // next line
                        continue;
                    }

                    // create workbook
                    Workbook logWorkbook = logApp.Workbooks.Add();
                    if (logWorkbook == null)
                    {
                        continue;
                    }

                    // create worksheet
                    Worksheet logWorksheet = (Worksheet)logWorkbook.Sheets.Add();
                    logWorksheet.Name = Path.GetFileNameWithoutExtension(file);
                    if (logWorksheet == null)
                    {
                        continue;
                    }

                    int row = 1;
                    foreach (String line in lines)
                    {
                        if (line == null)
                        {
                            continue;
                        }

                        // parsing words with remove empty data
                        String[] words = line.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                        if (words == null || words.Length == 0)
                        {
                            // next when words is empty
                            continue;
                        }
                        if (words.Length == 4)
                        {
                            // fill worksheet
                            for (int i = 0; i < words.Length; i++)
                            {
                                String word = words[i];
                                logWorksheet.Cells[row, 1 + i] = word;
                            }

                            // next row
                            ++row;
                        }
                    }

                    // save worksheet
                    logApp.ActiveWorkbook.SaveAs(Path.GetDirectoryName(file) + "/" + Path.GetFileNameWithoutExtension(file) + ".xls", XlFileFormat.xlWorkbookNormal);

                    // close worksheet
                    logWorkbook.Close();
                }
                logApp.Quit();

                MessageBox.Show("Succes");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }
    }
}

  1. 右键单击>选择以自动更新命名空间(无需手动更新)。Your SolutionSync Namespaces

  2. 运行您的 .Program

结果:

enter image description here