将文本文件转换为Excel

时间:2016-06-22 09:31:32

标签: excel excel-vba 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>

我想创建包含四列的Excel文件: -

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

Excel中不需要所有其他数据。我怎样才能做到这一点?我知道这不是我想要的。我应该先写一些关于我先尝试过的代码,但实际上我并不知道。

2 个答案:

答案 0 :(得分:4)

 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);
        }
    }

答案 1 :(得分:1)

如果你要接受VBA-在你看过一些教程之后 - 你需要这种方法。
1。通过EOF method.获取文本文件
2。使用REGEX的每个所需条件的UDF将是我的方法 - 提示:您可以检查您的正则表达式逻辑here - 。
以下是提取电子邮件的示例:

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

enter image description here

enter image description here