C# 拆分word(根据标题或书签拆分)

【实现功能】

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

【开发环境】

     vs2005+office2003

【实现过程】

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

     2:首先读取word文档

ContractedBlock.gif ExpandedBlockStart.gif Code
        ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();

ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 读取word文档
        
/// </summary>
        
/// <param name="path">word文档路径</param>
        
/// <returns></returns>

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

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

            
return doc;
        }

 

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

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
        
/// 创建word文档
        
/// </summary>
        
/// <returns></returns>

        private Document CreateDocument()
ExpandedBlockStart.gifContractedBlock.gif        
{
            
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];来记录这九个书签的开始和结束位置.

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
        
/// 获取书签的位置
        
/// </summary>
        
/// <param name="word">文档对象</param>
        
/// <returns></returns>

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

            }

            
return result;
        }

     

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
        
/// 拆分word
        
/// </summary>

        private void CovertWord()
ExpandedBlockStart.gifContractedBlock.gif        
{
            
object missing = System.Reflection.Missing.Value;

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string path = "c:\\1134";
                Directory.CreateDirectory(path);

                Document doc 
= ReadDocument("c:\\a.doc");


                
int[,] positions = GetPosition(doc);


                
object oStart = 0;
                
object oEnd = 0;
                
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (i != 9)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        oEnd 
= positions[i, 0];
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        oEnd 
= doc.Content.End;
                    }


                    Range tocopy 
= doc.Range(ref oStart,ref oEnd);
                    tocopy.Copy();

                    Document docto 
= CreateDocument();
                    docto.Content.Paste();

                    
object filename = path + "\\" + i.ToString() + ".doc";
                    docto.SaveAs(
ref filename, ref missing, ref missing, ref missing, ref missing,
                        
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        
ref missing, ref missing, ref missing, ref missing, ref missing);
                    docto.Close(
ref missing, ref missing, ref missing);

                    oStart 
= oEnd;
                }

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                word.Quit(
ref missing, ref missing, ref missing);
            }



        }

 

【结语】

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

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

【完整源码】

      /Files/zrx401558287/splitword.rar 

 

转载于:https://www.cnblogs.com/zrx401558287/archive/2009/03/06/1404606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值