C# 浏览器下载指定地址 Excel 文件 - 代码示例
以下是使用 C# 实现让浏览器下载指定地址的 Excel 文件的示例代码:
string fileUrl = 'http://example.com/example.xlsx';
string fileName = 'example.xlsx';
WebClient client = new WebClient();
byte[] fileBytes = client.DownloadData(fileUrl);
Response.Clear();
Response.ContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
Response.AddHeader('Content-Disposition', 'attachment; filename=' + fileName + '');
Response.BinaryWrite(fileBytes);
Response.End();
首先,我们需要指定要下载的 Excel 文件的地址和文件名。然后,使用 WebClient 类下载文件的字节数据。接着,设置响应的 Content-Type 为 Excel 文件类型,并添加 Content-Disposition 标头,以指示浏览器将文件下载到本地。最后,将文件字节数据写入响应输出流中,完成下载。
原文地址: https://www.cveoy.top/t/topic/oYAk 著作权归作者所有。请勿转载和采集!