• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#用excel作为模板打印

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//打印操作,套打、打印、预览
        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 = System.IO.Path.GetFullPath(@"../../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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#类继承中的私有字段都去了哪里?发布时间:2022-07-14
下一篇:
C#for循环遍历DataTable删除DataRow发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap