我在我的program上运行时间生成了一个XML文件。此xmlfile.innertext()具有某些格式(例如-enter,space,space,tab,acorments),如下/p>
这是Windows驱动程序的测试
2115 -xxxxxxxxxx分支xxxx
xxxxxxxxxx xxxxxxxxxxx
XXXXXXXXXX XX
xxxxxxxxxxx:08/23/16xxx xxx:
08/23/19xxxxxxxx xxxxxx:9
xxxxxx xxx x
xxxx:xxx 0,01
xxxxxx xxx
xxxxx:1
xxxxxxxxx
我正在尝试使用此xmlfile.innertext创建一个Word文档(使用上述格式& Alignments)使用vb.net(提供了使用的代码为示例)。
Dim vWordDoc As WordprocessingDocument = WordprocessingDocument.Create(tmppath, WordprocessingDocumentType.Document)
' Set the content of the document so that Word can open it.
Dim vMainPart As MainDocumentPart = vWordDoc.AddMainDocumentPart()
' Create the document structure and add some text.
vMainPart.Document = New Document()
Dim vBody As Body = vMainPart.Document.AppendChild(New Body())
Dim vPara As Paragraph = vBody.AppendChild(New Paragraph())
Dim run As Run = vPara.AppendChild(New Run())
run.AppendChild(New Text(XMLFile.InnerText))
vMainPart.Document.Save()
vWordDoc.Close()
通过使用Microsoft.Office.Interop.Word.Application -> Range.InsertXML()
方法,我可以在Word文件中带上相同的格式。
但是我想使用OpenXML方法在我新创建的Word文件中捕获相同的XML格式?
单词UI/Classic Object模型自动化了许多直接与文件内容中未打开时无用的内容。InsertXML
本质上执行XML转换为单词内容。开放XML SDK中没有直接等效的。
开放的XML SDK确实具有替代性FormatiMportPart,替代性FormatimportParttype Enum和Altchunk。
根据枚举文档,XML是此方法的有效文件格式。结果是否与InsertXML
相同,您必须测试。Altchunk链接包含您可以用于起点的示例代码,我在此处复制以供参考。
using System.Linq;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace altChunk
{
class Program
{
static void Main(string[] args)
{
string fileName1 = @"c:UsersPublicDocumentsDestination.docx";
string fileName2 = @"c:UsersPublicDocumentsSource.docx";
string testFile = @"c:UsersPublicDocumentsTest.docx";
File.Delete(fileName1);
File.Copy(testFile, fileName1);
using (WordprocessingDocument myDoc =
WordprocessingDocument.Open(fileName1, true))
{
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk =
mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (FileStream fileStream = File.Open(fileName2, FileMode.Open))
chunk.FeedData(fileStream);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document
.Body
.InsertAfter(altChunk, mainPart.Document.Body
.Elements<Paragraph>().Last());
mainPart.Document.Save();
}
}
}
}
这种方法是将外部内容存储为ZIP软件包的一部分。当Word打开文件时,它执行转换,将内容集成到Word文档中。在此过程中删除了存储的内容。