Export using ReportViewer without viewing
ReportViewer is a great control. You can make drill down report and preview the report in the webpage.
Export as Excel or Pdf file easily, etc. Great features.
But, sometimes the user may not want to preview the report, the just want to get the file.
You can probably do that with Crystal report, but ReportViewer can just do the same, there is no point to shift.
Here is how to export as a byte stream. Use the LocalReport.Render()
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
//export to excel, use "PDF" is you would like pdf format
byte[] l_reportBytes = rv_waterLicenseReport.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.ClearContent();
//setup the file name
Response.AddHeader("Content-Disposition", "attachment; filename=" + "ApplicationForm" + tb_LicenseNo.Text.Trim() + ".xls");
Response.AddHeader("Accept-Header", l_reportBytes.Length.ToString());
//define mime type
Response.ContentType = "application/vnd.ms-excel";
Response.OutputStream.Write(l_reportBytes, 0, l_reportBytes.Length);
Response.Flush();
Response.Close();
Strangely, the one page report of mine will export out as two page with the second page blank.
I will look into that later. For how to create a report, see my other post.
ReportViewer DataSource and Expression Walkthrough
There is a problem to many report framework except JasperReport. The field expression syntax and their ability.
ReportViewer is not exception.
I have a post about some basic on using ReportViewer. But I recently found that a little part about binding data source is missing. That is the reason why this post is writen.
The post before is about object data source in code behind. There is one thing I forgot myself too. If you are wordering why your method do not appear in the data source window. A method that returns DataTable can not be a data source. You have to stick to Enumerable data. And in the code behind, you have to go to design view first, see what name of the data source should be named. You will need that to assign the data source to the ReportViewer.
除了JasperReport以外,很多報表工具都有同樣的問題.它們報表內的欄位所使用的語法雜亂及其功能限制.連ReportViewer也不例外.
我之前有一篇關於ReportViewer的基本應用. 但我最近發現中間有一小段關於連結資料源的地方漏掉了.這就是為甚麼我要寫這一篇的原因.
前一篇提到怎樣在code behind連結物件資料源.有一件事我自己也忘記了.如果你搞不懂為甚麼你的資料源取得方法沒有出現在資料源窗格.因為一個回傳DataTable的方法並不能作為一個資料源.你非得要用可列舉資料才行.至於code behind,你應該先去設計檢視,看一下資料源設定了甚麼名稱.在你指定資料源時要用到.
After data source are bound. You may want to do some extra checking in your expressions. Like show only positive number.
According to ReportViewer, it tells you that the expression is in VB syntax, I doublt it. You can’t even use If Then Else.
Here is the Link of MSDN on expression syntax.
If you want to check if a number is NaA, you can’t… The double in RDLC don’t have NaN property.
I just check it if it is larger than zero since I am removing negative number anyway.
在資料連結後.你可能會想給你的內容表達式加一點檢查.例如只顯示正數.根據ReportViewer的說明,它說所使用的是VB語法,我很懷疑.你連If Then Else都不能用.這是MSDN有關於表達式語法的文章.如果你想檢查一個數字是否NaN,沒辦法…因為RDLC裡的double並沒有NaN這個屬性.我只是檢查數值是否大於零,反正我都要忽略負數.
The syntax should be like
語法如下
Iif(ExpressionOfField > 0, ExpressionOfField, "")
There is also an issue when you are using ReportViewer on IIS 7. You may encounter JavaScript error and your image buttons turn into ‘X’. That is because the HttpHandler registration is moved from System.Web to System.WebServer.
如果你在IIS 7上運行你的ReportViewer. 你可能會遇到JavaScript錯誤並發現你的小圖示都變成一個’X’.那是因為HttpHandler的註冊從System.Web移到System.WebServer了.
You can solve it by.
解決方法如下.
Open Internet Information Services (IIS) Manager and select your Web application.
打開IIS管理員,選擇你的網路應用程式
Under IIS area, double-click on Handler Mappings icon.
在IIS區,點兩下處理常式對應.
At the Action pane on your right, click on Add Managed Handler.
在右邊的動作區,點新增事件處理常式
At the Add Managed Handler dialog, enter the following:
在新增管理對話框,輸入以下
Request path: Reserved.ReportViewerWebControl.axd
Type: Microsoft.Reporting.WebForms.HttpHandler
Name: Reserved-ReportViewerWebControl-axd
Click OK.
按確定
The solution was found here, thanks to Antonio Ooi. I hope he don’t mind me quoting his article.
解決方法來自這裡,感謝Antonio Ooi.希望他不介意我引述其文章.
ReportViewer basic technique for simple report
ReportViewer comes along with Visual Studio 2008 and it is very handy tools if you want to generate report in your website. At least much easier than OpenXml. The best thing is, it support pdf format and do not need to have micro excel in your server(you know what I am saying if you use COM to generate excel). Although its designer is not mature as Crystal report.
There are only three things you need to create a report. 1. datasource 2. passing parameters 3. hard work.
Datasource
Database query as datasource is straighy forward using the IDE, I am going to talk about customize datasource in code behind.
Any function that returns a IEnumerable collection could be use as a datasource(If I remeber it correctly). For example :
public static List<yourClass> GetYourClassAsDataSource(Guid param1, int param2)
You do all calculation you need in that function and then set it as datasource in your page code behind like this.
List<yourClass> l_list = ReportDataAgent.GetYourClassAsDataSource(id,integer);
reportviewer.LocalReport.DataSources.Clear();
ReportDataSource rds = new ReportDataSource("DataSourceName", l_list);
reportviewer.LocalReport.DataSources.Add(rds);
Parameter
ReportParameter yourParam = new ReportParameter("yourParam ", value);
reportviewer.LocalReport.SetParameters(new ReportParameter[] { yourParam });
This is it. I didn’t use any advance features such as drill down report, etc.
Remember to install ReportViewer to your server before it works.
P.S. I am stuck when installing ReportViewer Add-in to VWD express 2008 CHT version.