尝试从c#asp.net Web应用程序将excel文件保存到用户桌面。测试时,这在本地计算机上工作正常,但在远程服务器上却没有。有人可以帮我解决此问题吗?谢谢!
代码:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
FileInfo fi = new FileInfo(filePath + @"\spreadsheet.xlsx");
excelPackage.SaveAs(fi);
答案 0 :(得分: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();