OpenXML操作Word的踩过的几个坑

1、Open XML是微软提供的操作word的公共组件,但是只能支持Office2007及以上版本。
2、我这里XML操作的简单结构不涉及单元格等复杂操作,如需复杂操作需自行通过OpenXml Tool查看研究。具体操作结构包括:document 根节点
body paragraph段落 run
如图:
OpenXML操作Word的踩过的几个坑_第1张图片
3、格式文本写入body步骤 :
段属性、Run属性、Run内容、Run写入段中。
代码如下:
//段属性
paragraphTitle.Append(paragraphTitlePropertites);
//Run属性
runTitle.Append(runTitleProperties1);
//Run内容
runTitle.Append(new Text() { Text = schemaNode.Name });
//Run写入段中
paragraphTitle.Append(runTitle);
//段写入Body
elements.Add(paragraphTitle);
4、几个简单的单位换算
1字符=100 em
1行距=12 * 20em
1Size=*2Size
5、如下是没有整理的代码块

/* 这里是读取word到docum代码
WordprocessingDocument Word = null; //创建空文书并打开
                OpenSettings openSettings = new OpenSettings() { AutoSave = true };
                Word = WordprocessingDocument.Open(filePath, true, openSettings); //打开Word
                DocumentFormat.OpenXml.Wordprocessing.Body OpenXMLDocumentBody = null;
                OpenXMLDocumentBody = Word.MainDocumentPart.Document.Body;
*/

