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。
我在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");