excel - 将文本文件转换为 Excel

标签 excel text-files vba

我需要将文本文件转换为 Excel 文件。我找到了与此相关的文章,但我的要求略有不同,所以我没有任何想法。

我有包含这种格式的行的文本文件

Jun 13 07:35:08 mail dovecot: pop3-login: Login: user=<veena,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfe1bfaebbaaa38fbbaabcbbe1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>>, method=PLAIN, rip=102.201.122.131, lip=103.123.113.83, mpid=33178, session=<Wfdfdfcxvc>

我想创建具有四列的 Excel 文件:-

  • 第一列包含上面行的“Jun 13 07:35:08”
  • 第二列包含上面行的“pop3”
  • 第三列包括“veena, [email protected]
  • 第四列包括“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);
        }
    }

关于excel - 将文本文件转换为 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963971/

相关文章:

vba - Excel VBA - 对象引用未设置为对象的实例

excel - 通过excel vba保存.xlsx文件,无需打开保存的文件

VBA 将 msoThemeColor 存储在变量中

python - 使用 excelwriter 写入 Excel 文件没有给出文件

excel - 从目录和子目录中获取过滤后的文件名列表到数组中

java - 将 javafx 文本区域写入带有换行符的文本文件

java - 使用计数作为字符串的索引如何生成随机单词?

android - .txt 文件的 MIME 类型?

excel - 使用每个文件打开 Excel 2010 的新实例

excel - 在 VBA 中,使用 With、End With 是否总是更好?