zoukankan      html  css  js  c++  java
  • C#模板打印功能-模板为WPS或Excel

    //---WPS-----
    using EtApp = ET;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Configuration;
    //--Excel--------
    using EtAppExcel = Microsoft.Office.Interop.Excel;

    namespace LensMaterialPowerCenter.Dorm
    {
        public partial class frmDormAllotRoomReportInfo : Office2007Form
        {

            //--------WPS------
            string strProduct = string.Empty;
            EtApp.Application objApp = null; //实例WPS Excel类

            EtAppExcel.Application objAppExcel = null; //实例Office Excel类  

        }

    }     

     //打印两联模板
            #region
            private void btnTwoPrint_Click(object sender, EventArgs e)
            {
                try
                {
                    if (dtStaff.Rows.Count == 0)
                    {
                        MessageBox.Show("请先选择打印数据记录,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    OpenExcelTwoFile();

                }
                catch { }
            }
            public void OpenExcelTwoFile()  //打开Excel文件 
            {
                try
                {
                    System.Windows.Forms.OpenFileDialog sfd = new OpenFileDialog();

                    sfd.DefaultExt = "*.xls";

                    sfd.Filter = "Any File(*.xls)|*.xls|(*.et)|*.et";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        //OperateTwo(sfd.FileName);  
                        OperateTwo_Page(sfd.FileName); //---------mark by zl for 测试两联纸 2013-05-20----------
                    }
                }
                catch { }

            }
            private void OperateTwo(string pFileName)
            {
                try
                {
                    //打开一个WPS Excel应用
                    objApp = new EtApp.Application();
                    objApp.DisplayAlerts = false;//DisplayAlerts 属性设置成 False,就不会出现这种警告。
                    objApp.Visible = false;

                    if (objApp == null)
                    {
                        throw new Exception("打开 WPS Excel应用时发生错误!");
                    }
                    EtApp.Workbooks wbs = objApp.Workbooks;
                    //打开一个现有的工作薄
                    EtApp._Workbook wb = wbs.Add(pFileName);
                    EtApp.Sheets shs = wb.Sheets;
                    //选择第一个Sheet页
                    EtApp._Worksheet sh = (EtApp._Worksheet)shs.get_Item(1);

                    string[] str = null;
                    int page = 0;
                    if (dtStaff.Rows.Count % 2 == 0)
                    {
                        page = dtStaff.Rows.Count / 2;
                    }
                    else
                    {
                        page = dtStaff.Rows.Count / 2 + 1;
                    }
                    for (int i = 0; i < page; i++)
                    {
                        sh.Cells[2, 1] = "";
                        sh.Cells[2, 2] = "";
                        sh.Cells[2, 3] = "";
                        //sh.Cells[2, 16] = DateTime.Now.Date;
                        sh.Cells[2, 4] = "";
                        sh.Cells[2, 5] = "";
                        sh.Cells[2, 6] = "";

                        sh.Cells[6, 1] = "";
                        sh.Cells[6, 2] = "";
                        sh.Cells[6, 3] = "";
                        //sh.Cells[2, 16] = DateTime.Now.Date;
                        sh.Cells[6, 4] = "";
                        sh.Cells[6, 5] = "";
                        sh.Cells[6, 6] = "";

                        for (int j = i * 2; j < (i * 2) + 2; j++)
                        {
                            if (j < dtStaff.Rows.Count)
                            {
                                str = new string[6];
                                str[0] = dtStaff.Rows[j][0].ToString().Trim();  //姓名
                                str[1] = "'" + dtStaff.Rows[j][1].ToString().Trim(); //身份证号
                                str[2] = dtStaff.Rows[j][2].ToString().Trim(); //性别
                                str[3] = dtStaff.Rows[j][3].ToString().Trim();  //宿舍区
                                str[4] = dtStaff.Rows[j][4].ToString().Trim(); //房号
                                str[5] = dtStaff.Rows[j][5].ToString().Trim(); //床号

                                switch ((j + 1) % 2)
                                {
                                    case 1:
                                        sh.Cells[2, 1] = str[0];
                                        sh.Cells[2, 2] = str[1];
                                        sh.Cells[2, 3] = str[2];
                                        //sh.Cells[2, 16] = DateTime.Now.Date;
                                        sh.Cells[2, 4] = str[3];
                                        sh.Cells[2, 5] = str[4];
                                        sh.Cells[2, 6] = str[5];
                                        sh.Cells[3, 9] = System.DateTime.Now.Date.ToString("yyyy年MM月dd日");
                                        break;

                                    case 0:
                                        sh.Cells[7, 1] = str[0];
                                        sh.Cells[7, 2] = str[1];
                                        sh.Cells[7, 3] = str[2];
                                        //sh.Cells[2, 16] = DateTime.Now.Date;
                                        sh.Cells[7, 4] = str[3];
                                        sh.Cells[7, 5] = str[4];
                                        sh.Cells[7, 6] = str[5];
                                        sh.Cells[8, 9] = System.DateTime.Now.Date.ToString("yyyy年MM月dd日");
                                        break;
                                }
                            }
                        }
                        objApp.Visible = true;
                        objApp.UserControl = true;

                        objApp.Visible = true;
                        objApp.UserControl = true;

                        ///
                        ///打印
                        ///
                        //wb.PrintPreview(false);
                        sh.PrintPreview(false);
                        object oMissing = System.Reflection.Missing.Value;

                        wb.PrintOut(1, 1, oMissing, oMissing, oMissing, false, false, oMissing, false, 1, 1, 12124, 12464,
                           false, ET.ETPaperTray.etPrinterAutomaticSheetFeed, false, ET.ETPaperOrder.etPrinterOverThenDown);
                    }
                    wb.Close(false, null, null);  //退出工作薄
                    objApp.Quit();  //退出wps
                    System.GC.Collect();
                }
                catch (Exception theException)
                {
                    String errorMessage;
                    errorMessage = "Error: ";
                    errorMessage = String.Concat(errorMessage, theException.Message);
                    errorMessage = String.Concat(errorMessage, " Line: ");
                    errorMessage = String.Concat(errorMessage, theException.Source);

                    MessageBox.Show(errorMessage, "Error");
                }

            }


            #region
            private void OperateTwo_Page(string pFileName)
            {
                try
                {
                    //打开一个Office Excel应用
                    objAppExcel = new EtAppExcel.Application();
                    objAppExcel.DisplayAlerts = false;//DisplayAlerts 属性设置成 False,就不会出现这种警告。
                    objAppExcel.Visible = false;

                    if (objAppExcel == null)
                    {
                        throw new Exception("打开 Office Excel应用时发生错误!");
                    }
                    EtAppExcel.Workbooks wbs = objAppExcel.Workbooks;
                    //打开一个现有的工作薄
                    EtAppExcel._Workbook wb = wbs.Add(pFileName);
                    EtAppExcel.Sheets shs = wb.Sheets;
                    //选择第一个Sheet页
                    EtAppExcel._Worksheet sh = (EtAppExcel._Worksheet)shs.get_Item(1);

                    string[] str = null;
                    int page = 0;
                    if (dtStaff.Rows.Count % 2 == 0)
                    {
                        page = dtStaff.Rows.Count / 2;
                    }
                    else
                    {
                        page = dtStaff.Rows.Count / 2 + 1;
                    }
                    for (int i = 0; i < page; i++)
                    {
                        sh.Cells[2, 1] = "";
                        sh.Cells[2, 2] = "";
                        sh.Cells[2, 3] = "";
                        //sh.Cells[2, 16] = DateTime.Now.Date;
                        sh.Cells[2, 4] = "";
                        sh.Cells[2, 5] = "";
                        sh.Cells[2, 6] = "";

                        sh.Cells[6, 1] = "";
                        sh.Cells[6, 2] = "";
                        sh.Cells[6, 3] = "";
                        //sh.Cells[2, 16] = DateTime.Now.Date;
                        sh.Cells[6, 4] = "";
                        sh.Cells[6, 5] = "";
                        sh.Cells[6, 6] = "";

                        for (int j = i * 2; j < (i * 2) + 2; j++)
                        {
                            if (j < dtStaff.Rows.Count)
                            {
                                str = new string[6];
                                str[0] = dtStaff.Rows[j][0].ToString().Trim();  //姓名
                                str[1] = "'" + dtStaff.Rows[j][1].ToString().Trim(); //身份证号
                                str[2] = dtStaff.Rows[j][2].ToString().Trim(); //性别
                                str[3] = dtStaff.Rows[j][3].ToString().Trim();  //宿舍区
                                str[4] = dtStaff.Rows[j][4].ToString().Trim(); //房号
                                str[5] = dtStaff.Rows[j][5].ToString().Trim(); //床号

                                switch ((j + 1) % 2)
                                {
                                    case 1:
                                        sh.Cells[2, 1] = str[0];
                                        sh.Cells[2, 2] = str[1];
                                        sh.Cells[2, 3] = str[2];
                                        //sh.Cells[2, 16] = DateTime.Now.Date;
                                        sh.Cells[2, 4] = str[3];
                                        sh.Cells[2, 5] = str[4];
                                        sh.Cells[2, 6] = str[5];
                                        sh.Cells[3, 9] = System.DateTime.Now.Date.ToString("yyyy年MM月dd日");
                                        break;

                                    case 0:
                                        sh.Cells[7, 1] = str[0];
                                        sh.Cells[7, 2] = str[1];
                                        sh.Cells[7, 3] = str[2];
                                        //sh.Cells[2, 16] = DateTime.Now.Date;
                                        sh.Cells[7, 4] = str[3];
                                        sh.Cells[7, 5] = str[4];
                                        sh.Cells[7, 6] = str[5];
                                        sh.Cells[8, 9] = System.DateTime.Now.Date.ToString("yyyy年MM月dd日");
                                        break;
                                }
                            }
                        }
                        objAppExcel.Visible = true;
                        objAppExcel.UserControl = true;

                        objAppExcel.Visible = true;
                        objAppExcel.UserControl = true;

                        ///
                        ///打印
                        ///
                        //wb.PrintPreview(false);
                        sh.PrintPreview(false);
                        object oMissing = System.Reflection.Missing.Value;

                        //wb.PrintOut(1, 1, oMissing, oMissing, oMissing, false, false, oMissing, false, 1, 1, 12124, 12464,
                        //   false, ET.ETPaperTray.etPrinterAutomaticSheetFeed, false, ET.ETPaperOrder.etPrinterOverThenDown);
                        wb._PrintOut(1, 1, oMissing, false, oMissing, oMissing, oMissing);
                    }
                    wb.Close(false, null, null);  //退出工作薄
                    objAppExcel.Quit();  //退出wps
                    System.GC.Collect();
                }
                catch (Exception theException)
                {
                    String errorMessage;
                    errorMessage = "Error: ";
                    errorMessage = String.Concat(errorMessage, theException.Message);
                    errorMessage = String.Concat(errorMessage, " Line: ");
                    errorMessage = String.Concat(errorMessage, theException.Source);

                    MessageBox.Show(errorMessage, "Error");
                }

            }
            #endregion
            #endregion

  • 相关阅读:
    CF149D Coloring Brackets
    CF508D
    CF483C Diverse Permutation
    【纪念】我写过几乎最长的代码
    .net core图片上传详解
    layui插件croppers的使用
    关于日常操作中sql的性能
    leeCode 278
    leeCode刷题 1078
    leeCode刷题 lc184
  • 原文地址:https://www.cnblogs.com/Tannhuser/p/6233163.html
Copyright © 2011-2022 走看看