PageMargins margins; // Get the PageMargins structure and set the 
// margins for the report.
ReportDocument Report = new ReportDocument();
string path = Server.MapPath("企业登记基本情况_分年统计周.rpt");
Report.Load(path);

margins = Report.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 350;
// Apply the page margins.
Report.PrintOptions.ApplyPageMargins(margins); // Select the printer.
string printerName = "\\\\局域网机器名\\打印机名(例HP)";
Report.PrintOptions.PrinterName = printerName; // Print the report. Set the startPageN and endPageN
// parameters to 0 to print all pages.
Report.SetDataSource(ds);
Report.PrintToPrinter(1, false,0,1);

解决方案 »

  1.   

    我加一个本地打印机,却可以执行成功..不会出错..楼上那位老兄.我改成了.
     Dim oRpt As New crpt3()
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            BindReport("Select * from Mytree")
        End Sub
        Sub BindReport(ByVal SQL As String)        Dim myConnection As New SqlClient.SqlConnection()        myConnection.ConnectionString = "Persist Security Info=False;User ID=sa;pwd=;Initial Catalog=drkhorse;Data Source=CRM\CRM"        Dim MyCommand As New SqlClient.SqlCommand()        MyCommand.Connection = myConnection        MyCommand.CommandText = SQL        MyCommand.CommandType = CommandType.Text        Dim MyDA As New SqlClient.SqlDataAdapter()        MyDA.SelectCommand = MyCommand
            '已建好的DATASET的文件
            Dim myDS As New Dataset2()
            'This is our DataSet created at Design Time      
            MyDA.Fill(myDS, "Mytree")
            'You have to use the same name as that of your Dataset that you created during design time
            '  Dim oRpt As New crpt3()
            ' This is the Crystal Report file created at Design Time
            oRpt.SetDataSource(myDS)
            ' Set the SetDataSource property of the Report to the Dataset
            CrystalReportViewer1.ReportSource = oRpt        ' Set the Crystal Report Viewer's property to the oRpt Report object that we created
        End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BindReport("select * from Mytree where name like '9%'")
        End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            BindReport("select * from Mytree")
        End Sub    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            '  oRpt.PrintOptions.PrinterName = "Canon S400在GW-SERVER上"
            oRpt.PrintOptions.PrinterName = "\\\\GW-SERVER\\Canon S400"
            ' oRpt.PrintOptions.PrinterName = "Canon S400"
            oRpt.PrintToPrinter(1, True, 1, 2)
        End Sub
    可还是不行啊.提示错误.
    文件 C:\WIN2000\TEMP\temp_a977e0de-c830-4691-824c-ba502935dbd0.rpt 内出错:指定的打印机无效。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: CrystalDecisions.CrystalReports.Engine.InvalidArgumentException: 文件 C:\WIN2000\TEMP\temp_a977e0de-c830-4691-824c-ba502935dbd0.rpt 内出错:指定的打印机无效。源错误: 
    行 71:     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    行 72:         '  oRpt.PrintOptions.PrinterName = "Canon S400在GW-SERVER上"
    行 73:         oRpt.PrintOptions.PrinterName = "\\\\GW-SERVER\\Canon S400"
    行 74:         ' oRpt.PrintOptions.PrinterName = "Canon S400"
    行 75:         oRpt.PrintToPrinter(1, True, 1, 2)
     
    打印机明明有啊.