无法在ASP.Net中生成excel文件 [英] An excel File can not be generated in ASP.Net
问题描述
Hello Friends ...
我在ASP.NET中使用Microsoft.Office.Interop.Excel创建一个excel文件。在开发时,正在生成和下载正确的Excel文件。但是在IIS上托管项目后,我收到以下错误
访问路径'C:\ Mori-2014 \ Report \ DailyReport .xls'被拒绝。
C:\ Mori-2014 \ Report \DailyReport.xls是excel文件生成的路径。从那里数据被转换成二进制格式并生成可下载的文件。
Hello Friends ...
i m creating an excel file using Microsoft.Office.Interop.Excel in ASP.NET. While at Developing time, An Excel File was generating and downloading properly. But After hosting the project on IIS, I am getting following error
Access to the path 'C:\Mori-2014\Report\DailyReport.xls' is denied.
C:\Mori-2014\Report\DailyReport.xls is path where the excel file is generating. and from there data is converted into binary format and generating downloadable file.
string FilePath="C:\Mori-2014\Report\DailyReport.xls";
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
Response.Buffer = true;
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.CacheControl = "public";
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Description", "Excel File Download");
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
请帮帮我。
Please Help me.
推荐答案
First of如果您正在创建/下载任何文件,请不要使用直接C:驱动器路径,它将始终在IIS中托管后拒绝访问。
所以更好选项是使用:
First of all if you are creating/downloading any file, don't use direct C: drive path it would always give you Access denied after hosting in IIS.
So the better option is use :
string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
驱动器上的路径类似于C:\ Users \ user_name \ AppData \ Rooaming。
您可以创建文件夹根据您的项目名称进行此路径和访问。
存储/下载/在此位置操作您的文件(Excel文件)。
Path on drive would be like "C:\Users\user_name\AppData\Roaming".
You can create you folder in this path and access as per you projects name.
Store/Download/Manipulate your File(Excel file) in this location which is user specific.
这篇关于无法在ASP.Net中生成excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!