将文本文件转换为Excel

将文本文件转换为Excel,excel,text-files,vba,Excel,Text Files,Vba,我需要将文本文件转换为Excel文件。我找到了关于这方面的文章,但我的要求有点不同,所以我不知道 我有包含此格式行的文本文件 Jun 13 07:35:08 mail dovecot: pop3-login: Login: user=<veena,.patel@test.com>, method=PLAIN, rip=102.201.122.131, lip=103.123.113.83, mpid=33178, session=<Wfdfdfcxvc> Jun 13

我需要将文本文件转换为Excel文件。我找到了关于这方面的文章,但我的要求有点不同,所以我不知道

我有包含此格式行的文本文件

Jun 13 07:35:08 mail dovecot: pop3-login: Login: user=<veena,.patel@test.com>, method=PLAIN, rip=102.201.122.131, lip=103.123.113.83, mpid=33178, session=<Wfdfdfcxvc>
Jun 13 07:35:08 mail dovecot:pop3登录:登录:user=,method=PLAIN,rip=102.201.122.131,lip=103.123.113.83,mpid=33178,会话=
我想创建包含四列的Excel文件:-

  • 第一列包括上述行的“Jun 13 07:35:08”
  • 第二列包括上述行的“pop3”
  • 第三栏包括“veena”,。patel@test.com"
  • 第四列包括“102.201.122.131”

Excel中不需要所有其他数据。我该怎么做?我知道这不是我想要的。我应该先写一些关于我尝试过的东西的代码,但我真的不知道。如果你喜欢VBA,在看了一些教程之后,你需要这种方法。
一,。通过命令获取文本文件
二,。我的方法是使用每个所需条件的UDF-提示:您可以检查您的正则表达式逻辑-。
以下是提取电子邮件的示例:

Function UserInString(StringToAnalyze As String) As String
Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
Dim Regexmatches As Variant
Dim ItemMatch As Variant
    With regex
      .Pattern = "[a-z]{1,99}[,][.][A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"
      .Global = True
    End With

    If regex.Test(StringToAnalyze) = True Then 'there's a user in the string! ' 1. If regex.Test(StringToAnalyze) = True
    Set Regexmatches = regex.Execute(StringToAnalyze)
    For Each ItemMatch In Regexmatches
      UserInString = IIf(UserInString = "", ItemMatch, ItemMatch & "," & UserInString)
    Next ItemMatch
    Else ' 1. If regex.Test(StringToAnalyze) = True
    UserInString = "There's no user in the string!"
    End If ' 1. If regex.Test(StringToAnalyze) = True
End Function

