| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3203 人关注过本帖, 1 人收藏
标题:[讨论]用excel模板打印问题
只看楼主 加入收藏
FlyBee
Rank: 1
来 自:云南玉溪
等 级:新手上路
帖 子:113
专家分:0
注 册:2007-12-11
结帖率:100%
收藏(1)
 问题点数:0 回复次数:2 
[讨论]用excel模板打印问题
最近在网上看了一个用Excel模板打印的文章,测试通过。。。。
就是有个问题大家研究下怎么把图片加载进去连图片也可以打印出来
源码

/*
'   //-*****************************************************-//
'  //            如果您要献一份爱心,请参预希望工程!       //
' //                如果您还支持作者,请联系作者!         //
'//-*****************************************************-//
'
'    /// 作 者:长江支流(周方勇)
'    /// Email:MisGoldPrinter@  QQ:150439795
'    /// 网 址:www.
'    /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
'
*/

//打印操作,套打、打印、预览
        enum PrintFlag
        {
            /// <summary>
            /// 套打,只打印没有印刷的部分
            /// </summary>
            CasePrint,
            /// <summary>
            /// 打印全部
            /// </summary>
            PrintAll,
            /// <summary>
            /// 预览全部
            /// </summary>
            PreviewAll        
        }


        //套打、打印、预览三个按钮关联些委托实例
        private void Print_Click(object sender, System.EventArgs e)
        {
            Button btn = (Button)sender;
            switch(btn.Tag.ToString())
            {
                case "套打":
                    Print(PrintFlag.CasePrint);
                    break;
                case "打印":
                    Print(PrintFlag.PrintAll);
                    break;
                case "预览":
                    Print(PrintFlag.PreviewAll);
                    break;            
            }        
        }


        private void Print(PrintFlag p_printFlag)
        {

            //制作步骤:
            /*    1、用Excel作出与要打印的样式一样的电子表格存为模板;
             *     技巧:最好把第一行与第一列作为空行,以利于调整边距(虽然Excel、打印机可调整页边距), 尽量的在需要调整的地方多空几行与几列,以利于调整套打对准
             *
             *  2、如同本程序一样,将Excel作为套打的模板,直接将要打印的数据写入;
             *
             *  3、打印,根据实际的效果调整Excel模板行高列宽及空出的行列, 直到能够准确的套上。将模板拷贝一份,清除模板上的文字与网格线,做成套打的模板。
             */

            #region 套打、打印预览

            //用Excel打印,步骤为:打开、写数据、打印预览、关闭
            GoldPrinter.ExcelExpert.ExcelBase excel = new GoldPrinter.ExcelExpert.ExcelBase();    
            string strFileName = "invoice.xlt";            //模板文件名

            if (p_printFlag == PrintFlag.CasePrint)
            {
                strFileName = "invoiceCase.xlt";        //套打模板文件名
            }

            string strExcelTemplateFile = (@"..\..\ExcelTemplate\" + strFileName);
                        
            excel.Open(strExcelTemplateFile);                            //用模板文件
            excel.Visible = false;                                        //建议:如果excel不可见且在编程情况下写数据特别是大量数据时
            excel.ScreenUpdating = false;                                //设置此开关能大大提高效率。写完后如要可见,再设置此属性为真刷新屏幕。
            excel.Caption = "税 务 机 关 代 开 统 一 发 票(国 税)";    //"MIS金质打印通  通打天下报表";


            //在模板中写入要打印的数据

            //***发票抬头***

            //年月日
            excel.SetCellText(7,"B",txtYear.Text + "年" + txtMonth.Text + "月" + txtDay.Text + "日" );    
            
            //付款方名称    
            excel.SetCellText(8,"D",txtPayer.Text);
            //收款方名称    
            excel.SetCellText(9,"D",txtCollecter.Text);
            //及地址、电话    
            excel.SetCellText(11,"D",txtCollecterAddTel.Text);


            // 代开普通发票    申 请 表 号 码    
            excel.SetCellText(8,"J",txtInvoiceApplicationNo.Text);
            //收款方识别号或 证 件 号 码    
            excel.SetCellText(9,"J",txtCollecterID.Text);

            
            //***品名及金额、备注***
            //B14到B23是品名   F14到F23为金额

            excel.SetCellText("B14",txtP1.Text);
            excel.SetCellText("F14",txtJ1.Text);
            
            excel.SetCellText("B15",txtP2.Text);
            excel.SetCellText("F15",txtJ2.Text);

            excel.SetCellText("B16",txtP3.Text);
            excel.SetCellText("F16",txtJ3.Text);

            excel.SetCellText("B17",txtP4.Text);
            excel.SetCellText("F17",txtJ4.Text);

            excel.SetCellText("B18",txtP5.Text);
            excel.SetCellText("F18",txtJ5.Text);

            excel.SetCellText("B19",txtP6.Text);
            excel.SetCellText("F19",txtJ6.Text);

            excel.SetCellText("B20",txtP7.Text);
            excel.SetCellText("F20",txtJ7.Text);

            excel.SetCellText("B21",txtP8.Text);
            excel.SetCellText("F21",txtJ8.Text);

            excel.SetCellText("B22",txtP9.Text);
            excel.SetCellText("F22",txtJ9.Text);

            excel.SetCellText("B23",txtP10.Text);
            excel.SetCellText("F23",txtJ10.Text);


            //备注
            //excel.SetCellText(14,"I",txtMemo.Imag.);



            //***发票总金额***

            //合计人民币   (大写)        
            excel.SetCellText(24,"D",txtTotalUpper.Text);
            //合计人民币   (小写)
            excel.SetCellText(24,"K",txtTotalLower.Text);


            //税额   (大写)    
            excel.SetCellText(25,"D",txtTaxUpper.Text);
            //税额   (小写)    
            excel.SetCellText(25,"L",txtTaxLower.Text);


            //***发票尾***
            //税控码
            excel.SetCellText(26,"C",txtTaxControlCode.Text);
            //开票人:    
            excel.SetCellText(26,"H",txtWriter.Text);

            //刷新Excel屏幕
            excel.ScreenUpdating = true;


            if (p_printFlag == PrintFlag.CasePrint || p_printFlag == PrintFlag.PrintAll)
            {
                excel.Print();                //打印
            }
            else
            {
                excel.PrintPreview();        //预览
            }

            excel.Close();                    //关闭并释放    
            
            #endregion
    
        }


        private void frmInvoice_Load(object sender, System.EventArgs e)
        {
            //初始当日日期
            System.DateTime dt = System.DateTime.Now;
            SetToday(dt);
        }


        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }


        //回车
        private void frmInvoice_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                SendKeys.Send("{TAB}");
            }
        }


        //金额小写转人民币大写
        private void txtTotalLower_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            SetUpperMoney();
        }


        //重新总计
        private void txtJX_TextChanged(object sender, System.EventArgs e)
        {
            double dblMoney = 0;

            dblMoney += GetInputMoney(txtJ1.Text);
            dblMoney += GetInputMoney(txtJ2.Text);
            dblMoney += GetInputMoney(txtJ3.Text);
            dblMoney += GetInputMoney(txtJ4.Text);
            dblMoney += GetInputMoney(txtJ5.Text);
            dblMoney += GetInputMoney(txtJ6.Text);
            dblMoney += GetInputMoney(txtJ7.Text);
            dblMoney += GetInputMoney(txtJ8.Text);
            dblMoney += GetInputMoney(txtJ9.Text);
            dblMoney += GetInputMoney(txtJ10.Text);

            txtTotalLower.Text = dblMoney.ToString();
            SetUpperMoney();

        }


        //改变税率重算
        private void cboTaxRate_TextChanged(object sender, System.EventArgs e)
        {
            SetUpperMoney();
        }


        private void btnRefDate_Click(object sender, System.EventArgs e)
        {
            cldSelect.Visible = true;
            cldSelect.SetDate(new DateTime(int.Parse(txtYear.Text),int.Parse(txtMonth.Text),int.Parse(txtDay.Text)));
            cldSelect.Focus();
        }


        private void cldSelect_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
        {
            SetToday(e.End);
            cldSelect.Visible = false;        
        }


        //大写合计人民币、税额
        private void SetUpperMoney()
        {
            try
            {
//                string strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text));
//                //合计人民币
//                txtTotalUpper.Text = strUpper;
//
//                strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text) * Double.Parse(cboTaxRate.Text) / 100);
//                //税额 = 合计人民币 * 税率
//                txtTaxUpper.Text = strUpper;

            }
            catch{}                
        }


        private double GetInputMoney(string p_text)
        {
            double dblReturn = 0;
            try
            {
                dblReturn = double.Parse(p_text);
            }
            catch{}

            return dblReturn;
        }


        private void SetToday(System.DateTime dt)
        {
            txtYear.Text = dt.Year.ToString();    
            
            txtMonth.Text = GetLengthTwoDate(dt.Month.ToString());
            txtDay.Text = GetLengthTwoDate(dt.Day.ToString());    
        }


        private string GetLengthTwoDate(string p_MonthOrDay)
        {
            string strReturn = p_MonthOrDay;
            if (strReturn.Length == 1)
            {
                strReturn = "0" + strReturn;
            }

            return strReturn;
        }


    }//End Class
}//End Namespace
搜索更多相关主题的帖子: excel 模板 打印 
2008-09-30 09:44
C心情
Rank: 2
等 级:论坛游民
帖 子:90
专家分:74
注 册:2008-1-19
收藏
得分:0 
期待中....
2008-09-30 12:22
FlyBee
Rank: 1
来 自:云南玉溪
等 级:新手上路
帖 子:113
专家分:0
注 册:2007-12-11
收藏
得分:0 
我发现一个很严重的问题。。。。
就是只在excel单元格中插一张图片都很难
2008-10-01 23:12
快速回复:[讨论]用excel模板打印问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018971 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved