【问题标题】:How to save textfile data in column format in ASP.NET MVC如何在 ASP.NET MVC 中以列格式保存文本文件数据
【发布时间】:2019-12-10 05:28:30
【问题描述】:

我目前正在从注册表单中获取输入并将输入详细信息下载为文本文件。下载的文本文件结果是:JimJone03/09/1998 00:00:00xxx@gmail.com 230436315

我希望结果以列格式显示。

例如:

  • 吉姆·琼斯

  • 03/09/98

  • xxx@gmail.com

  • +23057xxx556

这是我尝试过的:

public ActionResult Create(Information information)
{
    var byteArray = Encoding.ASCII.GetBytes(information.FirstName + "" 
                    + information.Surname + "" + information.DOB + "" 
                    + information.Email + " " + information.Tel);

    var stream = new MemoryStream(byteArray);

    return File(stream, "text/plain", "your_file_name.txt");
}

【问题讨论】:

标签: asp.net asp.net-mvc model-view-controller


【解决方案1】:

您可以尝试使用正则表达式来匹配它们:

var input = "Jim Jone 03/09/98 xxx@gmail.com +23057897765";

var name = new Regex("^[a-zA-Z\\s]+").Match(input);

var birthday = new Regex("\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}").Match(input);

var email = new Regex(@"([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)").Match(input);

var tel = new Regex("\\+\\d+").Match(input);

Console.WriteLine("Input: " + "\"" + input + "\"");
Console.WriteLine("Name: " + "\"" + name.Value.Substring(0, name.Value.Length - 1) + "\"");
Console.WriteLine("Birthday: " + "\"" + birthday.Value + "\"");
Console.WriteLine("Email: " + "\"" + email.Value + "\"");
Console.WriteLine("Tel: " + "\"" + tel.Value + "\"");

输出:

然后,您可以将它们逐行写入文本文件。

【讨论】:

  • 输入不固定。输入来自文本框
  • @bhav 我只是做一个测试。
【解决方案2】:

你为什么不直接使用Environment.NewLine

var byteArray = Encoding.ASCII.GetBytes($"{information.FirstName}{Environment.NewLine}{information.Surname}{Environment.NewLine}{information.DOB}{Environment.NewLine}{information.Email}{Environment.NewLine}{information.Tel}");

这应该在文件中的每个值之后添加一个“新行”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2011-05-01
    相关资源
    最近更新 更多