/// 
        /// 
        /// 
        /// 当前操作的纲要节点    
        /// 当前操作的纲要节点
        /// 
        internal List BuildWordElement(RecordAnalysisSubmitBody schemaNode, XmlCroLayOutModelBody croLayOutNode)
        {
            List elements = new List();
            #region 写入标题  
            //标题显示到word文档
            if (schemaNode.ShowToContent == "1")
            {
                #region   格式
                Paragraph paragraphTitle = new Paragraph();
                ParagraphProperties paragraphTitlePropertites = new ParagraphProperties();
                ParagraphMarkRunProperties paragraphTitilePropertitlesRun = new
                     ParagraphMarkRunProperties();
                #endregion
                Run runTitle = new Run();
                RunProperties runTitleProperties1 = new RunProperties();
                if (croLayOutNode.TitleFormat != null)
                {
                    #region  字体
                    if (!string.IsNullOrWhiteSpace(croLayOutNode.TitleFormat.FontName))
                    {
                        RunFonts runFonts2 = new RunFonts()
                        {
                            Ascii = croLayOutNode.TitleFormat.FontName,
                            EastAsia = croLayOutNode.TitleFormat.FontName
                        };
                        runTitleProperties1.Append(runFonts2);
                    }
                    #endregion

                    #region  字号
                    if (!string.IsNullOrWhiteSpace(croLayOutNode.TitleFormat.FontSize))
                    {
                        FontSize fontSize2 = new FontSize() { Val = (Convert.ToInt32(croLayOutNode.TitleFormat.FontSize) * 2).ToString() };
                        runTitleProperties1.Append(fontSize2);
                    }
                    #endregion

                    #region  加粗
                    if (croLayOutNode.TitleFormat.FontBold.ToString() == "true")
                    {
                        Bold bold2 = new Bold();
                        runTitleProperties1.Append(bold2);
                    }
                    #endregion

                    #region   对其方式
                    Justification justification1 = new Justification();
                    switch (croLayOutNode.TitleFormat.ParagraphAlignment)
                    {
                        //左对齐
                        case "1":
                            {
                                justification1 = new Justification() { Val = JustificationValues.Left };
                                paragraphTitlePropertites.Append(justification1); break;
                            }
                        //居中齐
                        case "3":
                            {
                                justification1 = new Justification() { Val = JustificationValues.Center };
                                paragraphTitlePropertites.Append(justification1); break;
                            }
                        //右对齐
                        case "2":
                            {
                                justification1 = new Justification() { Val = JustificationValues.Right };
                                paragraphTitlePropertites.Append(justification1); break;
                            }
                        //两端
                        case "4":
                            {
                                justification1 = new Justification() { Val = JustificationValues.Both };
                                paragraphTitlePropertites.Append(justification1);
                                break;
                            }
                    }
                    #endregion

                    #region   缩进 需要明确
                    Indentation indentation1 = new Indentation();
                    //首行缩进
                    if (!string.IsNullOrEmpty(croLayOutNode.TitleFormat.ParagraphFirstLineIndent) &&
                        croLayOutNode.TitleFormat.ParagraphFirstLineIndent != "0")
                    {
                        indentation1.FirstLineChars = Convert.ToInt32(croLayOutNode.TitleFormat.ParagraphFirstLineIndent) * 100;
                    }
                    //左缩进
                    if (!string.IsNullOrEmpty(croLayOutNode.TitleFormat.ParagraphIndentationLeft) &&
                     croLayOutNode.TitleFormat.ParagraphIndentationLeft != "0")
                    {
                        indentation1.LeftChars = Convert.ToInt32(croLayOutNode.TitleFormat.ParagraphIndentationLeft) * 100;
                    }
                    paragraphTitlePropertites.Append(indentation1);
                    #endregion

                    #region   行间距
                    //端前
                    SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines() { LineRule = LineSpacingRuleValues.Auto };
                    if (!string.IsNullOrEmpty(croLayOutNode.TitleFormat.ParagraphBefore) &&
                     croLayOutNode.TitleFormat.ParagraphBefore != "0")
                    {
                        spacingBetweenLines1.BeforeLines = Convert.ToInt32(croLayOutNode.TitleFormat.ParagraphBefore) * 12 * 20;
                    }
                    //端后
                    if (!string.IsNullOrEmpty(croLayOutNode.TitleFormat.ParagraphAfter) &&
                     croLayOutNode.TitleFormat.ParagraphAfter != "0")
                    {
                        spacingBetweenLines1.AfterLines = Convert.ToInt32(croLayOutNode.TitleFormat.ParagraphAfter) * 12 * 20;
                    }
                    if (!string.IsNullOrEmpty(croLayOutNode.TitleFormat.ParagraphLineSpacing) &&
                     croLayOutNode.TitleFormat.ParagraphLineSpacing != "0")
                    {
                        spacingBetweenLines1.Line =  (Convert.ToInt32(croLayOutNode.TitleFormat.ParagraphLineSpacing) * 12 * 20).ToString();
                    }
                    paragraphTitlePropertites.Append(spacingBetweenLines1);
                    #endregion


                }
                paragraphTitlePropertites.Append(paragraphTitilePropertitlesRun);
                paragraphTitle.Append(paragraphTitlePropertites);
                runTitle.Append(runTitleProperties1);
                runTitle.Append(new Text() { Text = schemaNode.Name });
                paragraphTitle.Append(runTitle);
                elements.Add(paragraphTitle);
            }
            #endregion

            Paragraph paragraphContent = new Paragraph();
            #region 写入纲要节点
            #region   格式
            ParagraphProperties paragraphContentProper = new ParagraphProperties();
            ParagraphMarkRunProperties paragraphContentProperRun = new
                 ParagraphMarkRunProperties();
           
            #endregion
            Run runContent = new Run();
            RunProperties runContentProperties1 = new RunProperties();
       
            if (croLayOutNode.ContentFormat != null && croLayOutNode.ContentFormat.Enable.ToString() == "true")
            {
                #region  字体
                if (!string.IsNullOrWhiteSpace(croLayOutNode.ContentFormat.FontName))
                {
                    RunFonts runFonts2 = new RunFonts()
                    {
                        Ascii = croLayOutNode.ContentFormat.FontName,
                        EastAsia = croLayOutNode.ContentFormat.FontName
                    };
                    runContentProperties1.Append(runFonts2);

                    Kern kern1 = new Kern() { Val = (UInt32Value)0U };

                    runContentProperties1.Append(kern1);
                }
                #endregion

                #region  字号
                if (!string.IsNullOrWhiteSpace(croLayOutNode.ContentFormat.FontSize))
                {
                    FontSize fontSize2 = new FontSize() { Val = (Convert.ToInt32( croLayOutNode.ContentFormat.FontSize)*2).ToString() };
                    runContentProperties1.Append(fontSize2);

                    Underline underline1 = new Underline() { Color = "000000" };
                    runContentProperties1.Append(underline1);
                }
                #endregion

                #region  加粗
                if (croLayOutNode.ContentFormat.FontBold.ToString() == "true")
                {
                    Bold bold2 = new Bold();
                    runContentProperties1.Append(bold2);
                }
                #endregion

                #region   对其方式
                Justification justification1 = new Justification();
                switch (croLayOutNode.TitleFormat.ParagraphAlignment)
                {
                    //左对齐
                    case "1":
                        {
                            justification1 = new Justification() { Val = JustificationValues.Left };
                            paragraphContentProper.Append(justification1); break;
                        }
                    //居中齐
                    case "3":
                        {
                            justification1 = new Justification() { Val = JustificationValues.Center };
                            paragraphContentProper.Append(justification1); break;
                        }
                    //右对齐
                    case "2":
                        {
                            justification1 = new Justification() { Val = JustificationValues.Right };
                            paragraphContentProper.Append(justification1); break;
                        }
                    //两端
                    case "4":
                        {
                            justification1 = new Justification() { Val = JustificationValues.Both };
                            paragraphContentProper.Append(justification1);
                            break;
                        }
                }
                #endregion

                #region   缩进 需要明确
                Indentation indentation1 = new Indentation();
                //首行缩进
                if (!string.IsNullOrEmpty(croLayOutNode.ContentFormat.ParagraphFirstLineIndent) &&
                    croLayOutNode.ContentFormat.ParagraphFirstLineIndent != "0")
                {
                    indentation1.FirstLineChars = Convert.ToInt32(croLayOutNode.ContentFormat.ParagraphFirstLineIndent) * 100;
                }
                //左缩进
                if (!string.IsNullOrEmpty(croLayOutNode.ContentFormat.ParagraphIndentationLeft) &&
                 croLayOutNode.ContentFormat.ParagraphIndentationLeft != "0")
                {
                    indentation1.LeftChars = Convert.ToInt32(croLayOutNode.ContentFormat.ParagraphIndentationLeft) * 100;
                }
                paragraphContentProper.Append(indentation1);
                #endregion

                #region   行间距
                //端前
                SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines() { LineRule = LineSpacingRuleValues.Auto };
                if (!string.IsNullOrEmpty(croLayOutNode.ContentFormat.ParagraphBefore) &&
                 croLayOutNode.ContentFormat.ParagraphBefore != "0")
                {
                    spacingBetweenLines1.BeforeLines = Convert.ToInt32(croLayOutNode.ContentFormat.ParagraphBefore) * 12 * 20;
                }
                //端后
                if (!string.IsNullOrEmpty(croLayOutNode.ContentFormat.ParagraphAfter) &&
                 croLayOutNode.ContentFormat.ParagraphAfter != "0")
                {
                    spacingBetweenLines1.AfterLines = Convert.ToInt32(croLayOutNode.ContentFormat.ParagraphAfter) * 12 * 20;
                }
                if (!string.IsNullOrEmpty(croLayOutNode.ContentFormat.ParagraphLineSpacing) &&
                 croLayOutNode.ContentFormat.ParagraphLineSpacing != "0")
                {
                    spacingBetweenLines1.LineRule = LineSpacingRuleValues.Auto;
                    spacingBetweenLines1.Line = (Convert.ToInt32(croLayOutNode.ContentFormat.ParagraphLineSpacing) * 12 * 20).ToString();
                }
                else {
                    spacingBetweenLines1.Line = (12 * 20).ToString();
                }
                paragraphContentProper.Append(spacingBetweenLines1);
                #endregion


            }

            paragraphContentProper.Append(paragraphContentProperRun);
            paragraphContent.Append(paragraphContentProper);
            runContent.Append(runContentProperties1);
            foreach (Model.Receipt_Model.Base.RecordContent c in schemaNode.Contents)
            {
                runContent.Append(new Text() { Text = c.Content }); ;
            }
            paragraphContent.Append(runContent);  
            elements.Add(paragraphContent);
            #endregion

           
            return elements;
        }

你可能感兴趣的:(OpenXml)