&tag(OpenXMLSDK/Word);
*目次 [#na01e235]
#contents
*関連ページ [#p0abaab5]
-[[OpenXMLSDK]]
*参考情報 [#w0998208]

*docxファイルの構造 [#ze202724]
-docxファイルはzipファイルなので、docxファイルの拡張子をzipに変更すると展開することができる。
-内部はパーツにわかれたxmlファイルの集合。
-全体をプログラムで生成したり既存docxファイルの一部分を書き換えたりすることができる。

*単純なdocxファイルの生成 [#td5860f5]
-VisualStudio2010でコンソールアプリを新規作成する。
-[参照の追加]ダイアログの参照タブで C:\Program Files (x86)\Open XML SDK\V2.0\lib フォルダの DocumentFormat.OpenXml.dll を追加する。
-Main.csに次のコードを記述する。
#pre{{
namespace OpenXMLDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var package = WordprocessingDocument.Create(
                "test.docx", WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainDocumentPart1 = package.AddMainDocumentPart();
                Document document1 = new Document();
                Body body1 = new Body();
                Paragraph paragraph1 = new Paragraph();

                Run run1 = new Run();
                run1.Append(new Text() { Text = "a+b=c" });
                paragraph1.Append(run1);
                body1.Append(paragraph1);
                document1.Append(body1);
                mainDocumentPart1.Document = document1;
            }
        }
    }
}
}}
-実行すると"test.docx"が作成される。
*複雑なdocxの生成 [#n51fecbc]
-Open XML SDK 2.0 Productivity Toolを実行し、[ドキュメントエクスプローラー]ペインにdocxファイルをドロップすると構造を表示してくれる。ツールバーの[コードの反映]をクリックすると、docxを生成するのに必要なプログラムコードが表示される。
-同ツールでSDKのドキュメントも表示できる。
-これらを駆使してwordファイルを作成する。

*パーツの置換 [#dd819989]
-一からdocxファイルを生成するのではなく、既存docxを置換する方法もある。
-[[方法 : Office オープン XML 形式のドキュメントを操作する:http://msdn.microsoft.com/ja-jp/library/aa982683(v=office.12).aspx]]に、"Office オープン XML 形式のドキュメントをプログラムで操作する"という項目があり、これが参考になる。
-上記記事のプログラム。docxファイルをPackageで操作し、docx中のstyles.xmlを新しいstyles.xmlで置き換えている。
#pre{{
    class Program
    {
        private static String stylePath = @"d:\WordOpenXMLFormatSample\NewStylePart\styles.xml";
        private static String packagePath = @"d:\WordOpenXMLFormatSample\SampleWordDocument.docx.zip";

        private static void CopyStream(Stream source, Stream target)
        {
            const int bufSize = 0x1000;
            // const int bufSize = 1024;
            byte[] buf = new byte[bufSize];
            int bytesRead = 0;
            while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
            {
                target.Write(buf, 0, (int)bytesRead);
            }
            source.Close();
            target.Close();
        }


        static void SwapStylePart(String packagePath, String stylePath)
        {
            using (Package package = Package.Open(packagePath, FileMode.Open, FileAccess.ReadWrite))
            {
                Uri uriPartTarget = new Uri("/word/styles.xml", UriKind.Relative);
                package.DeletePart(uriPartTarget);
                PackagePart packagePartReplacement = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml");
                using (FileStream fileStream = new FileStream(stylePath, FileMode.Open,
                    FileAccess.Read))
                {
                    // Load the new styles.xml using a stream.
                    CopyStream(fileStream, packagePartReplacement.GetStream());
                }
            }

        }

        static void Main(string[] args)
        {
            SwapStylePart(packagePath, stylePath);
        }
    }
}}



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS