日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用C#實(shí)現(xiàn)操作Word打印-創(chuàng)新互聯(lián)

使用C#實(shí)現(xiàn)操作Word打印?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、盤龍網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、盤龍網(wǎng)絡(luò)營銷、盤龍企業(yè)策劃、盤龍品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供盤龍建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
class PrintClass
{
  #region 全局變量
  private DataGridView datagrid;//需要打印的數(shù)據(jù)來源

  private PageSetupDialog pagesetupdialog;
  private PrintPreviewDialog printpreviewdialog;
  int currentpageindex = 0;//當(dāng)前頁的編號(hào)
  int rowcount = 0;//數(shù)據(jù)的行數(shù)
  public Size PaperSize = new Size(827, 1169);//答應(yīng)的紙張大小
  public int headerheight = 30;//標(biāo)題高度
  Margins margins = new Margins(50, 60, 50, 80);
  public int celltopmargin = 6;//單元格頂邊距 
  public int pagerowcount = 7;//每頁行數(shù) 
  public int rowgap = 23;//行高 
  public int colgap = 5;//每列間隔 
  public Font headerfont = new Font("Arial", 9, FontStyle.Bold);//列名標(biāo)題字體
  public Brush brushHeaderFont = new SolidBrush(Color.Black);//列名字體畫刷
  public Font Cellfont = new Font("Arial", 9);//單元格字體
  public bool isautopagerowcount = true;//是否自動(dòng)計(jì)算行數(shù)
  public bool PageAspect = false;//打印的方向
  public static bool PageScape = false;//打印方向
  public string paperName = string.Empty;
  #endregion

  #region 打印信息的初始化
  /// 
  /// 打印信息的初始化
  /// 
  /// 打印數(shù)據(jù)
  /// 紙張大小
  /// 是否橫向打印
  public PrintClass(DataGridView datagrid, string paperName, bool lendscape)
  {
    this.datagrid = datagrid;//獲取打印數(shù)據(jù)
    this.paperName = paperName;
    PrintDocument printdocument = new PrintDocument();//實(shí)例化PrintDocument類
    printpreviewdialog = new PrintPreviewDialog();//實(shí)例化PrintPreviewDialog類
    printpreviewdialog.Document = printdocument;//獲取預(yù)覽文檔的信息
    printpreviewdialog.FormBorderStyle = FormBorderStyle.Fixed3D;//設(shè)置窗體的邊框樣式
    //橫向打印的設(shè)置
    if (!string.IsNullOrEmpty(paperName) )
    {
      if (lendscape == true)
      {
        printdocument.DefaultPageSettings.Landscape = lendscape;//橫向打印
      }
      else
      {
        printdocument.DefaultPageSettings.Landscape = lendscape;//縱向打印
      }
    }
    pagesetupdialog = new PageSetupDialog();//實(shí)例化PageSetupDialog類
    pagesetupdialog.Document = printdocument;//獲取當(dāng)前頁的設(shè)置
    printdocument.PrintPage += new PrintPageEventHandler(this.printdocument_printpage);//事件的重載
  }
  #endregion
  
  #region 頁的打印事件
  /// 
  /// 頁的打印事件(主要用于繪制打印報(bào)表)
  /// 
  private void printdocument_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
    if (this.isautopagerowcount)//自動(dòng)計(jì)算頁的行數(shù)
    {
      double countHeight = e.PageBounds.Height - this.margins.Top - this.headerfont.Height - this.headerheight - this.margins.Bottom;
      pagerowcount = (int)Math.Ceiling(countHeight / this.rowgap);//獲取每頁的行數(shù)
    }
    int pagecount = (int)(rowcount / pagerowcount);//獲取打印多少頁
    pagesetupdialog.AllowOrientation = true;//啟動(dòng)打印頁面對(duì)話框的方向部分
    int colcount = 0;//記錄數(shù)據(jù)的列數(shù)
    int y = margins.Top;//獲取表格的頂邊距
    string cellvalue = "";//記錄文本信息(單元格的文本信息)
    int startrow = currentpageindex * pagerowcount;//設(shè)置打印的初始頁數(shù)
    int endrow = startrow + this.pagerowcount < rowcount ? startrow + pagerowcount : rowcount;//設(shè)置打印的大頁數(shù)
    int currentpagerowcount = endrow - startrow;//獲取打印頁數(shù)
    colcount = datagrid.ColumnCount;//獲取打印數(shù)據(jù)的列數(shù)

