zoukankan      html  css  js  c++  java
  • C#操作Word完全功略

    前提:
    导入COM库:Microsoft word 11.0 Object Library.
    引用里面就增加了:



    创建新Word
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

    打开文档:
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                
    object fileName = @"E:\CCCXCXX\TestDoc.doc";
                oDoc 
    = oWord.Documents.Open(ref fileName,
                
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    导入模板
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                
    object fileName = @"E:\XXXCCX\Test.doc";
                oDoc 
    = oWord.Documents.Add(ref fileName, ref oMissing,
                                
    ref oMissing, ref oMissing);


    .添加新表
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

                
    object start = 0;
                
    object end = 0;
                Word.Range tableLocation 
    = oDoc.Range(ref start, ref end);
                oDoc.Tables.Add(tableLocation, 
    34ref oMissing, ref oMissing);

    .表插入行
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

                
    object start = 0;
                
    object end = 0;
                Word.Range tableLocation 
    = oDoc.Range(ref start, ref end);
                oDoc.Tables.Add(tableLocation, 
    34ref oMissing, ref oMissing);

                Word.Table newTable 
    = oDoc.Tables[1];
                
    object beforeRow = newTable.Rows[1];
                newTable.Rows.Add(
    ref beforeRow);

    .单元格合并
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

                
    object start = 0;
                
    object end = 0;
                Word.Range tableLocation 
    = oDoc.Range(ref start, ref end);
                oDoc.Tables.Add(tableLocation, 
    34ref oMissing, ref oMissing);

                Word.Table newTable 
    = oDoc.Tables[1];
                
    object beforeRow = newTable.Rows[1];
                newTable.Rows.Add(
    ref beforeRow);

                Word.Cell cell 
    = newTable.Cell(11);
                cell.Merge(newTable.Cell(
    12));

    .单元格分离
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

                
    object start = 0;
                
    object end = 0;
                Word.Range tableLocation 
    = oDoc.Range(ref start, ref end);
                oDoc.Tables.Add(tableLocation, 
    34ref oMissing, ref oMissing);

                Word.Table newTable 
    = oDoc.Tables[1];
                
    object beforeRow = newTable.Rows[1];
                newTable.Rows.Add(
    ref beforeRow);

                Word.Cell cell 
    = newTable.Cell(11);
                cell.Merge(newTable.Cell(
    12));

                
    object Rownum = 2;
                
    object Columnnum = 2;
                cell.Split(
    ref Rownum, ref  Columnnum);

    通过段落控制插入
                object oMissing = System.Reflection.Missing.Value;
                
    object oEndOfDoc = "\\endofdoc"/* \endofdoc is a predefined bookmark */

                
    //Start Word and create a new document.
                Word._Application oWord;
                Word._Document oDoc;
                oWord 
    = new Word.Application();
                oWord.Visible 
    = true;
                oDoc 
    = oWord.Documents.Add(ref oMissing, ref oMissing,
                    
    ref oMissing, ref oMissing);

                
    //Insert a paragraph at the beginning of the document.
                Word.Paragraph oPara1;
                oPara1 
    = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara1.Range.Text 
    = "Heading 1";
                oPara1.Range.Font.Bold 
    = 1;
                oPara1.Format.SpaceAfter 
    = 24;    //24 pt spacing after paragraph.
                oPara1.Range.InsertParagraphAfter();
  • 相关阅读:
    Shell基础:什么是shell脚本、2种脚本解释器、#!约定解释器类型、运行shell脚本的2种方式、shell变量命令规范/赋值/如何使用/只读变量/删除变量/变量类型、shell字符串及其常用方法、shell数组及其常用方法、shell注释
    Linux su命令:su命令语法、su root与su
    docker容器内使用apt报错E: List directory /var/lib/apt/lists/partial is missing.
    浅析事务是什么、mysql是如何实现事务提交和回滚的、保证事务持久性redo log的实现原理、保证事务一致性undo log的实现原理、事务ACID特性及其实现原理
    浅析前后端分离架构下的API安全问题:JWT保证token不被盗用的方案(即如何防范Replay Attacks)
    浅析如何保证缓存与数据库的双写一致性:4种更新缓存的设计模式理解
    浅析SpringCloud中断路器是什么、断路器的作用以及在Feign中使用断路器
    浅析后端微服务涉及到定时任务时如何解决多集群定时任务重复执行并发的方案对比
    Linux连续执行多条命令的写法区别
    Dockerfile中RUN/CMD/ENTRYPOINT命令区别
  • 原文地址:https://www.cnblogs.com/top5/p/1684723.html
Copyright © 2011-2022 走看看