虽然我可以在 Debug模式下创建 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");
}
最佳答案
以下是我们如何从 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);
}
关于c# - 如何在 IIS 上设置 rdlc 报告的自定义位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34239420/