簡體   English   中英

OpenXML WordProcessingDocument到base64

[英]OpenXML WordProcessingDocument to base64

我已經基於模板創建了一個word文檔。

現在,我想獲取此新word文檔的base64字符串。

我發現了一些有關獲取內存流,然后將其轉換為base64的方法,但我不太了解它是如何工作的。

有什么想法可以在base64轉換此WordprocessingDocument doc嗎?

謝謝

最簡單的方法是

byte[] bytes= System.IO.File.ReadAllBytes(sFullFileName);
string sBase64Bytes = System.Convert.ToBase64String(bytes);

您不必使用內存流來加載字節並將其轉換為base64。但是,如果要更新內存中的某些內容並獲取內存中的base64更新,則可以嘗試以下一種方法:

          byte[] bytes = System.Convert.FromBase64String(sBase64Data);
          using (MemoryStream ms = new MemoryStream(bytes, true))
          {
              ms.Write(bytes, 0, bytes.Length);
              using (WordprocessingDocument doc = WordprocessingDocument.Open(ms,true))
              {
                  XmlDocument xCusDoc = OpenXmlHelper.GetCustomXmlDocument(doc);
                  sLayoutName = xCusDoc.DocumentElement.Attributes["name"].Value.ToString();
                  using (MemoryStream msw= new MemoryStream())
                  {
                      ms.WriteTo(msw);
                      bytes = msw.GetBuffer();
                  }
              }//end using (WordprocessingDocument doc
          }//end using (MemoryStream ms 

如果您想將其保存到物理文件中,則可以寫入字節。

System.IO.File.WriteAllBytes(sFileName, bytes);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM