我需要把.xlsx下载

时间:2014-01-16 13:08:25

标签: c# asp.net xlsx

我正在尝试下载.xlsx文件。我正在使用library.cs。当我使用MVC 3时,我会用它来做:

HttpContext.Response.Clear();
HttpContext.Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
return File(pck.GetAsByteArray(), "application/octet-stream");

但是现在,有了.cs,我不知道我是怎么做的。

1 个答案:

答案 0 :(得分:3)

试试这个:

public void DownloadExcel(byte[] buffer, string nameFile) {

    // Verify invalid chars on nameArchive parameter:
    var preparedName = new String(
       nameFile.Where(c => !Path.GetInvalidFileNameChars().Contains(c)).ToArray()
    );

    if (String.IsNullOrEmpty(preparedName))
       preparedName = "DefaultName";

    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.ContentType = 
       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    response.AddHeader("Content-Disposition", 
       String.Format("attachment;filename={0}.xlsx", preparedName));
    response.AddHeader("Content-Length", buffer.Length.ToString());
    response.BinaryWrite(buffer);
    response.End();
}

我认为您的案例中的问题是Mine Type,尝试使用与上述方法相同的方法。