-2

データ接続を使わずに Microsoft Word ファイルを読みたいのですが、

4

4 に答える 4

2

「.doc」は単純なテキスト ベースのファイル形式ではありません。操作には相互運用を使用する必要があります。

COM ライブラリ "Microsoft Word 12.0 Object Library" を含めます。ApplicationClass を作成し、Documents プロパティを使用してドキュメントを開きます。

        object wordPath = null;
        object missing = System.Reflection.Missing.Value;

        wordPath = @"C:\sample.doc";

        // Create Interop object
        ApplicationClass word = new ApplicationClass();
        word.Visible = false;

        // Open document
        Document doc = word.Documents.Open(ref wordPath,
            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);

        // Set document as active for interaction
        doc.Activate();

        // Select the whole content of the word document
        word.Selection.WholeStory();

        // Get the text from the document
        string text = word.Selection.Text;

Scott C. Reynoldsのブログに非常に優れた紹介があります。

于 2009-03-14T13:34:19.883 に答える
1

Word ドキュメントは .doc 拡張子を使用し、オプションで XML ベースの形式で保存できます。代わりにそれを行うオプションがある場合は、XML 解析ライブラリを使用してコンテンツを取得できます。スキーマ全体はかなり複雑ですが、いくつかの有用なものを簡単な方法で抽出できる場合があります。

于 2009-03-14T10:20:14.287 に答える
1

「ドキュメント」が「Word 2003 ドキュメント」を意味する場合、それはプレーン テキスト ファイルではなく、バイナリ ファイル形式です。どこかに文書化されているかどうかはわかりませんが、明らかに Open Office のようなプロジェクトがリバース エンジニアリングを行っています。

「ドキュメント」が別の意味である場合は、明確にしてください。

于 2009-03-14T09:50:50.020 に答える
0
object wordPath = null;
    object missing = System.Reflection.Missing.Value;

wordPath = @"C:\sample.doc";

// Create Interop object
ApplicationClass word = new ApplicationClass();
word.Visible = false;

// Open document
Document doc = word.Documents.Open(ref wordPath,
                                   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);

// Set document as active for interaction
doc.Activate();

// Select the whole content of the word document
word.Selection.WholeStory();

// Get the text from the document
string text = word.Selection.Text;
于 2009-03-18T09:53:30.153 に答える