2011年12月14日 星期三

Crystal Report常用程式碼段


//列印份數屬性
 printerSettings.Copies = 2;
//橫印
printerSettings.DefaultPageSettings.Landscape = true;


//設定印表紙張為A5
System.Drawing.Printing.PrintDocument PDSize = new System.Drawing.Printing.PrintDocument();
  for (int i = 0; i < PDSize.PrinterSettings.PaperSizes.Count; i++)
  {
      if (PDSize.PrinterSettings.PaperSizes[i].PaperName=="A5")
      {
          System.Drawing.Printing.PaperSize size = PDSize.PrinterSettings.PaperSizes[i];
          //選紙張A5
          printerSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Paper", size.Width, size.Height);
      }
  }


//完整印表範例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace CrystalReportsApplication1
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        //Crystal Report for VS2010 Viewer 測試
        private void button1_Click(object sender, EventArgs e)
        {
            // ReportPreview1是CrystalReport1.rpt報表檔的預覽畫面
            ReportPreview1 rv1 = new ReportPreview1();
            rv1.Show();
        }

        //Crystal Report for VS2010 直接列印二份報表到預設印表機測試
        private void button2_Click(object sender, EventArgs e)
        {
            CrystalReport1 report1 = new CrystalReport1();
            report1.PrintToPrinter(2, false, 0, 0);
        }

        //Crystal Report for VS2010 選擇印表機後列印測試
        private void button3_Click(object sender, EventArgs e)
        {
            CrystalReport1 report1 = new CrystalReport1();
            PrinterSettings printerSettings = new PrinterSettings();
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrinterSettings = printerSettings;
            printDialog.AllowPrintToFile = false;
            printDialog.AllowSomePages = true;
            printDialog.UseEXDialog = true;
            //顯示Print Dialog讓User挑選印表機
            DialogResult result = printDialog.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                return;
            }
            //設定印表機後, 印出報表
            report1.PrintOptions.PrinterName = printerSettings.PrinterName;
            report1.PrintToPrinter(printerSettings.Copies, false, 0, 0);
        }
    }
}

---
Welly Lin

沒有留言:

張貼留言