web下vs2008+crystal report 不预览直接打印

下载了一个PrintControl.cab(vs2008用的)

然后在webconfig中设置: 

代码
 1  <configSections>
 2     <sectionGroup name="businessObjects">
 3       <sectionGroup name="crystalReports">
 4         <section name="printControl" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" /> </sectionGroup>
 5     </sectionGroup>
 6   </configSections>
 7   <businessObjects>
 8     <crystalReports>
 9       <printControl>
10         <add key="url" value="WebProducer/PrintControl.cab" />
11       </printControl>
12     </crystalReports>
13   </businessObjects>

 

在需要打印的页面上添加以下代码:

1 <object id="CrystalPrintControl" classid="CLSID:83A3D1E4-ADC6-434D-9B61-B8CBA6183441"
2     codebase="PrintControl.cab" version="10,5,1,2285" height="0px"
3     viewastext>
4 </object>

 

设置height="0px"主要是让 object不显示在页面上;

添加水晶报表crystalReportViewr

 

代码
1   <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
2                 EnableDrillDown="false" DisplayGroupTree="false" style="font-size: 11px" 
3                 HasCrystalLogo="False" BestFitPage="False" HasExportButton="False" 
4                 PrintMode="ActiveX" Height="40px" Width="0px" />

 

 

将width设置为“0”让客户无法看见crystalReportViewr,但实际上crystalReportViewr还是存在于页面上的。本来想把height设置为“0px”但总提示出错,结果设置为这个数运行后同样看不到crystalReportViewr.就不理它了。

在前台页面上加上脚本调用水晶报表自带的打印按钮

 

1   
2     <script type="text/javascript">
3 function printIt() {
4 document.getElementsByName("CrystalReportViewer1$ctl02$ctl01").item(0).click();
5 return false;
6  }
7 </script>

 

然后添加自定义打印按钮:

<input id="bt1" type="button" onclick="printIt();"/>

 

后台方面绑定水晶报表:

 

代码
  protected void Page_Load(object sender, EventArgs e)
    {
     
        
//if (!IsPostBack)
        //{
        //}
//
本来是想写在IsPostBack里面的,但这样写就提示您请求的报表需要更多信息... 所以只好写在外面 ,原因是打印的时候还要重新绑定水晶报表。
   BindCRV();

    }
void BindCRV
{
  CrystalDecisions.Web.CrystalReportSource crs 
= new CrystalDecisions.Web.CrystalReportSource();
        PrintDocument prtdoc 
= new PrintDocument();
        string strDefaultPrinter 
= prtdoc.PrinterSettings.PrinterName;
        
try
        {
            crs.ReportDocument.Load(reportpath);
            crs.ReportDocument.SetDataSource(ds.Tables[
0]);
            crs.DataBind();
   CrystalReportViewer1.ReportSource 
= crs;
            CrystalReportViewer1.DataBind();
}
     
catch (Exception ex)
        {
            
//UnCommon.ShoMessage(ex.Message, false, this);
            //  WSBG.UnCommon.ExecuteScript("alert('" + ex.Message + "')", this);

        }
        
finally
        {
            crs.Dispose();
            GC.Collect();
        }
}

 

终于大功公成,不预览直接打印

posted @ 2009-12-03 09:27  hambywu  阅读(1409)  评论(0编辑  收藏  举报