![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
PrintFunction
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public String printName = String.Empty;
public Font prtTextFont = new Font("Verdana", 10);
public Font prtTitleFont = new Font("宋体", 10);
private String[] titles = new String[0];
public String[] Titles
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
titles = value as String[];
if (null == titles)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
titles = new String[0];
}
}
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return titles;
}
}
private Int32 left = 20;
private Int32 top = 20;
public Int32 Top
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
top = value;
}
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return top;
}
}
public Int32 Left
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
left = value;
}
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return left;
}
}
private DataTable printedTable;
private Int32 pheight;
private Int32 pWidth;
private Int32 pindex;
private Int32 curdgi;
private Int32[] width;
private Int32 rowOfDownDistance = 3;
private Int32 rowOfUpDistance = 2;
public Boolean PrintDataTable(DataTable table)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
PrintDocument prtDocument = new PrintDocument();
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (printName != String.Empty)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
prtDocument.PrinterSettings.PrinterName = printName;
}
prtDocument.DefaultPageSettings.Landscape = true;
prtDocument.OriginAtMargins = false;
PrintDialog prtDialog = new PrintDialog();
prtDialog.AllowSomePages = true;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
prtDialog.ShowHelp = false;
prtDialog.Document = prtDocument;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (DialogResult.OK != prtDialog.ShowDialog())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
printedTable = table;
pindex = 0;
curdgi = 0;
width = new Int32[printedTable.Columns.Count];
pheight = prtDocument.PrinterSettings.DefaultPageSettings.Bounds.Bottom;
pWidth = prtDocument.PrinterSettings.DefaultPageSettings.Bounds.Right;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
prtDocument.PrintPage += new PrintPageEventHandler(Document_PrintPage);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
prtDocument.Print();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
catch( InvalidPrinterException ex )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("没有安装打印机");
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("打印错误");
}
prtDocument.Dispose();
return true;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Int32 startColumnControls = 0;
Int32 endColumnControls = 0;
private void Document_PrintPage(object sender, PrintPageEventArgs ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Int32 x = left;
Int32 y = top;
Int32 rowOfTop = top;
Int32 i;
Pen pen = new Pen(Brushes.Black, 1);
if (0 == pindex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (i = 0; i < titles.Length; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ev.Graphics.DrawString(titles[i], prtTitleFont, Brushes.Black, left, y + rowOfUpDistance);
y += prtTextFont.Height + rowOfDownDistance;
}
rowOfTop = y;
foreach(DataRow dr in printedTable.Rows)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (i = 0; i < printedTable.Columns.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Int32 colwidth = Convert.ToInt16(ev.Graphics.MeasureString(dr[i].ToString().Trim(), prtTextFont).Width);
if (colwidth>width[i])
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
width[i] = colwidth;
}
}
}
}
for (i = endColumnControls; i < printedTable.Columns.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
String headtext = printedTable.Columns[i].ColumnName.Trim();
if (pindex == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int colwidth = Convert.ToInt16(ev.Graphics.MeasureString(headtext, prtTextFont).Width);
if (colwidth > width[i])
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
width[i] = colwidth;
}
}
if (x + width[i] > pheight)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
ev.Graphics.DrawString(headtext, prtTextFont, Brushes.Black, x, y + rowOfUpDistance);
x += width[i];
}
startColumnControls = endColumnControls;
if (i < printedTable.Columns.Count)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
endColumnControls = i;
ev.HasMorePages = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
endColumnControls = printedTable.Columns.Count;
}
ev.Graphics.DrawLine(pen, left, rowOfTop, x, rowOfTop);
y += rowOfUpDistance + prtTextFont.Height + rowOfDownDistance;
ev.Graphics.DrawLine(pen, left, y, x, y);
for (i = curdgi; i < printedTable.Rows.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
x = left;
for (Int32 j = startColumnControls; j < endColumnControls; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ev.Graphics.DrawString(printedTable.Rows[i][j].ToString().Trim(), prtTextFont, Brushes.Black, x, y + rowOfUpDistance);
x += width[j];
}
y += rowOfUpDistance + prtTextFont.Height + rowOfDownDistance;
ev.Graphics.DrawLine(pen, left, y, x, y);
if (y > pWidth - prtTextFont.Height - 30)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
}
ev.Graphics.DrawLine(pen, left, rowOfTop, left, y);
x = left;
for (Int32 k = startColumnControls; k < endColumnControls; k++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
x += width[k];
ev.Graphics.DrawLine(pen, x, rowOfTop, x, y);
}
if (y > pWidth - prtTextFont.Height - 30)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pindex++;
if (0 != startColumnControls)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
curdgi = i + 1;
}
if (!ev.HasMorePages)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
endColumnControls = 0;
}
ev.HasMorePages = true;
}
}
public void PrintPreviewDataTable(DataTable prtTable)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
PrintDocument prtDocument = new PrintDocument();
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (printName != String.Empty)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
prtDocument.PrinterSettings.PrinterName = printName;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
prtDocument.DefaultPageSettings.Landscape = true;
prtDocument.OriginAtMargins = false;
printedTable = prtTable;
pindex = 0;
curdgi = 0;
width = new int[printedTable.Columns.Count];
pheight = prtDocument.PrinterSettings.DefaultPageSettings.Bounds.Bottom;
pWidth = prtDocument.PrinterSettings.DefaultPageSettings.Bounds.Right;
prtDocument.PrintPage += new PrintPageEventHandler(Document_PrintPage);
PrintPreviewDialog prtPreviewDialog = new PrintPreviewDialog();
prtPreviewDialog.Document = prtDocument;
prtPreviewDialog.ShowIcon = false;
prtPreviewDialog.WindowState = FormWindowState.Maximized;
prtPreviewDialog.ShowDialog();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
catch (InvalidPrinterException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("没有安装打印机");
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("打印预览失败");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
以上代码没有对 当列的长度操作1页的情况,进行考虑。
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow