【转】asp.net不用CrystalReportViewer 直接打印

给页面放一个DropdownList,在PageLoad事件里添加以下代码:
 For Each iprt As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
            DropDownList1.Items.Add(iprt)
   Next

放一个Button,点击事件为:
Dim reportDoc As ReportDocument = New ReportDocument()

       reportDoc.Load(Server.MapPath("CrystalReport1.rpt"))
        reportDoc.PrintOptions.PrinterName = DropDownList1.SelectedItem.Text
        reportDoc.PrintToPrinter(1, False, 0, 0)

在VS2005上测试通过。

今天发现一个好的办法,给画面拖一个CrystalReportSource空间CrystalReportSource1,拖一个sqldatasource,在sqldatasource中进行数据筛选,然后将它与CrystalReportSource绑定,执行下边几句进行打印

CrystalReportSource1.DataBind(); '取出筛选的数据

CrystalReportSource1.ReportDocument.PrinterName = DropDownList1.SelectedItem.Text

CrystalReportSource1.ReportDocument.PrintToPrinter(1, False, 0, 0)



aspx 代码:

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
            PrintMode="ActiveX" Width="600px" Height="400px" />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
        </CR:CrystalReportSource>

.cs代码:

string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Clear();
        foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)   //只能列出本地打印机
            this.DropDownList1.Items.Add(iprt);
         this.DropDownList1.Items.Add(new ListItem("\\\\chen\\HP Deskjet F300 series", "0"));

        //设置数据源
        sql = "select [money],payMent,addTime from tableOpen where year(addTime) = 2007 and month(addTime) = 07 and day(addTime) = 14";
        //sql = "select [money],payMent,addTime from tableOpen";

        DataSet ds = HensomeWeb.DB.DbHelperSQL.Query(sql);

        this.CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport8.rpt"));
        CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["ds"]);
        CrystalReportSource1.DataBind();
      

       //绑定到 CrystalReportViewer
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        this.CrystalReportViewer1.DataBind();

    }


   //button事件,选择打印机并打印报表
    protected void Button1_Click(object sender, EventArgs e)
    {
        CrystalReportSource1.ReportDocument.PrintOptions.PrinterName = DropDownList1.SelectedItem.Text;
        CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 1, 1);

转载于:https://my.oschina.net/starmier/blog/83812

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值