如何解决将Excel文件保存到用户桌面时的问题?

问题描述 投票:0回答:1

尝试从c#asp.net Web应用程序将excel文件保存到用户桌面。这在测试时在本地机器上运行良好,但在远程服务器上没有。有人可以帮我解决这个问题吗?谢谢!

码:

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
FileInfo fi = new FileInfo(filePath + @"\spreadsheet.xlsx");
excelPackage.SaveAs(fi);

错误:enter image description here

c# asp.net file-permissions
1个回答
1
投票

看起来你可能正在使用EPPplus(excelPackage.SaveAs(fi);)。因此,如果您使用asp.net作为标记表明您可以将Excel文件发送到浏览器,则用户将获得“保存文件”对话框。

//convert the excel package to a byte array
byte[] bin = excelPackage.GetAsByteArray();

//clear the buffer stream
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;

//set the correct contenttype
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

//set the correct length of the data being send
Response.AddHeader("content-length", bin.Length.ToString());

//set the filename for the excel package
Response.AddHeader("content-disposition", "attachment; filename=\"ExcelDemo.xlsx\"");

//send the byte array to the browser
Response.OutputStream.Write(bin, 0, bin.Length);

//cleanup
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
© www.soinside.com 2019 - 2024. All rights reserved.