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. Ansible hostvars

    1.  inventory hosts file 中的server 变量会覆盖group变量. hostvars: { "iaas_name": "test", ...

  2. spark MLLib的基础统计部分学习

    参考学习链接:http://www.itnose.net/detail/6269425.html 机器学习相关算法,建议初学者去看看斯坦福的机器学习课程视频:http://open.163.com/s ...

  3. Java 代码性能优化

    代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...

  4. mysql中文显示问号

    mysql插入中文后显示为?,查到http://blog.csdn.net/chenxingzhen001/article/details/7567812中方法,即 在my.ini配置文件中的[myd ...

  5. mssql update from

    update b set memo = a.name from a,b where a.id = b.id --mssql的update :from语法 2 --a表 b表 结构分别 id ,name ...

  6. 【Python之路】第六篇--Python基础之模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  7. HDU 4451 Dressing

    HDU 4451 Dressing 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4451 Description Wangpeng has ...

  8. java 基础知识2

  9. Kettle 使用相关

    Kettle 作为国外的一款开源ETL软件,要使用了

  10. 【JS】学习18天Jquery Moblie的总结笔记。

    现在是2013年11月18日1:53分,从1号开始学习JqueryMoblie(简称JQM),这些天遇到的问题,和走的弯路还真不少. 先做个小总结,做了那么多天的一些收获: ●JQM是一个手机网站/轻 ...