创建docx word文档web api .net core 2.0
Create docx word document web api .net core 2.0
我正在 Asp.net 核心 2.0 中开发 Web API 项目。我需要一个库或创建 Word 文档的方法。我搜索了一个尝试过的 NPOI 和 DocX。两者都没有预期的那么好。谁能给我推荐一个工具?
乍一看,下面的 link 可以提供一些帮助:
https://asp.net-hacker.rocks/2017/02/23/word-document-formatter-in-aspnetcore.html
无论如何,关于 DOCX - 使用 Microsoft Open XML SDK 的 Word 文档 - 您可以使用 2.8.1 版将 Open XML SDK 安装到您的解决方案中。都是免费的。
文档位于:
在 GIT => https://github.com/OfficeDev/Open-XML-SDK ;
在 MSDN 上 => https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
ps:MSDN 文档都是关于版本 SDK 2.5 而不是 2.8.1,但是正如 GIT link 中所解释的,没有实质性的变化。
无论如何,要在 Web APP CORE 2.0 中使用它,您可以执行以下操作:
- 安装 Nuget Packgaes 版本 SDK 2.8.1
在您的控制器中引用以下命名空间:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
如果您只想将其保存在本地文件夹中,请执行以下操作:
public static void CreateWordprocessingDocument(string filepath)
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
}
}
并这样称呼它:
public IActionResult GenerateDocx()
{
string filePath = @"c:\word\Invoice.docx";
CreateWordprocessingDocument(filePath);
}
如果你想生成一个 docx 并从他们的浏览器保存到用户的计算机中,在点击 link 后,执行如下操作:
// GET verb
public IActionResult GenerateDocxBrowser()
{
// open xml sdk - docx
using (MemoryStream mem = new MemoryStream())
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
{
wordDoc.AddMainDocumentPart();
// siga a ordem
Document doc = new Document();
Body body = new Body();
// 1 paragrafo
Paragraph para = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
Justification justification1 = new Justification() { Val = JustificationValues.Center };
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
paragraphProperties1.Append(paragraphStyleId1);
paragraphProperties1.Append(justification1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run = new Run();
RunProperties runProperties1 = new RunProperties();
Text text = new Text() { Text = "The OpenXML SDK rocks!" };
// siga a ordem
run.Append(runProperties1);
run.Append(text);
para.Append(paragraphProperties1);
para.Append(run);
// 2 paragrafo
Paragraph para2 = new Paragraph();
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Normal" };
Justification justification2 = new Justification() { Val = JustificationValues.Start };
ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();
paragraphProperties2.Append(paragraphStyleId2);
paragraphProperties2.Append(justification2);
paragraphProperties2.Append(paragraphMarkRunProperties2);
Run run2 = new Run();
RunProperties runProperties3 = new RunProperties();
Text text2 = new Text();
text2.Text = "Teste aqui";
run2.AppendChild(new Break());
run2.AppendChild(new Text("Hello"));
run2.AppendChild(new Break());
run2.AppendChild(new Text("world"));
para2.Append(paragraphProperties2);
para2.Append(run2);
// todos os 2 paragrafos no main body
body.Append(para);
body.Append(para2);
doc.Append(body);
wordDoc.MainDocumentPart.Document = doc;
wordDoc.Close();
}
return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");
}
}
以上代码甚至展示了如何使用最重要的对象:正文、段落、运行和文本。请记住始终遵守此命令!
我正在 Asp.net 核心 2.0 中开发 Web API 项目。我需要一个库或创建 Word 文档的方法。我搜索了一个尝试过的 NPOI 和 DocX。两者都没有预期的那么好。谁能给我推荐一个工具?
乍一看,下面的 link 可以提供一些帮助:
https://asp.net-hacker.rocks/2017/02/23/word-document-formatter-in-aspnetcore.html
无论如何,关于 DOCX - 使用 Microsoft Open XML SDK 的 Word 文档 - 您可以使用 2.8.1 版将 Open XML SDK 安装到您的解决方案中。都是免费的。
文档位于:
在 GIT => https://github.com/OfficeDev/Open-XML-SDK ;
在 MSDN 上 => https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
ps:MSDN 文档都是关于版本 SDK 2.5 而不是 2.8.1,但是正如 GIT link 中所解释的,没有实质性的变化。
无论如何,要在 Web APP CORE 2.0 中使用它,您可以执行以下操作:
- 安装 Nuget Packgaes 版本 SDK 2.8.1
在您的控制器中引用以下命名空间:
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing;
如果您只想将其保存在本地文件夹中,请执行以下操作:
public static void CreateWordprocessingDocument(string filepath) { // Create a document by supplying the filepath. using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add some text. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument")); } }
并这样称呼它:
public IActionResult GenerateDocx()
{
string filePath = @"c:\word\Invoice.docx";
CreateWordprocessingDocument(filePath);
}
如果你想生成一个 docx 并从他们的浏览器保存到用户的计算机中,在点击 link 后,执行如下操作:
// GET verb public IActionResult GenerateDocxBrowser() { // open xml sdk - docx using (MemoryStream mem = new MemoryStream()) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true)) { wordDoc.AddMainDocumentPart(); // siga a ordem Document doc = new Document(); Body body = new Body(); // 1 paragrafo Paragraph para = new Paragraph(); ParagraphProperties paragraphProperties1 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" }; Justification justification1 = new Justification() { Val = JustificationValues.Center }; ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); paragraphProperties1.Append(paragraphStyleId1); paragraphProperties1.Append(justification1); paragraphProperties1.Append(paragraphMarkRunProperties1); Run run = new Run(); RunProperties runProperties1 = new RunProperties(); Text text = new Text() { Text = "The OpenXML SDK rocks!" }; // siga a ordem run.Append(runProperties1); run.Append(text); para.Append(paragraphProperties1); para.Append(run); // 2 paragrafo Paragraph para2 = new Paragraph(); ParagraphProperties paragraphProperties2 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Normal" }; Justification justification2 = new Justification() { Val = JustificationValues.Start }; ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties(); paragraphProperties2.Append(paragraphStyleId2); paragraphProperties2.Append(justification2); paragraphProperties2.Append(paragraphMarkRunProperties2); Run run2 = new Run(); RunProperties runProperties3 = new RunProperties(); Text text2 = new Text(); text2.Text = "Teste aqui"; run2.AppendChild(new Break()); run2.AppendChild(new Text("Hello")); run2.AppendChild(new Break()); run2.AppendChild(new Text("world")); para2.Append(paragraphProperties2); para2.Append(run2); // todos os 2 paragrafos no main body body.Append(para); body.Append(para2); doc.Append(body); wordDoc.MainDocumentPart.Document = doc; wordDoc.Close(); } return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx"); } }
以上代码甚至展示了如何使用最重要的对象:正文、段落、运行和文本。请记住始终遵守此命令!