C# 尝试从dot net core mvc使用open xml下载word文档时出错
我正试图通过在控制器内部编写代码来使用dotnetcore中的openxml插件生成word文档,如下所示C# 尝试从dot net core mvc使用open xml下载word文档时出错,c#,.net,ms-word,openxml,openxml-sdk,C#,.net,Ms Word,Openxml,Openxml Sdk,我正试图通过在控制器内部编写代码来使用dotnetcore中的openxml插件生成word文档,如下所示 public IActionResult CreateDocument() { using (MemoryStream mem = new MemoryStream()) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(me
public IActionResult CreateDocument()
{
using (MemoryStream mem = new MemoryStream())
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
{
wordDoc.AddMainDocumentPart();
Document doc = new Document();
//Body body = new Body();
/// Add header
MainDocumentPart mainDocPart = wordDoc.MainDocumentPart;
HeaderPart headerPart1 = mainDocPart.AddNewPart<HeaderPart>("r97");
Header header1 = new Header();
Paragraph paragraph1 = new Paragraph() { };
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "This is Header Part";
run1.Append(text1);
paragraph1.Append(run1);
header1.Append(paragraph1);
headerPart1.Header = header1;
// getting an error here in this line
SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault();
if (sectionProperties1 == null)
{
sectionProperties1 = new SectionProperties() { };
mainDocPart.Document.Body.Append(sectionProperties1);
}
HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" };
sectionProperties1.InsertAt(headerReference1, 0);
.......
........
.........
}
}
}
public IActionResult CreateDocument()
{
使用(MemoryStream mem=new MemoryStream())
{
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Create(mem,WordprocessingDocumentType.Document,true))
{
wordDoc.AddMainDocumentPart();
单据单据=新单据();
//车身=新车身();
///添加标题
MainDocumentPart mainDocPart=wordDoc.MainDocumentPart;
HeaderPart headerPart1=mainDocPart.AddNewPart(“r97”);
收割台收割台1=新收割台();
第1段=新的第()段{};
运行run1=新运行();
Text text1=新文本();
text1.Text=“这是标题部分”;
run1.Append(text1);
第1段追加(第1行);
标题1.附加(第1段);
headerPart1.Header=header1;
//在此行中获取错误
SectionProperties sectionProperties1=mainDocPart.Document.Body.Subjections().FirstOrDefault();
如果(sectionProperties1==null)
{
sectionProperties1=新SectionProperties(){};
mainDocPart.Document.Body.Append(sectionProperties1);
}
HeaderReference headerReference1=新的HeaderReference(){Type=HeaderFooterValues.Default,Id=“r97”};
sectionProperties1.插入(headerReference1,0);
.......
........
.........
}
}
}
但是如何在这一行SectionProperties sectionProperties1=mainDocPart.Document.Body.subjections().FirstOrDefault()中获取错误代码>
**错误:**DocumentFormat.OpenXml.Packaging.MainDocumentPart.Document.get返回空值。
我不确定我在这段代码上哪里做错了,有人能提出一些关于这段代码的想法吗
非常感谢您在这一行实例化了mainDocPart
变量:
MainDocumentPart mainDocPart=wordDoc.MainDocumentPart代码>
然后您没有在其中添加任何文档。这就是出现错误的原因:DocumentFormat.OpenXml.Packaging.MainDocumentPart.Document.get返回空值。
如何解决这个问题:
mainDocPart.Document = doc; //you define earlier but do not used.
doc.Body = new Body();
SectionProperties sectionProperties1 =
mainDocPart.Document.Body.Descendants<SectionProperties>()?.FirstOrDefault();
if (sectionProperties1 == null)
{
sectionProperties1 = new SectionProperties() { };
mainDocPart.Document.Body.Append(sectionProperties1);
}
mainDocPart.Document=doc//您可以在前面定义,但不使用。
doc.Body=新的Body();
SectionProperties sectionProperties1=
mainDocPart.Document.Body.subjects()?.FirstOrDefault();
如果(sectionProperties1==null)
{
sectionProperties1=新SectionProperties(){};
mainDocPart.Document.Body.Append(sectionProperties1);
}
您可以在此文档页面中获得更多信息:您在此行实例化了mainDocPart
变量:
MainDocumentPart mainDocPart=wordDoc.MainDocumentPart代码>
然后您没有在其中添加任何文档。这就是出现错误的原因:DocumentFormat.OpenXml.Packaging.MainDocumentPart.Document.get返回空值。
如何解决这个问题:
mainDocPart.Document = doc; //you define earlier but do not used.
doc.Body = new Body();
SectionProperties sectionProperties1 =
mainDocPart.Document.Body.Descendants<SectionProperties>()?.FirstOrDefault();
if (sectionProperties1 == null)
{
sectionProperties1 = new SectionProperties() { };
mainDocPart.Document.Body.Append(sectionProperties1);
}
mainDocPart.Document=doc//您可以在前面定义,但不使用。
doc.Body=新的Body();
SectionProperties sectionProperties1=
mainDocPart.Document.Body.subjects()?.FirstOrDefault();
如果(sectionProperties1==null)
{
sectionProperties1=新SectionProperties(){};
mainDocPart.Document.Body.Append(sectionProperties1);
}
您可以在此文档页面中获得更多信息:要添加到Lutti的答案中,您需要了解最小的WordprocessingDocument
由MainDocumentPart
组成,其内容至少如下:
因此,当您创建一个“空”文档时,您至少应该有以下代码:
使用(var stream=new MemoryStream())
使用(WordprocessingDocument wordDocument=WordprocessingDocument.Create(stream,WordprocessingDocumentType.Document))
{
MainDocumentPart MainDocumentPart=wordDocument.AddMainDocumentPart();
mainDocumentPart.Document=新文档(新正文());
}
注意w:document
和w:body
元素与document
和body
实例的对应关系
以创建标头为例,您通常可以通过组合构造函数来简化代码,从而创建类似XML标记的结构,如下所示:
var headerPart=mainDocumentPart.AddNewPart();
标题部分标题=
新标题(
新段落(
新运行(
新文本(“这是标题部分”);
在上面的代码示例中,我将代码布局为您将格式化相应的开放XML标记:
这是标题部分
在我看来,这更容易阅读,如果您理解创建所需结果所需的开放XML标记,则有助于避免错误。要补充Lutti的答案,您需要了解,最小的WordprocessingDocument
由MainDocumentPart
组成,其内容至少包括以下内容:
因此,当您创建一个“空”文档时,您至少应该有以下代码:
使用(var stream=new MemoryStream())
使用(WordprocessingDocument wordDocument=WordprocessingDocument.Create(stream,WordprocessingDocumentType.Document))
{
MainDocumentPart MainDocumentPart=wordDocument.AddMainDocumentPart();
mainDocumentPart.Document=新文档(新正文());
}
注意信件