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. 初级AD域渗透系列

      net group /domain 获得所有域用户组列表 net group “domain admins” /domain 获得域管理员列表 net group “enterprise admi ...

  2. Android UI方面的学习记录

    1,android:textAllCaps=“false” android5.0后有可能button的text显示全是大写,设置这个后才能正常显示小写 2,优化listview性能: 1,view重用 ...

  3. php函数搜集

    数组去重: 方法1:php自带此功能的函数array_unique($arr) 方法2:$arr = array_flip(array_flip($arr)); //这样便可以删除重复元素. 比上面的 ...

  4. IO流数据读写总结

    1.用java自带的IO读写方法 官方API网站:http://docs.oracle.com/javase/7/docs/api/ 2.Apache的Commons-io-2.4.jar中的方法,参 ...

  5. UIBezierPath与CAShapeLayer结合画扇形

    /*让半径等于期望半径的一半 lineWidth等于期望半径 就可以画圆*/ 可以看出layer的走势是从圆边的中间一半在圆外 一半在圆内 因此让半径等于期望半径的一半 lineWidth等于期望半径 ...

  6. Entity Framework映射的总结

    EF是一个ORM工具,映射永远是最核心的部分.所以接下来详细介绍Code First模式下EF的映射配置. 通过Code First来实现映射模型有两种方式Data Annotation和Fluent ...

  7. React学习笔记-04 props

    props实现从父组件与子组件的通信. 可以通过getDefaultProps初始化props. React 提供了propsTypes来验证props的类型 官方API: propTypes:fun ...

  8. OC 调用JS 代码 处理HTML5 实战

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; ...

  9. sql server 2012提示评估期已过的解决办法 附序列号

    sql server 2012提示评估期已过的解决方法: 第一步:进入SQL2012配置工具中的安装中心. 第二步:再进入左侧维护选项界面,然后选择选择版本升级. 第三步:进入输入产品密钥界面,输入相 ...

  10. 用VB把xlsx转换为xls

    Sub Test()Dim wb As Workbook, mPath As String, f As StringApplication.DisplayAlerts = FalseApplicati ...