gpt4 book ai didi

excel - 将文本文件转换为 Excel

转载 作者:行者123 更新时间:2023-12-03 00:53:25 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com