openxml sdk java_ASP.NET Core使用Web API通过Open XML SDK生成Word文件模板

本文档介绍了如何在ASP.NET Core项目中使用Web API和Open XML SDK创建并保存Word文件模板。首先,通过Nuget安装DocumentFormat.OpenXml包,然后引用必要的命名空间。接着展示了创建Word文件模板的代码,包括保存到服务器和直接输出到浏览器的方法。示例代码详细解释了如何添加文本和格式化内容到Word文档。
摘要由CSDN通过智能技术生成

1、使用Open XML SDK的文档

2、Nuget安装引用DocumentFormat.OpenXml

1)使用Nuget界面管理器

搜索"DocumentFormat.OpenXml",在列表中找到它,点击"安装"

2)使用Package Manager命令安装PM> Install-Package DocumentFormat.OpenXml

3)使用.NET CLI命令安装> dotnet add TodoApi.csproj package DocumentFormat.OpenXml

3、引用如下命名空间using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

4、生成Word文件模板保存在服务器的方法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);

}

5、生成Word文件模板输出到浏览器的方法// 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");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值