1、创建Word
public class OpenXmlWordUtility
{
/// <summary>
/// 文件全路径
/// </summary>
public string FilePath { get; private set; }
/// <summary>
/// 处理文档
/// </summary>
public WordprocessingDocument ProcessingDocument { get; private set; }
/// <summary>
/// 文档
/// </summary>
public Document Document
{
get
{
return this.ProcessingDocument.MainDocumentPart.Document;
}
}
/// <summary>
/// 打开文档
/// </summary>
public void OpenDocument()
{
this.ProcessingDocument = WordprocessingDocument.Open(this.FilePath, true);
this.TitleParagraphs = this.GetTitleParagraphs();
}
/// <summary>
/// 创建Word文档
/// </summary>
public void CreateDocument()
{
this.ProcessingDocument = WordprocessingDocument.Create(this.FilePath, WordprocessingDocumentType.Document);
MainDocumentPart mainDocumentPart = this.ProcessingDocument.AddMainDocumentPart();
mainDocumentPart.Document = new Document(new Body());
Body body = mainDocumentPart.Document.Body;
SectionProperties sectionProperties = new SectionProperties();
PageSize pageSize = new PageSize();
PageMargin pageMargin = new PageMargin();
DocumentFormat.OpenXml.Wordprocessing.Columns columns = new DocumentFormat.OpenXml.Wordprocessing.Columns() { Space = "220" }; //720
DocGrid docGrid = new DocGrid() { LinePitch = 100 }; //360
sectionProperties.Append(pageSize, pageMargin, columns, docGrid);
body.Append(sectionProperties);
mainDocumentPart.Document.Save();
}
/// <summary>
/// 保存文档
/// </summary>
public void SaveDocument()
{
this.ProcessingDocument.MainDocumentPart.Document.Save();
}
/// <summary>
/// 关闭文档
/// </summary>
public void CloseDocument()
{
if (this.ProcessingDocument != null)
{
this.ProcessingDocument.Close();
this.ProcessingDocument.Dispose();
}
}
/// <summary>
/// 清除指定标题下的所有内容
/// </summary>
public void ClearTitleContent(WordTitle title)
{
var index = this.WordTitles.IndexOf(title);
//title.Paragraph.NextSibling().RemoveAllChildren();
//return;
OpenXmlElement startElement = title.Paragraph.NextSibling();
OpenXmlElement stopElement = null;
if (index != this.WordTitles.Count - 1)
{
var tempLt = this.WordTitles.Skip(index + 1);
var tagTitle = tempLt.FirstOrDefault(p => p.Level == title.Level);
if (tagTitle != null)
{
stopElement = tagTitle.Paragraph;
OpenXmlElement tempElement = startElement;
List<OpenXmlElement> openxmleList = new List<OpenXmlElement>();
while (tempElement != stopElement)
{
openxmleList.Add(tempElement);
tempElement = tempElement.NextSibling();
}
foreach (var openXmlElement in openxmleList)
{
openXmlElement.Remove();
}
}
else // 等于null 通常是最后一个章节
{
stopElement = null;
OpenXmlElement tempElement = startElement;
List<OpenXmlElement> openxmleList = new List<OpenXmlElement>();
while (tempElement != stopElement)
{
openxmleList.Add(tempElement);
tempElement = tempElement.NextSibling();
}
foreach (var openXmlElement in openxmleList)
{
openXmlElement.Remove();
}
}
}
}
/// <summary>
/// 创建纵向页分节符
/// </summary>
/// <returns></returns>
public Paragraph CreatePortraitSectionBreakParagraph()
{
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00052B73", RsidRunAdditionDefault = "00052B73" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00052B73" };
// PageSize pageSize1 = new PageSize() { Width = (UInt32Value)16838U, Height = (UInt32Value)11906U, Orient = PageOrientationValues.Landscape };
PageSize pageSize1 = new PageSize();
//PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U };
DocumentFormat.OpenXml.Wordprocessing.Columns columns1 = new DocumentFormat.OpenXml.Wordprocessing.Columns() { Space = "425" };
DocGrid docGrid1 = new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 };
sectionProperties1.Append(pageSize1);
// sectionProperties1.Append(pageMargin1);
sectionProperties1.Append(columns1);
sectionProperties1.Append(docGrid1);
paragraphProperties1.Append(sectionProperties1);
paragraph1.Append(paragraphProperties1);
return paragraph1;
}
/// <summary>
/// 获取新段落
/// </summary>
/// <param name="text">段落文本</param>
/// <returns>段落</returns>
public Paragraph CreateParagraph(string text)
{
Paragraph paragraph = new Paragraph();
Run run = new Run();
run.Append(new Text(text));
paragraph.Append(run);
return paragraph;
}
}
2、新增标题并居中显示
OpenXmlWordUtility wordUtility = new OpenXmlWordUtility();
DocumentFormat.OpenXml.Wordprocessing.Paragraph par = this.WordUtility.CreateParagraph("1"); //新建标题1
par.PrependChild(new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties());
DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties pPr2 = par.ParagraphProperties;
// 设置StyleId
if (pPr2.ParagraphStyleId == null)
pPr2.ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId();
pPr2.ParagraphStyleId.Val = "1";
if (pPr2.Justification== null)
pPr2.Justification= new DocumentFormat.OpenXml.Wordprocessing.Justification(){ Vall = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Center};
this.WordUtility.Document.Body.Append(par);
StyleDefinitionsPart styleDefinitionsPart1 = this.WordUtility.Document.MainDocumentPart.AddNewPart<StyleDefinitionsPart>("rId1");
GeneratedCode.GeneratedClass.GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);
this.WordUtility.SaveDocument();
this.WordUtility.CloseDocument();