3
我正在編寫一個項目,它允許您直接從該站點編輯.docx文檔(我有一個需要編輯的模板)並將它們保存到你的電腦。 我在保存文件中已編輯的所有信息時遇到問題。使用OpenXML&MemoryStream在ASP.NET程序中編輯和保存.docx
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
var senderData = Sender_Textbox.Text;
var receiverData = Receiver_Textbox.Text;
var subjectData = Subject_Textbox.Text;
var contentData = Content_Textbox.Text;
docText = ReplaceData(docText, "receiver", receiverData);
docText = ReplaceData(docText, "sender", senderData);
docText = ReplaceData(docText, "subject", subjectData);
docText = ReplaceData(docText, "content", contentData);
using (StreamWriter sw = new StreamWriter(wDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
Session["ByteArray"] = memoryStream.ToArray();
}
static string ReplaceData(string docText, string oldText, string newText)
{
var regexText = new Regex(oldText);
return regexText.Replace(docText, newText);
}
我以前this教程從Office開發中心,它運作良好,在離線的解決方案。使用StreamWriter的部分不起作用,我下載了一個根本沒有改變的文檔。如何將我在線編輯的所有信息保存在wDoc中?