Jack
Jack

Reputation: 1

How to set custom location for rdlc report on IIS?

Although I can create rdlc report in debug mode, I encounter an error "Access to the path 'C:\xxx.xlsx' is denied." After looking on the web for workaround, I see that lots of the solutions suggest to give permission to the C drive for IIS user. However, it does not seem to be wisely to give permission to entire drive for just rendering a report. So, how to change this render location i.e. C:\inetpub\MyApplication? On the other hand I think there is no settings needed on reporting side i.e. ReportViewer.ProcessingMode = ProcessingMode.Local; or changing rdlc properties "Build Action", "Copy Output Directory"?

Note: I do not want the reports to be rendered on client's machine as some of them has no right to write any location under C:\ and I think generating reports on IIS location is much better. Is not it?

So, what is the best solution in this situation?


Update: How can I modify this method so that it just read the stream as excel withou writing it?

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");
}       

Upvotes: 2

Views: 1796

Answers (1)

Steve Greene
Steve Greene

Reputation: 12304

Here is how we render Excel files from an rdlc without saving it to a server folder. Just call the action and it will download to the user's browser.

    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);
    }

Upvotes: 2

Related Questions