C# 在word文档中复制表格并粘帖到下一页中

object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word._Application oWord;
            Microsoft.Office.Interop.Word._Document oDoc;
            oWord = new Microsoft.Office.Interop.Word.Application();
            //显示word文档
            oWord.Visible = true;
            //取得word文件模板
            object fileName = System.Windows.Forms.Application.StartupPath + "\word.doc";
            //根据模板生成一个新文档,相当于另存为
            oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
                            ref oMissing, ref oMissing);

//复制第一个表格
            oDoc.Tables[1].Select();
            oWord.Selection.Copy();

//在这里操作表格中的文本
            oDoc.Tables[1].Cell(1, 1).Range.Text = "这是第一个表格";

//下一页
            object mymissing = System.Reflection.Missing.Value;
            object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            oWord.Selection.EndKey(ref myunit, ref mymissing);
            object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
            oWord.Selection.InsertBreak(ref pBreak);

//粘贴第一个表格
            oWord.Selection.Paste();

oDoc.Tables[2].Cell(1, 1).Range.Text = "这是第二个表格";

bool saveChange = false;

//C#操作Word表格操作

object missing = System.Reflection.Missing.Value;

object template = (object)templateFilePath;  

object filename = (object)saveFilePath;    

object isVisible = missing;  

object readOnly = missing;  

object breakType = Word.WdBreakType.wdSectionBreakNextPage;  

object isSaveChange = (object)saveChange;  

Word.Document doc = null;  

//定义一个Word.Application 对象  

Word.Application WordApp = new Word.ApplicationClass();  

//C#操作Word表格操作之打开文档  

doc = WordApp.Documents.Open(ref template,   

ref missing,ref readOnly,ref missing,   

ref missing, ref missing, ref missing,   

ref missing, ref missing,ref missing,   

ref missing, ref isVisible, ref missing,   

ref missing, ref missing, ref missing);  

 

//C#操作Word表格操作之设置页眉文本  

 

WordApp.ActiveWindow.ActivePane.View.SeekView =  

 Word.WdSeekView.wdSeekCurrentPageHeader;  

WordApp.Selection.WholeStory();  

WordApp.Selection.TypeText( this.m_titleText );  

WordApp.ActiveWindow.ActivePane.View.SeekView =   

Word.WdSeekView.wdSeekMainDocument;  

 

//页面设置,设置页面为纵向布局,设置纸张类型为A4纸  

 

doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;  

doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);  

doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);  

 

//C#操作Word表格操作之创建表格及设置表格和单元格属性  

 

object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;  

doc.Content.Tables.Add(  

WordApp.Selection.Range, totalCount + 1,   

totalField - keyCount_1, ref missing,   

ref autoFitBehavior);  

 

//C#操作Word表格操作之合并单元隔  

 

doc.Content.Tables[1].Cell(i+1,j).Select();  

object moveUnit = Word.WdUnits.wdLine;  

object moveCount = 1;  

object moveExtend = Word.WdMovementType.wdExtend;  

WordApp.Selection.MoveUp(ref moveUnit,  

 ref moveCount, ref moveExtend);  

WordApp.Selection.Cells.Merge();  

WordApp.Selection.Cells.VerticalAlignment =   

Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  

 

doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”  

 

//添加表格行  

 

doc.Content.Tables[0].Rows.Add(ref beforeRow);  

 

//C#操作Word表格操作之添加表格列  

 

doc.Content.Tables[0].Columns.Add(ref beforeColumn);  

 

//文本居中  

WordApp.Selection.ParagraphFormat.Alignment =  

Word.WdParagraphAlignment.wdAlignParagraphCenter;  

WordApp.Selection.Cells.VerticalAlignment =   

Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  

 

//C#操作Word表格操作之选中单元格文字垂直居中  

 

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  

 

Word.Table newTable = oDoc.Tables[1];  

object beforeRow = newTable.Rows[1];  

newTable.Rows.Add(ref beforeRow);  

 

Word.Cell cell = newTable.Cell(1, 1);  

cell.Merge(newTable.Cell(1, 2)); 

