开发者

Reading Word Documents stored in Oracle DB as a BLOB object using C#

开发者 https://www.devze.com 2023-01-30 05:48 出处:网络
We store a word document in an Oracle 10g database as a BLOB object. I want to read the contents (the text) of this word document, make some changes, and write the text alone to a different field in a

We store a word document in an Oracle 10g database as a BLOB object. I want to read the contents (the text) of this word document, make some changes, and write the text alone to a different field in a C# code.

How do I do this in C# 2.0?

The easiest logic that I came up with is this -开发者_如何学Go

  1. Read the BLOB object
  2. Store it in the FileSystem
  3. Extract the text contents
  4. Do your job
  5. Write the text into a separate field.

I can use Word.dll but not any commercial solutions such as Aspose


I assume that you already know how to do steps 1 and 2 (use the Oracle.DataAccess and System.IO namespaces).

For step 3 and 5, use Word Automation. This MS support article shows you how to get started: How to automate Microsoft Word to create a new document by using Visual C#

If you know what version of Word it will be, then I'd suggest using early binding, otherwise use late binding. More details and sample code here: Using early binding and late binding in Automation

Edit: If you don't know how to use BLOBs from C#, take a look here: How to: Read and Write BLOB Data to a Database Table Through an Anonymous PL/SQL Block


This keeps coming up in my searches, so I'll add an answer for the benefit of future readers.

I highly recommend avoiding Word automation. It's painfully slow and subjects you to the whims of Microsoft's developers with each upgrade. Instead, process the files manually yourselves if you can. The files are nothing but zipped archives of XML files and resources (such as images embedded in the document).

In this case, you'd simply unzip the docx using your preferred library, manipulate the XML, and then zip the result back up.

This does require the use of docx files rather than doc files, but as the link above explains, this has been the default Word format since Office 2007 and shouldn't present an issue unless your users are desperately clinging to the past.

For an example of the time savings, Back in 2007 we converted one process that took 45 minutes using Word automation and, on the same hardware, it took 15 SECONDS processing the files manually. To be clear, I'm not blaming Microsoft for this - their Word automation methods don't know how you will manipulate the document, so they have to anticipate and track everything that you could possibly change. You, on the other hand, can write your method with laser focus because you know exactly what you want to do.

0

精彩评论

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

关注公众号