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

class PrintClass
{
#region 全局變量
private DataGridView datagrid;//需要打印的數(shù)據(jù)來源
private PageSetupDialog pagesetupdialog;
private PrintPreviewDialog printpreviewdialog;
int currentpageindex = 0;//當(dāng)前頁的編號
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;//是否自動計算行數(shù)
public bool PageAspect = false;//打印的方向
public static bool PageScape = false;//打印方向
public string paperName = string.Empty;
#endregion
#region 打印信息的初始化
/// <summary>
/// 打印信息的初始化
/// </summary>
/// <param datagrid="DataGridView">打印數(shù)據(jù)</param>
/// <param PageS="int">紙張大小</param>
/// <param lendscape="bool">是否橫向打印</param>
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 頁的打印事件
/// <summary>
/// 頁的打印事件(主要用于繪制打印報表)
/// </summary>
private void printdocument_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (this.isautopagerowcount)//自動計算頁的行數(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;//啟動打印頁面對話框的方向部分
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;//獲取表格的左邊距 繪畫時的x軸位置
//獲取報表的寬度
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;//橫向,下一個單元格的位置
int nnp = y + currentpagerowcount * rowgap + this.headerheight;//下一行線的位置
}
}
//打印所有的行信息
for (int i = startrow; i < endrow; i++) //對行進(jìn)行循環(huán)
{
x = margins.Left; //獲取線的X坐標(biāo)點(diǎn)
for (int j = 0; j < colcount; j++)//對列進(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)前打印的頁編號設(shè)為0
}
}
#endregion
#region 顯示打印預(yù)覽窗體
/// <summary>
/// 顯示打印預(yù)覽窗體
/// </summary>
public void print()
{
rowcount = 0;//記錄數(shù)據(jù)的行數(shù)
PageSettings storePageSetting = new PageSettings();//實(shí)列化一個對PageSettings對象
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
}
文章題目:使用C#實(shí)現(xiàn)操作Word打印-創(chuàng)新互聯(lián)
鏈接分享:http://www.chinadenli.net/article12/deigdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、定制網(wǎng)站、面包屑導(dǎo)航、網(wǎng)頁設(shè)計公司、服務(wù)器托管、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容