• 打印出datagrideview里面的内容,用到少量GDI+的知识


      
      1 using System;
    2 using System.Data;
    3 using System.Drawing;
    4 using System.Drawing.Printing;
    5 using System.Windows.Forms;
    6
    7
    8 namespace InvoiceSystem{
    9
    10 public partial class Printer : Form
    11 {
    12 private DataGridView invoiceGrid;
    13 private int rowCount = 0; //datagridview行数
    14 private int colCount = 0; //datagridview列数
    15 private int x = 0; //一个datagridview单元格宽度
    16 private int y = 0; //当前行行间距
    17 int i = 0; //判断行数for循环的起始值
    18 private int rowGap = 60; //行间距
    19 private int leftMargin = 50; //正文到左面的左边距
    20 private Font font = new Font("Arial", 10); //正文字体
    21 private Font headingFont = new Font("Arial", 11, FontStyle.Underline); //标题字体
    22 private Font captionFont = new Font("Arial", 10, FontStyle.Bold);
    23 private Brush brush = new SolidBrush(Color.Black);
    24 private string cellValue = string.Empty; //保存datagridview一个单元格里的内容
    25
    26
    27 public Printer(DataGridView invoiceGrid)
    28 {
    29 InitializeComponent();
    30 this.invoiceGrid = invoiceGrid; //从外部传入一个datagridview引用
    31
    32 }
    33
    34
    35 /// <summary>
    36 /// 打印按钮
    37 /// </summary>
    38 /// <param name="sender"></param>
    39 /// <param name="e"></param>
    40 private void button1_Click(object sender, EventArgs e)
    41 {
    42
    43 printDocument1.DocumentName = "发票列表"; //打印的文档文件名
    44 PaperSize ps=new PaperSize("16开",724,1024); //自定义纸张大小
    45 //ps.RawKind = 9; //设置为A4
    46 printDocument1.DefaultPageSettings.PaperSize = ps;
    47 printDocument1.DefaultPageSettings.Landscape = true; //使用横向打印
    48
    49 printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); //设置函数printDocument1_PrintPage为打印事件处理函数
    50 printDialog1.Document = printDocument1;
    51 printPreviewDialog1.Document = printDocument1;
    52
    53 if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && printPreviewDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    54 {
    55
    56 printDocument1.Print();
    57
    58
    59 }
    60 }
    61
    62
    63 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    64 {
    65
    66 rowCount = invoiceGrid.Rows.Count - 1;
    67 colCount = invoiceGrid.ColumnCount;
    68
    69 //打印标题
    70 y += rowGap;
    71 x = leftMargin;
    72 for (int j = 0; j < colCount; j++)
    73 {
    74 if (invoiceGrid.Columns[j].Width>0)
    75 {
    76 cellValue = invoiceGrid.Columns[j].HeaderText;
    77 e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), x, y, invoiceGrid.Columns[j].Width, rowGap); //将单元格的背景设置为灰色
    78 e.Graphics.DrawRectangle(Pens.Black, x, y, invoiceGrid.Columns[j].Width, rowGap); //画出一个单元格
    79 e.Graphics.DrawString(cellValue, headingFont, brush, x, y); //将datagridview标题里的单元格里的内容填入刚才画出的单元格
    80 x += invoiceGrid.Columns[j].Width; //宽度向后移一个datagridview单元格宽度的单位
    81 }
    82 }
    83
    84 //打印所有行
    85 for (; i < rowCount; i++)
    86 {
    87 y += rowGap;
    88 x = leftMargin;
    89
    90 for (int j = 0; j < colCount; j++)
    91 {
    92 if (invoiceGrid.Columns[j].Width>0)
    93 {
    94 cellValue = invoiceGrid.Rows[i].Cells[j].Value.ToString();
    95 e.Graphics.DrawRectangle(Pens.Black, x, y, invoiceGrid.Columns[j].Width, rowGap);
    96 e.Graphics.DrawString(cellValue, font, brush, x, y);
    97 x += invoiceGrid.Columns[j].Width;
    98 }
    99 }
    100
    101 if (y > e.PageBounds.Height) //e.PageBounds.Height表示页边距的高度
    102 {
    103 //允许多页打印
    104 y = 0;
    105 e.HasMorePages = true; //如果还有没有打印的就再执行一次print()函数把余下的打印出来
    106 i++;
    107 return;
    108
    109 }
    110
    111 }
    112 y += rowGap;
    113
    114
    115 //底部填充空格
    116 for (int j = 0; j < colCount; j++)
    117 {
    118 e.Graphics.DrawString(" ", font, brush, x, y);
    119 }
    120 i = 0;
    121 e.HasMorePages = false;
    122 }
    123 }
    124 }

      效果就是贴出的图片的样子

  • 相关阅读:
    hdu
    如何在maven中的项目使用tomcat插件
    Intellij IDEA 像eclipse那样给maven添加依赖,且Intellij idea里在pom.xml里添加Maven依赖,本地仓库下拉列表显示包很少的血的经验
    DataTables warning: table id=costitemProject
    Navicat Premium Mac 12 破解
    mac显示隐藏的文件
    tomcat7下载地址
    mac同时安装jdk7和jdk8
    屏蔽datatable错误提示
    mac上配置java jdk环境
  • 原文地址:https://www.cnblogs.com/lyhabc/p/2112755.html
Copyright © 2020-2023  润新知