この記事では、OpenXML SDKを使用する場合は、サイズ変更可能なMemoryStreamsを使用する必要があり、サンプルコードは正常に機能することを説明しています。
ただし、サンプルのC#コードをF#に変換すると、ドキュメントは変更されません。
open System.IO
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Wordprocessing
[<EntryPoint>]
let Main args =
let byteArray = File.ReadAllBytes "Test.docx"
use mem = new MemoryStream()
mem.Write(byteArray, 0, (int)byteArray.Length)
let para = new Paragraph()
let run = new Run()
let text = new Text("Newly inserted paragraph")
run.InsertAt(text, 0) |> ignore
para.InsertAt(run, 0) |> ignore
use doc = WordprocessingDocument.Open(mem, true)
doc.MainDocumentPart.Document.Body.InsertAt(para, 0) |> ignore
// no change to the document
use fs = new FileStream("Test2.docx", System.IO.FileMode.Create)
mem.WriteTo(fs)
0
を使用すると正常に動作しますWordprocessingDocument.Open("Test1.docx", true)
が、を使用したいと思いますMemoryStream
。私は何が間違っているのですか?