Ras*_*tko 5 c# ms-word openxml openxml-sdk
我已经使用 OpenXML 在 C# 中生成了 Word/docx 文档。使用 altChunk 添加/合并了另一份 docx 文档。当您打开该文档时,它看起来很棒。但进一步我需要使用 C# 代码处理该文档。我看到仍然存在 altChunk,它实际上具有指向合并文档的二进制序列化内容的指针。如果您在 Word - SaveAs 中执行此操作,并将文档保存到新文件并反映该文档(OpenXML 工具或 VS 扩展),那么您会发现 Word 引擎已将 altChunk 转换为段落。那太棒了。但我无法通过 C# 代码实现这一点。
如果我复制到新文件并保存,则什么也不会发生:
public static void SaveAgain(string masterDocumentPath)
{
string newDocumentPath = @"F:\test.docx";
using (Stream original = new FileStream(masterDocumentPath, FileMode.Open))
{
using (FileStream fs = File.Create(newDocumentPath))
{
original.CopyTo(fs);
}
}
using (WordprocessingDocument myDocNew =
WordprocessingDocument.Open(newDocumentPath, true))
{
myDocNew.MainDocumentPart.Document.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
我在构建代码时使用了这篇文章(如何使用 altChunk 进行文档组装)。我缺少什么?
更新
我已经通过 Interop 成功地做到了这一点。这是片段:
public static string SaveAsWithInterop(string masterDocumentPath)
{
string directoryPath = Path.GetDirectoryName(masterDocumentPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(masterDocumentPath);
string fileExtension = Path.GetExtension(masterDocumentPath);
string filenNameToSave = string.Concat(fileNameWithoutExtension, "Saved", fileExtension);
object savedFilePath = Path.Combine(directoryPath, filenNameToSave);
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
object missing = Missing.Value;
object isReadOnly = false;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(masterDocumentPath);
doc.SaveAs(ref savedFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Close();
return savedFilePath.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我仍然想知道是否可以使用 OpenXML 来做到这一点。我想避免互操作。
| 归档时间: |
|
| 查看次数: |
1977 次 |
| 最近记录: |