OpenXml生成Word

using DocumentFormat.OpenXml.Wordprocessing;



 //生成word文档

        private void 生成word文档ToolStripMenuItem_Click(object sender, EventArgs e)

        {

      using (WordprocessingDocument package = WordprocessingDocument.Create(@"OpenWord.docx", WordprocessingDocumentType.Document))
                {
                    CreateParts(package);
                }

}



private void CreateParts(WordprocessingDocument document)
        {          


            MainDocumentPart mainDocumentPart1 = document.AddMainDocumentPart();
            try
            {
                GenerateMainDocumentPart1Content(mainDocumentPart1);
            }
            catch (Exception ex) { MessageBox.Show("生成word失败!" + ex.Message); }
        }



  private void GenerateMainDocumentPart1Content(MainDocumentPart mainDocumentPart1)
        {





            Document document1 = new Document() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 w15 wp14" } };


            Body body1 = new Body();


            DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph1 = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();


            DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties paragraphProperties1 = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties();


            NumberingProperties numberingProperties1 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference() { Val = 0 };
            NumberingId numberingId1 = new NumberingId() { Val = 0 };


            numberingProperties1.Append(numberingLevelReference1);
            numberingProperties1.Append(numberingId1);
            DocumentFormat.OpenXml.Wordprocessing.Justification justification1 = new DocumentFormat.OpenXml.Wordprocessing.Justification() { Val = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Left };


            ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
            RunFonts runFonts1 = new RunFonts() { Hint = FontTypeHintValues.EastAsia, Ascii = "新宋体", HighAnsi = "新宋体", EastAsia = "新宋体", ComplexScript = "新宋体" };
            Bold bold1 = new Bold();
            BoldComplexScript boldComplexScript1 = new BoldComplexScript();
            DocumentFormat.OpenXml.Wordprocessing.Color color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "auto" };
            FontSize fontSize1 = new FontSize() { Val = "28" };
            FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() { Val = "28" };
            Languages languages1 = new Languages() { Val = "en-US", EastAsia = "zh-CN" };


            paragraphMarkRunProperties1.Append(runFonts1);
            paragraphMarkRunProperties1.Append(bold1);
            paragraphMarkRunProperties1.Append(boldComplexScript1);
            paragraphMarkRunProperties1.Append(color1);
            paragraphMarkRunProperties1.Append(fontSize1);
            paragraphMarkRunProperties1.Append(fontSizeComplexScript1);
            paragraphMarkRunProperties1.Append(languages1);


            paragraphProperties1.Append(numberingProperties1);
            paragraphProperties1.Append(justification1);
            paragraphProperties1.Append(paragraphMarkRunProperties1);


            DocumentFormat.OpenXml.Wordprocessing.Run run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();


            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties1 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties();




            //RunFonts runFonts2 = new RunFonts() { Hint = FontTypeHintValues.EastAsia, Ascii = "新宋体", HighAnsi = "新宋体", EastAsia = "新宋体", ComplexScript = "新宋体" };
            RunFonts runFonts2 = null;
            if (model.FirstWDBHFont != null)
            {
                runFonts2 = new RunFonts() { Hint = FontTypeHintValues.EastAsia, Ascii = model.WDBHFont, HighAnsi = model.WDBHFont, EastAsia = model.WDBHFont, ComplexScript = model.WDBHFont };
            }
            else
            {
                runFonts2 = new RunFonts() { Hint = FontTypeHintValues.EastAsia, Ascii = "新宋体", HighAnsi = "新宋体", EastAsia = "新宋体", ComplexScript = "新宋体" };
            }


            Bold bold2 = new Bold();
            BoldComplexScript boldComplexScript2 = new BoldComplexScript();


            //DocumentFormat.OpenXml.Wordprocessing.Color color2 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "auto" };
            DocumentFormat.OpenXml.Wordprocessing.Color color2 = null;
            if (model.FirstWDBHColor != null)
            {
                color2 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = model.FirstWDBHColor };
            }
            else
            {
                color2 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "auto" };
            }




            //FontSize fontSize2 = new FontSize() { Val = "21" };
            FontSize fontSize2 = null;
            if (model.FirstWDBHSize != null)
            {
                fontSize2 = new FontSize() { Val = model.FirstWDBHSize };
            }
            else
            {
                fontSize2 = new FontSize() { Val = "21" };
            }








            FontSizeComplexScript fontSizeComplexScript2 = new FontSizeComplexScript() { Val = "21" };
            Languages languages2 = new Languages() { Val = "en-US", EastAsia = "zh-CN" };


            runProperties1.Append(runFonts2);


            if (model.WDBHBold)
            {
                runProperties1.Append(bold2);
            }


            runProperties1.Append(boldComplexScript2);
            runProperties1.Append(color2);
            runProperties1.Append(fontSize2);
            runProperties1.Append(fontSizeComplexScript2);
            runProperties1.Append(languages2);
            DocumentFormat.OpenXml.Wordprocessing.Text text1 = new DocumentFormat.OpenXml.Wordprocessing.Text();
            text1.Text = cb_wdbh.Text;  //文档编号


            run1.Append(runProperties1);
            run1.Append(text1);

    paragraph1.Append(run1);

}

你可能感兴趣的:(openxml,word)