//---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

C#模板打印功能-模板为WPS或Excel的更多相关文章

  1. vue-element-admin实现模板打印

    一.简介 模板打印也叫”套打“,是业务系统和后台管理系统中的常用功能,B/S系统中实现”套打“比较繁琐,所以很多的B/S系统中的打印功能一直使用的是浏览器打印,很少实现模板打印.本篇将介绍在Vue E ...

  2. C#用 excel 作为模板打印

    //打印操作,套打.打印.预览        enum PrintFlag        {            /// <summary>            /// 套打,只打印没 ...

  3. 关于opencv模板匹配功能的项目测试记录

    模板匹配功能介绍的很好的一篇博客:https://www.cnblogs.com/XJT2018/p/9934139.html 就如上述博客所言:“若原图像中的匹配目标发生旋转或大小变化,该算法无效. ...

  4. C#微信接口之推送模板消息功能示例

    本文实例讲述了C#微信接口之推送模板消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

  5. C++模板常用功能讲解

    前言 泛型编程是C++继面向对象编程之后的又一个重点,是为了编写与具体类型无关的代码.而模板是泛型编程的基础.模板简单来理解,可以看作是用宏来实现的,事实上确实有人用宏来实现了模板类似的功能.模板,也 ...

  6. 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式

    按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...

  7. Printing tools 自定义模板打印的实现

    #ArcGIS for Server 自定义打印两种方法 友好阅读版本: http://gishub.info/2013/09/17/printingtools/ ## 前言使用web打印会遇到中文乱 ...

  8. ThinkPHP模板包含功能(转载)

    对于一些有共同属性的页面(如页脚),可以单独制作成一个模板,再利用 ThinkPHP 提供的模板包含功能包含进来.这样,当要修改这些公共页面时,只需修改对应的模板即可而不必修改每一个页面.模板的包含使 ...

  9. count_if 功能模板

    count_if 功能模板 template <class InputIterator, class UnaryPredicate> typename iterator_traits< ...

随机推荐

  1. synchronized和volatile的使用

    synchronized和volatile的使用 一步一步掌握线程机制(三)---synchronized和volatile的使用 现在开始进入线程编程中最重要的话题---数据同步,它是线程编程的核心 ...

  2. EM算法详解

    EM算法详解 1 极大似然估计 假设有如图1的X所示的抽取的n个学生某门课程的成绩,又知学生的成绩符合高斯分布f(x|μ,σ2),求学生的成绩最符合哪种高斯分布,即μ和σ2最优值是什么? 图1 学生成 ...

  3. 自己动手写spring容器(1)

    毕业刚刚一年多一点,毕业了后也顺利的进入了一家著名的互联网公司,做的是后台系统,用的呢也是SSI(struts2,spring)框架,平时做做项目,也已足够了,但是感觉越来越没动力了,越来越没有激情了 ...

  4. Haskell 笔记(三)类型系统

    类型 (Type) Haskell的类型系统式静态类型系统,在编译的时候就知道数据类型,所以不同类型的值运算在编译的时候就会报错,比如用布尔值和整数运算,在C语言中这种运算就不会报错. Haskell ...

  5. .net程序员求职简历

    .net程序员求职简历 个人概况 姓名 齐志超 学历 专科 毕业学校 河北软件职业技术学院 专业 软件开发与设计 手机 18730269286 年龄 22 性别 男 现居住地 北京 电子邮件 qzc9 ...

  6. [置顶] Nosql笔记(一)——关系型数据库回顾

    Nosql笔记(一)——关系型数据库回顾 在平常的商业应用中,我们所使用的大多都是关系型数据库,诸如SQL  Server. MY SQL. Oracle等. 关于关系型数据库中的关键技术: 存储引擎 ...

  7. anadonca环境配置和模块安装

    1.最方便的python环境配置: 下载anaconda即可,自带spyder,集成科学计算的库,自带pip,不用折腾. 想用sublime编写python并运行的话,需要自己配置编译环境,并下载插件 ...

  8. ajaxfileupload 实现多文件上传

    官网下载ajaxfileupload.js: 修改源码: jQuery.extend({ createUploadIframe: function(id, uri) { //create frame ...

  9. [转] 数据库加锁 sql加锁的

    [导读: 各种大型数据库所采用的锁的基本理论是一致的,但在具体实现上各有差别.SQL Server更强调由系统来管理锁.在用户有SQL请求时,系统分析请求,自动在满足锁定条件和系统性能之间为数据库加上 ...

  10. C++ 头文件系列(deque)

    简介 deque是double ended queue(即双端队列)的简称. 就像C++中的大部分容器的一样,deque具有以下属性: 顺序的(sequence) 动态增长的(dynamic grow ...