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. asp.net 如何引用dll

    这要看你引用dll文件是什么文件,是单纯的类库还是外部或自定义控件dll文件. 如果是类库dll文件,引用的步骤是这样的:在解决方案管理器中,选中要添加引用的项目或网站-右击-添加引用-选择要添加的d ...

  2. php学习笔记——表单

    13.表单 1)GET vs. POST GET 和 POST 都创建数组(例如,array( key => value, key2 => value2, key3 => value ...

  3. linux 文件和目录操作

    1 相对路径和绝对路径 (与C++中概念相似) cd /root/cat 绝对路径 cd ../文件名/ 相对路径,指相对某个文件而言的 2 切换目录 cd  当前目录 进入某个目录 pwd 显示当前 ...

  4. DMI ( Dynamic Method Invocation )

    功能: 点击 hello , 调用 execute 函数 点击 update , 调用 update 函数 1.项目结构 2.web.xml <?xml version="1.0&qu ...

  5. Python简记

    1.字符换行: print('ab \ncd \nef')

  6. C-Free 5.0编译失败问题解决办法

    解决关于C-Free 5.0编译时提示错误:[Error] undefined reference to `__dyn_tls_init_callback' 解决办法: 因为错误提示的路径是C:\Mi ...

  7. Hdu 3363 Ice-sugar Gourd(对称,圆)

    Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. 关于安装第三方模块和PILLOW

    看廖雪峰老师这一节的教程卡在这里挺久了 在谷歌上了搜了很久,最后根据这个教程上解决了这个问题 http://www.yihaomen.com/article/python/566.htm 觉得自己好蠢 ...

  9. Ubuntu下NFS,TFTP服务搭建

    环境:Ubuntu 一. 搭建NFS服务器 (1)安装: sudo apt-get install nfs-kernel-server   #安装NFS服务器端 sudo apt-get instal ...

  10. 草料生成app自动下载的二维码

    草料官网http://cli.im/app 填写iOS和安卓的appid就好了