110,536
社区成员
发帖
与我相关
我的任务
分享
private PrintDocument doc = new PrintDocument();
private DataTable table;
private int hasPrintedRowCount;//保存已经打印了多少页
private void Form1_Load(object sender, EventArgs e)
{
doc.BeginPrint += new PrintEventHandler(doc_BeginPrint);
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
}
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
int currentPageMaxCount = 10;
if (table.Rows.Count < currentPageMaxCount)
{
currentPageMaxCount = table.Rows.Count;
}
Matrix oldTransform = e.Graphics.Transform;
int baseIndex = hasPrintedRowCount;
for (int i = 0; i < currentPageMaxCount; i++)
{
e.Graphics.DrawString(table.Rows[i + baseIndex]["col1"].ToString(), this.Font, Brushes.Red, e.MarginBounds.X, e.MarginBounds.Y);
e.Graphics.TranslateTransform(0, 20);//移动RenderOrigin,保证垂直方向往下面移动20个像素,也就是每行20像素高。
hasPrintedRowCount++;
}
e.Graphics.Transform = oldTransform;
oldTransform.Dispose();
if (hasPrintedRowCount < table.Rows.Count)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
void doc_BeginPrint(object sender, PrintEventArgs e)
{
table = GetTable();
}
DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("col1"));
for (int i = 0; i < 30; i++)
{
DataRow r = table.NewRow();
r["col1"] = i;
table.Rows.Add(r);
}
return table;
}
private void button1_Click(object sender, EventArgs e)
{
hasPrintedRowCount = 0;
PrintDialog dia = new PrintDialog();
dia.Document = doc;
dia.ShowDialog(this);
}
int yulan = 0;
int yushu = 1;
int shuliang ;
int countPage = 0;
{
int fltLines = 0;//一页中的行数
fltLines =30;
int nCount=0;
if (yulan == countPage*yushu)
{
if (curline > yulan * fltLines)
{
curline = 0;
}
shuliang = 0;
}
if (dataGridView1.DataBindings != null)
{
for (int m = 0; m < countPage; m++)
{
if ((curline + nCount > dataGridView1.Rows.Count * yushu))
{
break;
}
yulan++;
nCount = 0;
int rowcount = shuliang * fltLines;
int[] columnsWidth = new int[dataGridView1.Columns.Count];
//C#打印控件的使用之得到所有列的个数
int[] columnsLeft = new int[dataGridView1.Columns.Count];
for (int c = 0; c < columnsWidth.Length; c++)
//C#打印控件的使用之得到列标题的宽度
{
if (dataGridView1.Columns[c].HeaderText.Length == 2 && dataGridView1.Columns[c].HeaderText != "姓名")
{
columnsWidth[c] = ((int)e.Graphics.MeasureString(dataGridView1.Columns[c].HeaderText, tabelTextFont).Width - 30);
}
else
{
columnsWidth[c] = ((int)e.Graphics.MeasureString(dataGridView1.Columns[c].HeaderText, tabelTextFont).Width - 20);
}
}
for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++) //C#打印控件的使用之rowindex当前行
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
//C#打印控件的使用之当前列
{
if (dataGridView1.Columns[columnIndex].HeaderText.Length == 2 && dataGridView1.Columns[columnIndex].HeaderText != "姓名")
{
int w = (int)e.Graphics.MeasureString(dataGridView1.Columns[columnIndex].Name, tabelTextFont).Width - 30;
columnsWidth[columnIndex] = w < columnsWidth[columnIndex] ? w : columnsWidth[columnIndex];
}
else
{
int w = (int)e.Graphics.MeasureString(dataGridView1.Columns[columnIndex].Name, tabelTextFont).Width - 20;
columnsWidth[columnIndex] = w < columnsWidth[columnIndex] ? w : columnsWidth[columnIndex];
}
}
}//C#打印控件的使用
int rowHidth = 33;
int tableLeft;
if (ZengJia)
{
tableLeft = 40;
}
else
{
tableLeft = 60;
}
int tableTop = 70;
columnsLeft[0] = tableLeft;
for (int i = 1; i <= columnsWidth.Length - 1; i++)
{
columnsLeft[i] = columnsLeft[i - 1] + columnsWidth[i - 1] + 15;
}
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;//居中打印
e.Graphics.DrawString(Title, new Font("宋体", 15), Brushes.Black, new Point(e.PageBounds.Width / 2, 20), sf);
//打印标题
for (int c = 0; c < columnsWidth.Length; c++) //打印表中的列名
{
... }//C#打印控件的使用
for (int rowIndex = rowcount; rowIndex < dataGridView1.Rows.Count; rowIndex++)//打印表中的内容
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
{
if (dataGridView1.Columns[columnIndex].Name == "shenfen")
{
... if (va[0] == '*' && va[va.Length - 1] == '*')
{//
... }
else
{
...
}
}
else
{
tabelTextFont = new Font("宋体", 12);
}
...
}
nCount++;
if (rowIndex==dataGridView1.Rows.Count-1)
{
...
}
if (nCount == fltLines)
{
shuliang++;
...
break;
}
}
curline += nCount;
if (yulan == countPage+1)
yushu++;
//如果味道大文件的末尾,继续打印
if (curline < dataGridView1.Rows.Count*yushu)
{
e.HasMorePages = true;
return;
}
else
{
e.HasMorePages = false;
}
}
}
//C#打印控件的使用之
}