我正在使用 EPPlus 将内容写入 Excel,它会自动下载文件,但在显示警报消息时它不起作用

问题描述 投票:0回答:1
Response.ClearContent();      //Clear contents of current Response.
Response.BufferOutput = true; //Buffer output before sending.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExportData.xlsx");
Response.BinaryWrite(fileContents);
Response.Flush(); //Send buffered output to client.
Response.End();

Both script mangager and Java script code not working
string script = @"<script>
    Toastify({
       text: 'Excel file downloaded successfully!',
       duration: 3000,  // Display duration for the message (in milliseconds)
       className: 'info'  // Optional class name for the toast message
    }).showToast();
</script>";

ScriptManager.RegisterStartupScript(this, this.GetType(), "DownloadStatusScript", script, false);

需要在警告框或某些对话框中显示Excel下载后的警告消息。项目位于.net。

c# asp.net
1个回答
0
投票

我在web api中编写了一个从ExcellPackage下载excel的扩展方法。看看这是否适合你。

public static HttpResponseMessage ToExcelHttpResponseMessage(this ExcelPackage package, string FileName)
{
    var bytes = package.GetAsByteArray();

    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/octet-stream");

    response.Content = new ByteArrayContent(bytes);
    response.Content = response.Content;
    response.Content.Headers.ContentType = mediaType;
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = FileName;

    return response;
}

用途:

return youExcelPackage.ToExcelHttpResponseMessage("ExcelFileName.xlsx");
© www.soinside.com 2019 - 2024. All rights reserved.