使用EPPLUS获取文件excel后,将日期mm / dd / yyyy更改为dd / mm / yyyy - MVC

时间:2016-08-18 02:57:32

标签: jquery asp.net-mvc asp.net-mvc-3 http epplus

我的input text日期时间格式为:dd / mm / yyyy,

Action ExportExcel:

public ActionResult ExportExcel(Datetime date)
{
    var stream = CreateExcelFile();

    var buffer = stream as MemoryStream;

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    Response.AddHeader("Content-Disposition", "attachment; filename=ExcelDemo.xlsx");

    Response.BinaryWrite(buffer.ToArray());

    Response.Flush();
    Response.End();

    return RedirectToAction("Index");
}

在我向此action提交表单之前,我格式input text:mm / dd / yyyy。

当我获得文件excel时,我想重新格式化input text:dd / mm / yyyy。

2 个答案:

答案 0 :(得分:0)

你应该使用DateTime格式。例如,如果你想要这种格式:“18/8/2016”在你的ActionResult中做这样的事情:

string format = "d/M/yy";
var exceltime = time.ToString(format);

您可以在此处找到更多信息: Custom Date and Time Format Strings

答案 1 :(得分:0)

所以你知道你的日期总是dd / MM / yyyy

您可以做的是将输入类型更改为字符串,然后将其转换为日期时间。

public ActionResult ExportExcel(string date)
{
    DateTime original_date = DateTime.Now;
    if(!string.IsNullOrEmpty(date))
    {
        originial_date = DateTime.ParseExact(date, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    }
    .
    .
}