首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用VB.NET中的openxml方法从现有的XML文件数据中创建一个word文件?

如何使用VB.NET中的openxml方法从现有的XML文件数据中创建一个word文件?
EN

Stack Overflow用户
提问于 2019-03-12 18:02:42
回答 1查看 74关注 0票数 1

我有一个在运行时生成的XML文件,在我的program.This XMLfile.InnerText()上有一些格式(如-enter,空格,制表符,对齐),如下所示。

这是对windows驱动程序的测试。

2115 -XXXXXXXXXX分支机构XXXX

XXXXXXXXXX

XXXXXXXXXX XX

XXXXXXXXXX : 08/23/16 XXX XXX:

08/23/19 XXXXXXXX XXXXXX :9

XXXXXX XXX X

XXXX: XXXX 0,01

XXX XXX XXX

XXXXX:1

XXXXXXXXX

我正在尝试使用VB.net(以使用的代码为例)创建一个word文档与此XMLfile.InnerText(与上面的格式和对齐)。

代码语言:javascript
复制
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格式?

EN

回答 1

Stack Overflow用户

发布于 2019-03-13 06:34:47

当文件内容在Word应用程序中未打开时,Word UI/经典对象模型会自动执行许多操作,而这些操作在直接处理文件内容时不可用。InsertXML实质上是执行从XML到Word内容的转换。Open XML SDK中没有直接的等价物。

Open XML SDK所拥有的是AlternativeFormatImportPartAlternativeFormatImportPartType EnumaltChunk

根据Enum文档,XML是这种方法的有效文件格式。结果是否会与InsertXML相同,您必须进行测试。altChunk链接包含示例代码,您可以将其用作起点,我将其复制到此处以便于参考。

代码语言:javascript
复制
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:\Users\Public\Documents\Destination.docx";  
            string fileName2 = @"c:\Users\Public\Documents\Source.docx";  
            string testFile = @"c:\Users\Public\Documents\Test.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文档中。在该过程中删除所存储的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55118745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档