虽然我可以在调试模式下创建rdlc报告,但我遇到错误“拒绝访问路径'C:\ xxx.xlsx'。”在网上查找解决方法后,我看到许多解决方案建议为IIS用户授予C驱动器权限。但是,仅为呈现报告提供整个驱动器的权限似乎并不明智。那么,如何更改此渲染位置,即C:\ inetpub \ MyApplication?另一方面,我认为报告方面没有需要设置,即ReportViewer.ProcessingMode = ProcessingMode.Local;
或更改rdlc属性“构建操作”,“复制输出目录”?
注意:我不希望报告在客户端的计算机上呈现,因为他们中的一些人无权在C:\下编写任何位置,我认为生成IIS位置的报告要好得多。不是吗?
那么,在这种情况下最好的解决方案是什么?
更新:如何修改此方法,以便只需将其作为excel读取即可?
public static void StreamToProcess(Stream readStream)
{
var writeStream = new FileStream(String.Format("{0}\\{1}.{2}", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "MyFile", "xlsx"), FileMode.Create, FileAccess.Write);
const int length = 16384;
var buffer = new Byte[length];
var bytesRead = readStream.Read(buffer, 0, length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, length);
}
readStream.Close();
writeStream.Close();
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + "\\" + "file" + "." + "xlsx");
}
答案 0 :(得分:2)
以下是我们如何从rdlc渲染Excel文件而不将其保存到服务器文件夹。只需调用该操作,它就会下载到用户的浏览器中。
public FileStreamResult ExcelReport(int type)
{
var body = _db.MyObjects.Where(x => x.Type == type);
ReportDataSource rdsBody = new ReportDataSource("MyReport", body);
ReportViewer viewer = new ReportViewer
{
ProcessingMode = ProcessingMode.Local
};
viewer.LocalReport.ReportPath = Server.MapPath(@"~\bin\MyReport.rdlc");
viewer.LocalReport.DataSources.Clear();
viewer.LocalReport.DataSources.Add(rdsBody);
viewer.LocalReport.EnableHyperlinks = true;
string filename = string.Format("MyReport_{0}.xls", type);
byte[] bytes = viewer.LocalReport.Render("Excel");
var stream = new MemoryStream(bytes);
return File(stream, "application/ms-excel", filename);
}