主题:高手指点:C#中打印的问题!!
judsondjq
[专家分:0] 发布于 2008-05-06 14:12:00
我新建了一个excel文件。往里面写了些数据。但是我现在要把它打印出来该如何实现。
我在form中有个“打印”按钮,我点击是,就会自动创建excel并将其打印出来。简单
的代码如下:
private void toolStripButton1_Click(object sender, EventArgs e)
{
Excel.Application app = new Excel.Application();
Excel.Workbook wbook = app.Workbooks.Add(Type.Missing);
Worksheet worksheet = (Worksheet)wbook.Worksheets[1];
worksheet.Cells[1, 1] = "5";
worksheet.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
}
上面Type.Missing应该怎么填写呢??是不是只需worksheet.PrintOut这句代码就可以实现打印呢?请高手指点哈!!
板凳
llg8212 [专家分:100] 发布于 2008-05-06 15:50:00
private void PrintPage(object sender, PrintPageEventArgs e)
{
float X = e.PageBounds.Width * 0.05f;//左边的X坐标
float RX = e.PageBounds.Width * 0.95f;//右边的X坐标
float Y = e.PageBounds.Height * 0.05f;//Y坐标
float BY = 0;//底线的Y坐标
if (currentPage == 1)//第一页时打印报表的标题
{
Font TitleFont = new Font("黑体", 24);
SizeF TitleSize = e.Graphics.MeasureString(cmbYear.Text + "年" + cmbMonth.Text
+ "月" + "的月报表", TitleFont);//测量字体的大小
X = (e.PageBounds.Width - TitleSize.Width) / 2;
e.Graphics.DrawString(cmbYear.Text + "年" + cmbMonth.Text
+ "月" + "的月报表", TitleFont, Brushes.Black, X, Y);//画标题
Y += TitleSize.Height + 15;
X = e.PageBounds.Width * 0.05f;
TitleFont.Dispose();
}
//定义字体和画笔
Font NormalFont = new Font("宋体", 12);
Pen LinePen = new Pen(Brushes.Black);
SizeF size = e.Graphics.MeasureString("样板",NormalFont);//测量字体的大小
//打印列头
BY = Y + size.Height;
e.Graphics.DrawLine(LinePen, X, Y, RX, Y);
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString("客户名称", NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.1f;
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString(" ", NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.03f;
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString("本月余额", NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.15f;
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString("备注", NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.24f;
e.Graphics.DrawLine(LinePen, RX, Y, RX, BY);
X = e.PageBounds.Width * 0.05f;
Y += size.Height;
//打印内容
for (int i = currentRow; i < lstvReport.Items.Count-2; i++)
{
BY = Y + size.Height;
e.Graphics.DrawLine(LinePen, X, Y, RX, Y);//画横线
e.Graphics.DrawLine(LinePen, X, Y, X, BY);//画竖线
e.Graphics.DrawString(lstvReport.Items[i].SubItems[0].Text, NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.1f;
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString(lstvReport.Items[i].SubItems[6].Text, NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.15f;
e.Graphics.DrawLine(LinePen, X, Y, X, BY);
e.Graphics.DrawString(lstvReport.Items[i].SubItems[7].Text, NormalFont, Brushes.Black, X, Y + 3);
X += e.PageBounds.Width * 0.24f;
e.Graphics.DrawLine(LinePen, RX, Y, RX, BY);
X = e.PageBounds.Width * 0.05f;
Y += size.Height;
currentRow++;
if (Y > e.PageBounds.Height * 0.95) //如果大于页面可见范围就换页
{
e.HasMorePages = true;
break;
}
}
NormalFont.Dispose();
LinePen.Dispose();
}
#endregion
在Load事件中调用InitControl()
在打印按钮事件中,调用printDocument1.Print().