    int x = margins.Left;//獲取表格的左邊距  繪畫時(shí)的x軸位置
    //獲取報(bào)表的寬度
    int cwidth = 0;
    for (int j = 0; j < colcount; j++)//循環(huán)數(shù)據(jù)的列數(shù)
    {
      if (datagrid.Columns[j].Width > 0)//如果列的寬大于0
      {
        cwidth += datagrid.Columns[j].Width + colgap;//累加每列的寬度
      }
    }
    y += rowgap;//設(shè)置表格的上邊線的位置
    //設(shè)置標(biāo)題欄中的文字
    for (int j = 0; j < colcount; j++)//遍歷列數(shù)據(jù)
    {
      int colwidth = datagrid.Columns[j].Width;//獲取列的寬度
      if (colwidth > 0)//如果列的寬度大于0
      {
        cellvalue = datagrid.Columns[j].HeaderText;//獲取列標(biāo)題
        //繪制標(biāo)題欄文字
        e.Graphics.DrawString(cellvalue, headerfont, brushHeaderFont, x, y + celltopmargin);//繪制列標(biāo)題
        x += colwidth + colgap;//橫向,下一個(gè)單元格的位置
        int nnp = y + currentpagerowcount * rowgap + this.headerheight;//下一行線的位置
      }
    }
    //打印所有的行信息
    for (int i = startrow; i < endrow; i++) //對(duì)行進(jìn)行循環(huán)
    {
      x = margins.Left; //獲取線的X坐標(biāo)點(diǎn)
      for (int j = 0; j < colcount; j++)//對(duì)列進(jìn)行循環(huán)
      {
        if (datagrid.Columns[j].Width > 0)//如果列的寬度大于0
        {
          cellvalue = datagrid.Rows[i].Cells[j].Value.ToString();//獲取單元格的值
          e.Graphics.DrawString(cellvalue, Cellfont, brushHeaderFont, x, y + celltopmargin+rowgap);//繪制單元格信息
          x += datagrid.Columns[j].Width + colgap;//單元格信息的X坐標(biāo)
          y = y + rowgap * (cellvalue.Split(new char[] { '\r', '\n' }).Length - 1);//單元格信息的Y坐標(biāo)
        }
      }
      y += rowgap;//設(shè)置下行的位置
    }
    currentpageindex++;//下一頁的頁碼
    if (currentpageindex < pagecount)//如果當(dāng)前頁不是最后一頁
    {
      e.HasMorePages = true;//打印副頁
    }
    else
    {
      e.HasMorePages = false;//不打印副頁
      this.currentpageindex = 0;//當(dāng)前打印的頁編號(hào)設(shè)為0
    }
  }
  #endregion

  #region 顯示打印預(yù)覽窗體
  /// 
  /// 顯示打印預(yù)覽窗體
  /// 
  public void print()
  {
    rowcount = 0;//記錄數(shù)據(jù)的行數(shù)
    PageSettings storePageSetting = new PageSettings();//實(shí)列化一個(gè)對(duì)PageSettings對(duì)象
    PrintDocument printdocument = pagesetupdialog.Document;
    foreach (PaperSize ps in printdocument.PrinterSettings.PaperSizes)//查找當(dāng)前設(shè)置紙張
    {
      if (paperName == ps.PaperName)//如果找到當(dāng)前紙張的名稱
      {
        printdocument.DefaultPageSettings.PaperSize = ps;//獲取當(dāng)前紙張的信息
      }
    }
    if (datagrid.DataSource is System.Data.DataTable)//判斷數(shù)據(jù)類型
    {
      rowcount = ((DataTable)datagrid.DataSource).Rows.Count;//獲取數(shù)據(jù)的行數(shù)
    }
    else if (datagrid.DataSource is System.Collections.ArrayList)//判斷數(shù)據(jù)類型
    {
      rowcount = ((ArrayList)datagrid.DataSource).Count;//獲取數(shù)據(jù)的行數(shù)
    }
    try
    {
      printdocument.DefaultPageSettings.Landscape = PageScape;//設(shè)置橫向打印
      printpreviewdialog.ShowDialog();//顯示打印預(yù)覽窗體
    }
    catch (Exception e)
    {
      throw new Exception("printer error." + e.Message);
    }
  }
  #endregion
}

當(dāng)前文章:使用C#實(shí)現(xiàn)操作Word打印-創(chuàng)新互聯(lián)
地址分享:http://www.dlmjj.cn/article/deigdc.html