私有作废按钮2\u单击(对象发送者,事件参数e)
{
尝试
{
如果(!String.IsNullOrWhiteSpace(文件名))
{
if(System.IO.File.Exists(fileName))
{
//字符串fileContant=System.IO.File.ReadAllText(文件名);
System.Text.StringBuilder sb=新的StringBuilder();
列表国家=新列表();
Country=null;
弦线;
字符串[]arrLine;
System.IO.StreamReader file=新的System.IO.StreamReader(文件名);
而((line=file.ReadLine())!=null)
{
如果(!string.IsNullOrWhiteSpace(行))
{
if(line.Contains(“rip=”))
{
arrLine=line.Split(新字符串[]{“邮件”},StringSplitOptions.None);
如果(arrLine.Length>1)
{
sb.追加(arrLine[0]+“;”);
if(line.Contains(“pop3登录:))
{
某人追加(“pop3;”;
}
else if(line.Contains(“imap登录:”)
{
某人追加(“imap;”;
}
其他的
{
某人加上(“;”);
}
arrLine=line.Split(新字符串[]{“user=,”},StringSplitOptions.None);
如果(arrLine.Length>1)
{
sb.追加(arrLine[0]+“;”);
}
其他的
{
某人加上(“;”);
}
}
其他的
{
某人加上(“;”);
}
arrLine=line.Split(新字符串[]{“rip=”},StringSplitOptions.None);
如果(arrLine.Length>1)
{
arrLine=arrLine[1]。拆分(新字符串[]{,“},StringSplitOptions.None);
如果(arrLine.Length>1)
{
sb.追加(arrLine[0]+“;”);
country=countries.FirstOrDefault(a=>a.IP==arrLine[0]);
if(country!=null&&!string.IsNullOrWhiteSpace(country.IP))
{
sb.附加(country.Name+“;”);
}
其他的
{
sb.追加(GetCountryByIP(arrLine[0],ref countries)+“;”);
}
}
其他的
{
某人加上“;”;
}
}
其他的
{
某人加上“;”;
}
sb.Append(系统、环境、新行);
}
}
}
}
file.Close();
DialogResult DialogResult=saveFileDialog1.ShowDialog();
字符串saveFileName=Application.StartupPath+@“\data.csv”;
if(dialogResult==dialogResult.OK)
{
saveFileName=saveFileDialog1.FileName;
}
System.IO.File.WriteAllText(saveFileName,sb.ToString());
Show(“文件保存在”+saveFileName);
fileName=string.Empty;
textBox1.Text=string.Empt
 private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (!String.IsNullOrWhiteSpace(fileName))
            {
                if (System.IO.File.Exists(fileName))
                {
                    //string fileContant = System.IO.File.ReadAllText(fileName);
                    System.Text.StringBuilder sb = new StringBuilder();
                    List<Country> countries = new List<Country>();
                    Country country = null;
                    string line;
                    string[] arrLine;
                    System.IO.StreamReader file = new System.IO.StreamReader(fileName);
                    while ((line = file.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            if (line.Contains("rip="))
                            {
                                arrLine = line.Split(new string[] { "mail" }, StringSplitOptions.None);
                                if (arrLine.Length > 1)
                                {
                                    sb.Append(arrLine[0] + ";");
                                    if (line.Contains("pop3-login:"))
                                    {
                                        sb.Append("pop3;");
                                    }
                                    else if (line.Contains("imap-login:"))
                                    {
                                        sb.Append("imap;");
                                    }
                                    else
                                    {
                                        sb.Append(";");
                                    }
                                    arrLine = line.Split(new string[] { "user=<" }, StringSplitOptions.None);
                                    if (arrLine.Length > 1)
                                    {
                                        arrLine = arrLine[1].Split(new string[] { ">," }, StringSplitOptions.None);
                                        if (arrLine.Length > 1)
                                        {
                                            sb.Append(arrLine[0] + ";");
                                        }
                                        else
                                        {
                                            sb.Append(";");
                                        }
                                    }
                                    else
                                    {
                                        sb.Append(";");
                                    }

                                    arrLine = line.Split(new string[] { "rip=" }, StringSplitOptions.None);
                                    if (arrLine.Length > 1)
                                    {
                                        arrLine = arrLine[1].Split(new string[] { "," }, StringSplitOptions.None);
                                        if (arrLine.Length > 1)
                                        {
                                            sb.Append(arrLine[0] + ";");

                                            country = countries.FirstOrDefault(a => a.IP == arrLine[0]);
                                            if (country != null && !string.IsNullOrWhiteSpace(country.IP))
                                            {
                                                sb.Append(country.Name + ";");
                                            }
                                            else
                                            {
                                                sb.Append(GetCountryByIP(arrLine[0],ref countries) + ";");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append(";;");
                                        }
                                    }
                                    else
                                    {
                                        sb.Append(";;");
                                    }
                                    sb.Append(System.Environment.NewLine);
                                }
                            }
                        }
                    }

                    file.Close();

                    DialogResult dialogResult = saveFileDialog1.ShowDialog();
                    string saveFileName=Application.StartupPath + @"\data.csv";
                    if (dialogResult == DialogResult.OK)
                    {
                        saveFileName = saveFileDialog1.FileName;
                    }

                    System.IO.File.WriteAllText(saveFileName, sb.ToString());
                    MessageBox.Show("File Save at " + saveFileName);
                    fileName = string.Empty;
                    textBox1.Text = string.Empty;

                }
                else
                {
                    MessageBox.Show("File Not Found");
                }
            }
            else
            {
                MessageBox.Show("Select File");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Message:" + ex.Message + " InnerException:" + ex.InnerException);
        }
    }