这是我用来使用Openxml生成word文档的一段代码,虽然我使用的是字符编码UTF-8但它没有帮助,但是它不能支持阿拉伯语。
感谢您的帮助,谢谢!
private void AddParts(WordprocessingDocument parent,string strHeader,string strBody, string strFooter)
{
var mainDocumentPart = parent.AddMainDocumentPart();
GenerateMainDocumentPart().Save(mainDocumentPart);
var documentSettingsPart =
mainDocumentPart.AddNewPart
<DocumentSettingsPart>("rId1");
GenerateDocumentSettingsPart().Save(documentSettingsPart);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<html><head></head><body>" + strBody + "</body></html>"));
AlternativeFormatImportPart formatImportPart = mainDocumentPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, "body");
// Feed HTML data into format import part (chunk).
formatImportPart.FeedData(ms);
AltChunk altChunk = new AltChunk();
altChunk.Id = "body";
mainDocumentPart.Document.Body.Append(altChunk);
AddHeaderAndFooter(mainDocumentPart, strHeader, strFooter);
}
private Document GenerateMainDocumentPart()
{
var element =
new Document(
new Body(
new Paragraph(
new Run(
new Text(""))
),
new SectionProperties(
new HeaderReference()
{
Type = HeaderFooterValues.First,
Id = "rId2"
},
new FooterReference()
{
Type = HeaderFooterValues.First,
Id = "rId3"
},
new HeaderReference()
{
Type = HeaderFooterValues.Default,
Id = "rId6"
},
new FooterReference()
{
Type = HeaderFooterValues.Default,
Id = "rId7"
},
new PageMargin()
{
Top = 1440,
Right = (UInt32Value)1440UL,
Bottom = 1440,
Left = (UInt32Value)1440UL,
Header = (UInt32Value)720UL,
Footer = (UInt32Value)720UL,
Gutter = (UInt32Value)0UL
},
new TitlePage()
)));
return element;
}
private Settings GenerateDocumentSettingsPart()
{
var element =
new Settings(new EvenAndOddHeaders());
return element;
}
private void AddHeaderAndFooter(MainDocumentPart mainPart,string strHeader,string strFooter)
{
// Delete the existing header and footer parts
mainPart.DeleteParts(mainPart.HeaderParts);
mainPart.DeleteParts(mainPart.FooterParts);
// Create a new header and footer parts for first and default pages
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
//HeaderPart headerPart2 = mainPart.AddNewPart<HeaderPart>();
FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
HeaderPart defaultHeaderPart = mainPart.AddNewPart<HeaderPart>();
FooterPart defaultFooterPart = mainPart.AddNewPart<FooterPart>();
GenerateHeaderPartContentFirst(headerPart,strHeader);
GenerateHeaderPartContent(defaultHeaderPart);
// GenerateHeaderPart2Content(headerPart);
GenerateFooterPartContentFirst(footerPart,strFooter);
GenerateFooterPartContent(defaultFooterPart);
//string rId = mainPart.GetIdOfPart(headerPart);
// Get Id of the headerPart and footer parts
string headerPartId = mainPart.GetIdOfPart(headerPart);
string footerPartId = mainPart.GetIdOfPart(footerPart);
string defaultHeaderPartId = mainPart.GetIdOfPart(defaultHeaderPart);
string defaultFooterPartId = mainPart.GetIdOfPart(defaultFooterPart);
// Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
IEnumerable<SectionProperties> sections = mainPart.Document.Body.Elements<SectionProperties>();
foreach (var section in sections)
{
// Delete existing references to headers and footers
section.RemoveAllChildren<HeaderReference>();
section.RemoveAllChildren<FooterReference>();
section.RemoveAllChildren<TitlePage>();
// Create the new header and footer reference node
section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId, Type = HeaderFooterValues.First });
section.PrependChild<HeaderReference>(new HeaderReference() { Id = defaultHeaderPartId, Type = HeaderFooterValues.Default });
//section.PrependChild<HeaderReference>(new HeaderReference() { Id = rId, Type=HeaderFooterValues.First });
section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId, Type = HeaderFooterValues.First });
section.PrependChild<FooterReference>(new FooterReference() { Id = defaultFooterPartId, Type = HeaderFooterValues.Default });
section.PrependChild<TitlePage>(new TitlePage());
}
mainPart.Document.Save();
}
private void GenerateHeaderPartContent(HeaderPart part)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
Paragraph paragraph2 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Default Header";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
part.Header = header1;
}
private void GenerateHeaderPartContentFirst(HeaderPart part,string strHeader)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
Paragraph paragraph2 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
paragraph1.Append(paragraphProperties1);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strHeader));
AlternativeFormatImportPart formatImportPart = part.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, "heder");
formatImportPart.FeedData(ms);
AltChunk chunk = new AltChunk() { Id = "heder" };
header1.Append(chunk);
part.Header = header1;
}
private void GenerateFooterPartContentFirst(FooterPart part,string strFooter)
{
Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" };
paragraphProperties1.Append(paragraphStyleId1);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strFooter));
AlternativeFormatImportPart formatImportPart = part.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, "footer");
formatImportPart.FeedData(ms);
AltChunk chunk = new AltChunk() { Id = "footer" };
footer1.Append(chunk);
part.Footer = footer1;
}
private void GenerateFooterPartContent(FooterPart part)
{
Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" };
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = " Default Footer";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
part.Footer = footer1;
}
private void GenerateHeaderPart2Content(HeaderPart headerPart1)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00BE49A6", RsidRunAdditionDefault = "00BE49A6" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
NoProof noProof1 = new NoProof();
runProperties1.Append(noProof1);
Picture picture1 = new Picture();
V.Shapetype shapetype1 = new V.Shapetype() { Id = "_x0000_t136", CoordinateSize = "21600,21600", OptionalNumber = 136, Adjustment = "10800", EdgePath = "m@7,l@8,m@5,21600l@6,21600e" };
V.Formulas formulas1 = new V.Formulas();
V.Formula formula1 = new V.Formula() { Equation = "sum #0 0 10800" };
V.Formula formula2 = new V.Formula() { Equation = "prod #0 2 1" };
V.Formula formula3 = new V.Formula() { Equation = "sum 21600 0 @1" };
V.Formula formula4 = new V.Formula() { Equation = "sum 0 0 @2" };
V.Formula formula5 = new V.Formula() { Equation = "sum 21600 0 @3" };
V.Formula formula6 = new V.Formula() { Equation = "if @0 @3 0" };
V.Formula formula7 = new V.Formula() { Equation = "if @0 21600 @1" };
V.Formula formula8 = new V.Formula() { Equation = "if @0 0 @2" };
V.Formula formula9 = new V.Formula() { Equation = "if @0 @4 21600" };
V.Formula formula10 = new V.Formula() { Equation = "mid @5 @6" };
V.Formula formula11 = new V.Formula() { Equation = "mid @8 @5" };
V.Formula formula12 = new V.Formula() { Equation = "mid @7 @8" };
V.Formula formula13 = new V.Formula() { Equation = "mid @6 @7" };
V.Formula formula14 = new V.Formula() { Equation = "sum @6 0 @5" };
formulas1.Append(formula1);
formulas1.Append(formula2);
formulas1.Append(formula3);
formulas1.Append(formula4);
formulas1.Append(formula5);
formulas1.Append(formula6);
formulas1.Append(formula7);
formulas1.Append(formula8);
formulas1.Append(formula9);
formulas1.Append(formula10);
formulas1.Append(formula11);
formulas1.Append(formula12);
formulas1.Append(formula13);
formulas1.Append(formula14);
V.Path path1 = new V.Path() { AllowTextPath = true, ConnectionPointType = Ovml.ConnectValues.Custom, ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", ConnectAngles = "270,180,90,0" };
V.TextPath textPath1 = new V.TextPath() { On = true, FitShape = true };
V.ShapeHandles shapeHandles1 = new V.ShapeHandles();
V.ShapeHandle shapeHandle1 = new V.ShapeHandle() { Position = "#0,bottomRight", XRange = "6629,14971" };
shapeHandles1.Append(shapeHandle1);
Ovml.Lock lock1 = new Ovml.Lock() { Extension = V.ExtensionHandlingBehaviorValues.Edit, TextLock = true, ShapeType = true };
shapetype1.Append(formulas1);
shapetype1.Append(path1);
shapetype1.Append(textPath1);
shapetype1.Append(shapeHandles1);
shapetype1.Append(lock1);
V.Shape shape1 = new V.Shape() { Id = "PowerPlusWaterMarkObject74287736", Style = "position:absolute;margin-left:0;margin-top:0;width:412.4pt;height:247.45pt;rotation:315;z-index:-251653120;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2051", AllowInCell = false, FillColor = "silver", Stroked = false, Type = "#_x0000_t136" };
V.Fill fill1 = new V.Fill() { Opacity = ".5" };
V.TextPath textPath2 = new V.TextPath() { Style = "font-family:\"Calibri\";font-size:1pt", String = "Draft" };
shape1.Append(fill1);
shape1.Append(textPath2);
picture1.Append(shapetype1);
picture1.Append(shape1);
run1.Append(runProperties1);
run1.Append(picture1);
paragraph2.Append(paragraphProperties1);
paragraph2.Append(run1);
header1.Append(paragraph2);
headerPart1.Header = header1;
}
#endregion
}
}