给页面放一个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);