回 帖 发 新 帖 刷新版面

主题:高手指点:C#中打印的问题!!

我新建了一个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这句代码就可以实现打印呢?请高手指点哈!!

回复列表 (共6个回复)

沙发

用worksheet.PrintOut可以实现,参数都用Type.Missing。使用 Missing 字段通过反射进行调用,以获取参数的默认值。这里的所有参数都有默认值,所以直接用Type.Missing即可。但好像总会提示保存一个mdi的文件,如果仅为了打印,而不导出Excel的话,可以用PrintDocument控件。
        #region Print
        private void InitControl()
        {
            printPreviewDialog1.Document = printDocument1;
            pageSetupDialog1.Document = printDocument1;
            printDocument1.BeginPrint += new PrintEventHandler(BeginPrint);
            printDocument1.PrintPage += new PrintPageEventHandler(PrintPage);
        }

        private void BeginPrint(object sender, PrintEventArgs e)
        {
            currentRow = 0;
            currentPage = 1;
        }

板凳

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().

3 楼

的确是这样。总会弹出一个是否要保存mdi的对话框,能不能让这个对会框不显示呢!!有高手能解释下不??

4 楼

应该是没有安装打印机的缘故吧!
楼主不要忘了给贴评分啊,我回的贴都不给评分,郁闷中...

5 楼

不好意思!!给你评了。。。谢谢你的解答哈!!!

6 楼

哈哈,谢谢楼主。

我来回复

您尚未登录,请登录后再回复。点此登录或注册