好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Winform中用PrintDocument控件打印报表实例

Winform中用PrintDocument控件打印报表实例

using System;

using System.Drawing;

using System.Drawing.Printing;

using System.Windows.Forms;

public class PrintReportForm : Form

{

    private Button btnPrint = new Button();

    private Button btnPreview = new Button();

    private PrintDocument printDoc = new PrintDocument();

    private PrintPreviewDialog previewDlg = new PrintPreviewDialog();

    public PrintReportForm()

    {

        // 初始化UI

        btnPrint.Text = "直接打印";

        btnPrint.Location = new Point(20, 20);

        btnPrint.Click += BtnPrint_Click;

        btnPreview.Text = "打印预览";

        btnPreview.Location = new Point(120, 20);

        btnPreview.Click += BtnPreview_Click;

        this.Controls.Add(btnPrint);

        this.Controls.Add(btnPreview);

        // 设置打印文档

        printDoc.PrintPage += PrintDoc_PrintPage;

        printDoc.DocumentName = "销售报表";

    }

    private void BtnPrint_Click(object sender, EventArgs e)

    {

        PrintDialog printDlg = new PrintDialog();

        printDlg.Document = printDoc;

        

        if (printDlg.ShowDialog() == DialogResult.OK)

        {

            printDoc.Print();

        }

    }

    private void BtnPreview_Click(object sender, EventArgs e)

    {

        previewDlg.Document = printDoc;

        previewDlg.ShowDialog();

    }

    private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)

    {

        // 设置字体和画刷

        Font titleFont = new Font("宋体", 18, FontStyle.Bold);

        Font headerFont = new Font("宋体", 12, FontStyle.Bold);

        Font contentFont = new Font("宋体", 10);

        SolidBrush brush = new SolidBrush(Color.Black);

        // 打印标题

        string title = "2025年6月销售报表";

        e.Graphics.DrawString(title, titleFont, brush, 

            new PointF((e.PageBounds.Width - e.Graphics.MeasureString(title, titleFont).Width) / 2, 50));

        // 打印表头

        float yPos = 100;

        e.Graphics.DrawString("产品名称", headerFont, brush, 50, yPos);

        e.Graphics.DrawString("数量", headerFont, brush, 250, yPos);

        e.Graphics.DrawString("单价", headerFont, brush, 350, yPos);

        e.Graphics.DrawString("总价", headerFont, brush, 450, yPos);

        // 打印表格内容(示例数据)

        string[,] data = {

            {"产品A", "120", "15.5", "1860"},

            {"产品B", "85", "22.3", "1895.5"},

            {"产品C", "210", "8.9", "1869"}

        };

        yPos += 30;

        for (int i = 0; i < 3; i++)

        {

            e.Graphics.DrawString(data[i,0], contentFont, brush, 50, yPos);

            e.Graphics.DrawString(data[i,1], contentFont, brush, 250, yPos);

            e.Graphics.DrawString(data[i,2], contentFont, brush, 350, yPos);

            e.Graphics.DrawString(data[i,3], contentFont, brush, 450, yPos);

            yPos += 20;

        }

        // 打印页脚

        e.Graphics.DrawString($"打印时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}", 

            contentFont, brush, 50, yPos + 30);

    }

}

// 程序入口

static class Program

{

    [STAThread]

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new PrintReportForm());

    }

}


查看更多关于Winform中用PrintDocument控件打印报表实例的详细内容...

  阅读:113次