开发者

C# microsoft word manipulation

I already added the microsoft reference in. But basically what I want to do is read the whole document and go line by line a开发者_高级运维nd if the line contains $, split based on $ and do some simple int's.

For the most part I want to do this while keeping the document in the exact formating it's in. Or simply put I want to do

string myworddoc = File.ReadAllLines(@"C:\doc.docx");
foreach(string myword in myworddoc)

but how do I do that with a word document?


I'm a little unclear what it is you're trying to accomplish here, but manipulating Word documents isn't terrible. You've got two ways to go.

The first is to use Word interop to manipulate the document. Word interop opens an instance of MS Word and exposes an object model for you to manipulate the document itself. This is a pain, but sometimes it's what you have to do. Here's an example:

ApplicationClass ap = new ApplicationClass();
Document doc = ap.Documents.Open(@"C:\doc.docx");

From here you can use the doc object to access the Contents property. That'll give you things like Text and Find.

Personally, I find the Interop cumbersome. Fortunately, it looks like you're working with 2007 or higher and your documents are formatted using OpenXML (the .docx extension indicates this). In this case, you can use the Open XML SDK (downloadable from Microsoft) to work with your Word document. This allows you to manipulate things without starting the MS Word process. Here's an Example (note that I'm using a number of different namespaces here--mainly DocumentFormat.OpenXml.Packaging and DocumentFormat.OpenXml.Wordprocessing).

WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\doc.docx", false);
foreach(Text textsection in doc.MainDocumentPart.Document.Descendants<Text>())
{
    // do something here
}


you can use an object library as doc files contain additional header.....

try this link : http://www.c-sharpcorner.com/UploadFile/Globalking/fileAccessingusingcsharp02242006050207AM/fileAccessingusingcsharp.aspx


It seems you are using Microsoft Office 2007 or later. This means you can do it easily with OpenXML.

Visit this Page

Manipulating Word 2007 Files with OpenXML

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