惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
K
Kaspersky official blog
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
U
Unit 42
D
Docker
I
InfoQ
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
C
Check Point Blog
The Cloudflare Blog
美团技术团队
V
Vulnerabilities – Threatpost
博客园_首页
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
A
Arctic Wolf
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
C
Cyber Attacks, Cyber Crime and Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
The Register - Security
The Register - Security
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
Y
Y Combinator Blog
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
L
Lohrmann on Cybersecurity

博客园 - Candyxiaoqiang

使用 ADO.NET 访问 Oracle 9i 存储过程(转) 使用 ADO.NET 和 Oracle 进行高级数据访问 (转自) HttpRequest, HttpRuntime, AppDomain and friends() 在showModalDialog窗体里,单击服务端Button产生新页面解决方法 - Candyxiaoqiang - 博客园 asp.net按钮 button的onclick事件 与oncommand 事件的区别 汇总 SQL 连接字符串的说明(转) JavaScript相关资料( 收集中.......) - Candyxiaoqiang - 博客园 Visual Studio常用小技巧 觉得不错 转一下 Web开发中用的工具(不断更新) C#实现Web文件上传的两种方法 (转) document,event javascript中常用对象介绍 带中文说明的 (转) ASP.Net实现将Word转换PDF格式 (转自:思绪随风~~~) 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料) 完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件 (转自zellzhang) 如何:将文档发送到打印机(转) 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)(转) 在IIS上启用Gzip压缩(HTTP压缩) (转自子秋的博客) 如何采用Local方式连接到ArcGIS Server(转) VS2005 直接调试网页不能显示
C# 拆分word(根据标题或书签拆分) (转自zrx401558287)
Candyxiaoqiang · 2009-05-21 · via 博客园 - Candyxiaoqiang

【实现功能】

     读取word文件的内容,根据word的标题或书签把word分成多个部分的word文件。

【开发环境】

     vs2005+office2003

【实现过程】

     1:操作word文档首先要添加word的.NET引用:Microsoft.Office.Interop.Word

     2:首先读取word文档


        ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();

        
/// <summary>
        
/// 读取word文档
        
/// </summary>
        
/// <param name="path">word文档路径</param>
        
/// <returns></returns>

        private Document ReadDocument(string path)
        
{
            
object missing = System.Reflection.Missing.Value;
            
            Type wordType 
= word.GetType();

            Documents docs 
= word.Documents;
            Type docsType 
= docs.GetType();
            
object objDocName = path;
            Document doc 
= (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, truetrue });

            
return doc;
        }

     3:创建一个文档来保存拆分的word部分


        /// <summary>
        
/// 创建word文档
        
/// </summary>
        
/// <returns></returns>

        private Document CreateDocument()
        
{
            
object missing = System.Reflection.Missing.Value;
            Document newdoc 
= word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            
return newdoc;
        }

     4:比较关键的就是根据标题或书签来定位了.

     如果根据标题定位就比较慢了,我们要遍历word中所有的Paragraphs然后判断他的OutlineLevel是否是标题,如果他的值是WdOutlineLevel.wdOutlineLevel1则说明是标题1,这样以此类推.

     如果根据书签来定位就是比较简单的也是比较有效率的,因为word对象里本身就有Bookmarks这个对象,我的程序里是固定的书签所以我就直接根据定义了一个int[,] result = new int[9, 2];来记录这九个书签的开始和结束位置.


/// <summary>
        
/// 获取书签的位置
        
/// </summary>
        
/// <param name="word">文档对象</param>
        
/// <returns></returns>

        private int[,] GetPosition(Document word)
        
{
            
int[,] result = new int[92];
            
int titleIndex = 0;
            
int bmcount = word.Bookmarks.Count;
            
for (int i = 1; i <= bmcount; i++)
            
{
                
object index = i;
                Bookmark bm
=word.Bookmarks.get_Item(ref index);
                
if (bm.Name == "模板书签禁止修改" + Convert.ToString(titleIndex + 1))
                
{
                    result[titleIndex, 
0= bm.Start;
                    result[titleIndex, 
1= bm.End;
                    titleIndex
++;
                }

            }

            
return result;
        }

     5:最后只需要根据定位把内容copy到新的word文档中然后保存就行了.

     word中可以使用Range方法,只需要传递开始和结束位置就可以返回一个Range对象,而这个对象就有copy的方法,我们把range copy到剪贴板中,然后在新建的文档中调用Paste方法然后再保存就成功把这部分提取到一个新的word中了。

Code

【结语】

     开始我用的遍历paragraph但对于大一点的文档超费时间,所以改成根据bookmark来定位。

     这个只是简单的操作word的一个例子,希望对大家有所帮助。

【完整源码】https://files.cnblogs.com/zrx401558287/splitword.rar