C# 在word文档中复制表格并粘帖到下一页中的更多相关文章

  1. java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)

    转自:https://blog.csdn.net/u014475796/article/details/49893261 在设计到数据库的开发中,难免要将图片或文档文件(如word)插入到数据库中的情 ...

  2. 怎样把Word文档导入Excel表格

    Word是现在办公中的基础文件格式了,很多的内容我们都通过Word来进行编辑,那么当我们需要将Word文档里的信息导入到Excel里面的时候,我们应该怎样做呢?下面我们就一起来看一下吧. 操作步骤: ...

  3. Struts2利用iText导出word文档(包含表格)以提供下载

    J2EE ExcelStrutsXML  在公司实习期间,带我的老师让我实现一功能——在显示课表的页面上上点击“导出文件“时能以word文档形式下载课表.将课表导出到excel里的功能他们已经实现了, ...

  4. JAVAWEB导出word文档,遍历表格数据,导出图片

    这是写的另一个导出word方法:https://www.cnblogs.com/pxblog/p/12790904.html 本次使用的是easypoi框架 官方教程:https://opensour ...

  5. word文档去掉复制过来的背景颜色

    选择清除格式

  6. 2018-10-04 [日常]用Python读取word文档中的表格并比较

    最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-d ...

  7. 2.QT中操作word文档

     Qt/Windows桌面版提供了ActiveQt框架,用以为Qt和ActiveX提供完美结合.ActiveQt由两个模块组成: A   QAxContainer模块允许我们使用COM对象并且可以 ...

  8. C# Word文档中插入、提取图片,文字替换图片

    Download Files:ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操作.图像会比阅读文字更有吸引力,而且图像是 ...

  9. java 处理word文档 (含图片,表格内容)

    因为本人长期从事Oa相关项目的开发,所以处理word文档,Pdf,Excel等是在所难免的. 1.需求      处理Excel 能够用jxl        或者poi 2需求     用户在系统上填 ...

随机推荐

  1. iOS Plugins

    iOS Plugins This section provides details for how to implement native plugin code on the iOS platfor ...

  2. loadrunner破解方法--lm70.dll,mlr5lprg.dll下载地址

    一.由于在压力测试执行中,出现一个-10803的错误 ,为解决这个错误,重新设置的环境变量,在次执行错误,这个问题解决了,但另外一个问题出来了,LR,打开脚本编辑器老提示找不到TEMP目录,当时没有想 ...

  3. 二十、oracle pl/sql基础

    一.pl/sql developer开发工具pl/sql developer是用于开发pl/sql块的集成开发环境(ide),它是一个独立的产品,而不是oracle的一个附带品. 二.pl/sql介绍 ...

  4. JavaScript——Cookie

    JavaScript中的Cookie基础 页面用来保存信息的,比如登录.记住用户名. [cookie的特性] (1)同一个网站中所有页面共享一套cookie: (2)数量.大小有限: (3)有保质期, ...

  5. win10锁屏壁纸路径

    C:\Users\ShanYu\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalS ...

  6. oracle 行专列

    首先,做准备工作. 建表 -- Create table create table DEMO ( n_iden NUMBER, c_order_code NVARCHAR2(), c_order_na ...

  7. C#委托与事件讲解(一)

    首先,我们还是先说说委托吧,从字面上理解,只要是中国人应该都知道这个意思,除非委托2个中文字不认识,举个例子,小明委托小张去买车票.     但是在我们的程序世界里,也是这么的简单吗?当然,什么是OO ...

  8. 第一百一十三节,JavaScript文档对象,DOM基础

    JavaScript文档对象,DOM基础 学习要点: 1.DOM介绍 2.查找元素 3.DOM节点 4.节点操作 DOM(Document Object Model)即文档对象模型,针对HTML和XM ...

  9. Cookie的简单使用

    一,新建一个空网站,添加一个Default.aspx <%@ Page Language="C#" AutoEventWireup="true" Code ...

  10. 2016GIAC全球互联网架构大会日程分享

    GIAC全球互联网架构大会是中国互联网技术领域一年一度的行业盛事,每年从互联网架构最热门高压应对.云计算.大数据.机器学习.分布式架构等领域甄选前沿的有典型代表的技术创新及研发实践的架构案例,分享他们 ...