将文本文件转换为Excel

我需要将文本文件转换为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> 

我想创build有四列的Excel文件: –

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

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

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

如果你是VBA的 – 在你看了一些教程之后,你需要这个方法。
1.通过EOF方法获取文本文件。
2.使用REGEX的每个你想要的标准的UDF将是我的方法 – 提示:你可以在这里检查你的正则expression式逻辑。
以下是提取电子邮件的示例:

 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 = "[az]{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 

在这里输入图像说明

在这里输入图像说明