不知在何处看的C#打印,应该可以实现其功能.
class DataGridPrinter
{
private PrintDocument ThePrintDocument;
private DataTable TheTable;
private DataGrid TheDataGrid;
public int RowCount = 0; // current count of rows;
private const int kVerticalCellLeeway = 10;
public int PageNumber = 1;
public ArrayList Lines = new ArrayList();
int PageWidth;
int PageHeight;
int TopMargin;
int BottomMargin;
public DataGridPrinter(DataGrid aGrid, PrintDocument aPrintDocument, DataTable aTable)
{
TheDataGrid = aGrid;
ThePrintDocument = aPrintDocument;
TheTable = aTable;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
}
public void DrawHeader(Graphics g)
{
SolidBrush ForeBrush = new SolidBrush(TheDataGrid.HeaderForeColor);
SolidBrush BackBrush = new SolidBrush(TheDataGrid.HeaderBackColor);
Pen TheLinePen = new Pen(TheDataGrid.GridLineColor,1);
StringFormat cellformat = new StringFormat();
cellformat.Trimming = StringTrimming.EllipsisCharacter;
cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
int columnwidth = PageWidth/TheTable.Columns.Count;
int initialRowCount = RowCount;
// draw the table header
float startxposition = TheDataGrid.Location.X;
RectangleF nextcellbounds = new RectangleF(0,0, 0, 0);
RectangleF HeaderBounds = new RectangleF(0, 0, 0, 0);
HeaderBounds.X = TheDataGrid.Location.X;
HeaderBounds.Y = TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
HeaderBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
HeaderBounds.Width = PageWidth;
g.FillRectangle(BackBrush, HeaderBounds);
for (int k = 0; k < TheTable.Columns.Count; k++)
{
string nextcolumn = TheTable.Columns[k].ToString();
RectangleF cellbounds = new RectangleF(startxposition, TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.HeaderFont.SizeInPoints + kVerticalCellLeeway);
nextcellbounds = cellbounds;
if (startxposition + columnwidth <= PageWidth)
{
g.DrawString(nextcolumn, TheDataGrid.HeaderFont, ForeBrush, cellbounds, cellformat);
}
startxposition = startxposition + columnwidth;
}
if (TheDataGrid.GridLineStyle != DataGridLineStyle.None)
g.DrawLine(TheLinePen, TheDataGrid.Location.X, nextcellbounds.Bottom, PageWidth, nextcellbounds.Bottom);
}
public bool DrawRows(Graphics g)
{
int lastRowBottom = TopMargin;
try
{
SolidBrush ForeBrush = new SolidBrush(TheDataGrid.ForeColor);
SolidBrush BackBrush = new SolidBrush(TheDataGrid.BackColor);
SolidBrush AlternatingBackBrush = new SolidBrush(TheDataGrid.AlternatingBackColor);
Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
StringFormat cellformat = new StringFormat();
cellformat.Trimming = StringTrimming.EllipsisCharacter;
cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
int columnwidth = PageWidth/TheTable.Columns.Count;
int initialRowCount = RowCount;
RectangleF RowBounds = new RectangleF(0, 0, 0, 0);
// draw the rows of the table
for (int i = initialRowCount; i < TheTable.Rows.Count; i++)
{
DataRow dr = TheTable.Rows[i];
int startxposition = TheDataGrid.Location.X;
RowBounds.X = TheDataGrid.Location.X;
RowBounds.Y = TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount)+1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
RowBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
RowBounds.Width = PageWidth;
Lines.Add(RowBounds.Bottom);
if (i%2 == 0)
{
g.FillRectangle(BackBrush, RowBounds);
}
else
{
g.FillRectangle(AlternatingBackBrush, RowBounds);
}
for (int j = 0; j < TheTable.Columns.Count; j++)
{
RectangleF cellbounds = new RectangleF(startxposition,
TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount) + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
if (startxposition + columnwidth <= PageWidth)
{
g.DrawString(dr[j].ToString(), TheDataGrid.Font, ForeBrush, cellbounds, cellformat);
lastRowBottom = (int)cellbounds.Bottom;
}
startxposition = startxposition + columnwidth;
}
RowCount++;
if (RowCount * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway) > (PageHeight * PageNumber) - (BottomMargin+TopMargin))
{
DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
return true;
}
}
DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
}
void DrawHorizontalLines(Graphics g, ArrayList lines)
{
Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
return;
for (int i = 0; i < lines.Count; i++)
{
g.DrawLine(TheLinePen, TheDataGrid.Location.X, (float)lines[i], PageWidth, (float)lines[i]);
}
}
void DrawVerticalGridLines(Graphics g, Pen TheLinePen, int columnwidth, int bottom)
{
if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
return;
for (int k = 0; k < TheTable.Columns.Count; k++)
{
g.DrawLine(TheLinePen, TheDataGrid.Location.X + k*columnwidth,
TheDataGrid.Location.Y + TopMargin,
TheDataGrid.Location.X + k*columnwidth,
bottom);
}
}
public bool DrawDataGrid(Graphics g)
{
try
{
DrawHeader(g);
bool bContinue = DrawRows(g);
return bContinue;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
}
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
DrawTopLabel(g);
bool more = dataGridPrinter1.DrawDataGrid(g);
if (more == true)
{
e.HasMorePages = true;
dataGridPrinter1.PageNumber++;
}
}
//DrawTopLabel打印页的标题,代码为:
void DrawTopLabel(Graphics g)
{
int TopMargin = printDocument1.DefaultPageSettings.Margins.Top;
g.FillRectangle(new SolidBrush(label1.BackColor), label1.Location.X, label1.Location.Y + TopMargin, label1.Size.Width, label1.Size.Height);
g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Location.X + 50, label1.Location.Y + TopMargin, new StringFormat());
}
//3.打印预览:
private void btnPreview_Click_1(object sender, System.EventArgs e)
{
dataGridPrinter1.PageNumber = 1;
dataGridPrinter1.RowCount = 0;
if (this.printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
}
this.printPreviewDialog1.ShowDialog();
}
//4.打印:
private void btnPrint_Click(object sender, System.EventArgs e)
{
dataGridPrinter1.PageNumber = 1;
dataGridPrinter1.RowCount = 0;
this.printDialog1.AllowSelection = true;
this.printDialog1.AllowSomePages = true;
this.printDialog1.ShowNetwork = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}