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 Toolbar

我都用ReportViewer在網頁上替客戶產生報表,他的好處是可以輸出Excel,pdf.

又可以在網頁上即時預覽,雖然是ActiveX,但它是應付客戶的最佳方法

但客戶才沒那麼好搞定,今天客戶說他看不懂上面的”select export format…”

所以我就去研究中文化了,以下就是中文化的三種方法.

1. ReportViewer 2005版

MS已經有提供語言包,在你的server安裝就可以

2. ReportViewer 2008手動部署

MS還沒提供語言包,所以方法就是在安裝有VS2008的電腦裡,把語言資源copy到伺服器

到%WINDOWS%\assembly裡面找下面兩個東西

Microsoft.ReportViewer.WebForms.resources.dll

Microsoft.ReportViewer.Common.resources.dll

在console用這個命令把他copy出來

xcopy %WINDOWS%\assembly\GAC_msil x:\GAC_MSIL /E /c

這樣檔案就會copy到x:\GAC_MSIL\

然後把兩個資源檔copy到你網頁專案的bin\裡面就可以

3. ReportViewer 2008寫程式修改
以下程式碼在MSDN有

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Reporting.WebForms;
namespace WebReport
{
public class ReportViewerMessagesZhtw : IReportViewerMessages
{
IReportViewerMessages Members#region IReportViewerMessages Members
public string BackButtonToolTip
{
get { return ("後退"); }
}
public string ChangeCredentialsText
{
get {return ("更改"); }
}
public string ChangeCredentialsToolTip
{
get { return ("ChangeCredentialsToolTip."); }
}
public string CurrentPageTextBoxToolTip
{
get { return ("當前頁"); }
}
public string DocumentMap
{
get { return ("文件地圖"); }
}
public string DocumentMapButtonToolTip
{
get { return ("文件地圖"); }
}
public string ExportButtonText
{
get { return ("匯出"); }
}
public string ExportButtonToolTip
{
get { return ("匯出"); }
}
public string ExportFormatsToolTip
{
get { return ("選擇匯出格式"); }
}
public string FalseValueText
{
get { return ("不正確的值"); }
}
public string FindButtonText
{
get { return ("尋找"); }
}
public string FindButtonToolTip
{
get { return ("尋找"); }
}
public string FindNextButtonText
{
get { return ("尋找下一筆"); }
}
public string FindNextButtonToolTip
{
get { return ("尋找下一筆"); }
}
public string FirstPageButtonToolTip
{
get { return ("第一頁"); }
}
public string InvalidPageNumber
{
get { return ("無效的頁數"); }
}
public string LastPageButtonToolTip
{
get { return ("最後一頁"); }
}
public string NextPageButtonToolTip
{
get { return ("下一頁"); }
}
public string NoMoreMatches
{
get { return ("沒有匹配"); }
}
public string NullCheckBoxText
{
get { return ("空值"); }
}
public string NullValueText
{
get { return ("空值"); }
}
public string PageOf
{
get { return ("/"); }
}
public string ParameterAreaButtonToolTip
{
get { return ("參數設定"); }
}
public string PasswordPrompt
{
get { return ("PasswordPrompt"); }
}
public string PreviousPageButtonToolTip
{
get { return ("上一頁"); }
}
public string PrintButtonToolTip
{
get { return ("列印"); }
}
public string ProgressText
{
get { return ("正在生成報表"); }
}
public string RefreshButtonToolTip
{
get { return ("刷新"); }
}
public string SearchTextBoxToolTip
{
get { return ("尋找"); }
}
public string SelectAValue
{
get { return ("SelectAValue"); }
}
public string SelectAll
{
get { return ("全選"); }
}
public string SelectFormat
{
get { return ("選擇格式"); }
}
public string TextNotFound
{
get { return ("未找到"); }
}
public string TodayIs
{
get { return ("TodayIs"); }
}
public string TrueValueText
{
get { return ("TrueValueText"); }
}
public string UserNamePrompt
{
get { return ("UserNamePrompt"); }
}
public string ViewReportButtonText
{
get { return ("查看報表"); }
}
public string ZoomControlToolTip
{
get { return ("縮放"); }
}
public string ZoomToPageWidth
{
get { return ("頁寬"); }
}
public string ZoomToWholePage
{
get { return ("整頁"); }
}
#endregion
}
}

然後要修改web.config的設定


因為我只有用到方法2就搞定了,方法3沒試過,有試過的人就說一下行不行吧

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.