new*_*bie 2 c# docx openxml sql-server-openxml
我正在使用这种方法来生成docx文件:
public static void CreateDocument(string documentFileName, string text)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body><w:p><w:r><w:t>#REPLACE#</w:t></w:r></w:p></w:body>
</w:document>";
docXml = docXml.Replace("#REPLACE#", text);
using (Stream stream = mainPart.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
stream.Write(buf, 0, buf.Length);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力:
CreateDocument("test.docx", "Hello");
Run Code Online (Sandbox Code Playgroud)
但是,如果我想放置HTML内容而不是Hello?例如:
CreateDocument("test.docx", @"<html><head></head>
<body>
<h1>Hello</h1>
</body>
</html>");
Run Code Online (Sandbox Code Playgroud)
或类似这样的东西:
CreateDocument("test.docx", @"Hello<BR>
This is a simple text<BR>
Third paragraph<BR>
Sign
");
Run Code Online (Sandbox Code Playgroud)
两种情况都为创建了无效的结构document.xml。任何的想法?如何从HTML内容生成docx文件?
您不能只是将 HTML 内容插入到“document.xml”中,这部分只需要 WordprocessingML 内容,因此您必须将该 HTML 转换为 WordprocessingML,请参阅此。
您可以使用的另一件事是 altChunk 元素,通过它您可以在 DOCX 文件中放置一个 HTML 文件,然后在文档中的某个特定位置引用该 HTML 内容,请参阅此。
最后作为替代方案,使用GemBox.Document 库,您可以完全完成您想要的操作,请参阅以下内容:
public static void CreateDocument(string documentFileName, string text)
{
DocumentModel document = new DocumentModel();
document.Content.LoadText(text, LoadOptions.HtmlDefault);
document.Save(documentFileName);
}
Run Code Online (Sandbox Code Playgroud)
或者您实际上可以直接将 HTML 内容转换为 DOCX 文件:
public static void Convert(string documentFileName, string htmlText)
{
HtmlLoadOptions options = LoadOptions.HtmlDefault;
using (var htmlStream = new MemoryStream(options.Encoding.GetBytes(htmlText)))
DocumentModel.Load(htmlStream, options)
.Save(documentFileName);
}
Run Code Online (Sandbox Code Playgroud)
我意识到我迟到了7年。尽管如此,对于将来寻找如何将HTML转换为Word Doc的人们,Microsoft MSDN站点上的此博客文章提供了使用OpenXML进行此操作所需的大部分要素。我发现帖子本身令人困惑,但是他包含的源代码为我澄清了所有这些。
唯一缺少的部分是如何从头开始构建Docx文件,而不是如他的示例所示如何合并到现有文件中。我从这里发现了这个小事。
不幸的是,我在其中使用的项目是用vb.net编写的。因此,我将首先共享vb.net代码,然后将其自动进行c#转换,这可能是正确的,也可能是不正确的。
vb.net代码:
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Imports System.IO
Dim ms As IO.MemoryStream
Dim mainPart As MainDocumentPart
Dim b As Body
Dim d As Document
Dim chunk As AlternativeFormatImportPart
Dim altChunk As AltChunk
Const altChunkID As String = "AltChunkId1"
ms = New MemoryStream()
Using myDoc = WordprocessingDocument.Create(ms,WordprocessingDocumentType.Document)
mainPart = myDoc.MainDocumentPart
If mainPart Is Nothing Then
mainPart = myDoc.AddMainDocumentPart()
b = New Body()
d = New Document(b)
d.Save(mainPart)
End If
chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkID)
Using chunkStream As Stream = chunk.GetStream(FileMode.Create, FileAccess.Write)
Using stringStream As StreamWriter = New StreamWriter(chunkStream)
stringStream.Write("YOUR HTML HERE")
End Using
End Using
altChunk = New AltChunk()
altChunk.Id = altChunkID
mainPart.Document.Body.InsertAt(Of AltChunk)(altChunk, 0)
mainPart.Document.Save()
End Using
Run Code Online (Sandbox Code Playgroud)
C#代码:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
IO.MemoryStream ms;
MainDocumentPart mainPart;
Body b;
Document d;
AlternativeFormatImportPart chunk;
AltChunk altChunk;
string altChunkID = "AltChunkId1";
ms = new MemoryStream();
Using (myDoc = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
{
mainPart = myDoc.MainDocumentPart;
if (mainPart == null)
{
mainPart = myDoc.AddMainDocumentPart();
b = new Body();
d = new Document(b);
d.Save(mainPart);
}
chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkID);
Using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write)
{
Using (StreamWriter stringStream = new StreamWriter(chunkStream))
{
stringStream.Write("YOUR HTML HERE");
}
}
altChunk = new AltChunk();
altChunk.Id = altChunkID;
mainPart.Document.Body.InsertAt(Of, AltChunk)[altChunk, 0];
mainPart.Document.Save();
}
Run Code Online (Sandbox Code Playgroud)
请注意,我ms在另一个例程中使用了内存流,该例程在使用后将被丢弃。
我希望这可以帮助其他人!