Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Microsoft.office.Interop.Word创建Word文件_C#_Ms Word_Office Interop - Fatal编程技术网

C# 使用Microsoft.office.Interop.Word创建Word文件

C# 使用Microsoft.office.Interop.Word创建Word文件,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我正在使用此代码使用Microsoft.interop.Word C#以编程方式创建Word文件,当我创建它时,我遇到了一个问题,即将第一页的页眉重复到文档的所有页面,如何将其设置为仅放置文档第一页的页眉 这就是我的代码: public void create_Document() { try { //Create an instance for word app Microsoft.Office.Inte

我正在使用此代码使用Microsoft.interop.Word C#以编程方式创建Word文件,当我创建它时,我遇到了一个问题,即将第一页的页眉重复到文档的所有页面,如何将其设置为仅放置文档第一页的页眉

这就是我的代码:

 public void create_Document()
    {
        try
        {
            //Create an instance for word app
            Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

            //Set animation status for word application
            winword.ShowAnimation = false;

            //Set status for word application is to be visible or not.
            winword.Visible = false;

            //Create a missing variable for missing value
            object missing = System.Reflection.Missing.Value;

            //Create a new document
            Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            //Add header into the document
            foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
            {
                //Get the header range and add the header details.
                Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
                headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
                headerRange.Font.Size = 30;
                headerRange.Text = "Exam Dates for the " + course_name;
                section.Borders.Enable = 1;
                section.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                section.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth300pt;
                section.Borders.OutsideColor = WdColor.wdColorBlack;

            }



            //adding text to document
            document.Content.SetRange(0, 0);
            // document.Content.Text = "Exam Dates :" + Environment.NewLine;

            //Add paragraph with Heading 1 style
            Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            object styleHeading1 = "Heading 1";
            para1.Range.set_Style(ref styleHeading1);
            para1.Range.Text = "Exam Dates";
            para1.Range.InsertParagraphAfter();



            //Create a 5X5 table and insert some dummy record
            // Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 4,2, ref missing, ref missing);
            Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 1, 2, ref missing, ref missing);

            firstTable.Borders.Enable = 1;

            firstTable.Cell(1, 1).Range.Text = "Exam Name";
            firstTable.Cell(1, 2).Range.Text = "Exam Date";
            connection.Close();
            connection.Open();
            SqlCommand cmd = new SqlCommand("select ExamName,Date from Exam where CourseNum='" + course_number + "'", connection);
            SqlDataReader reader = cmd.ExecuteReader();

            int intRow = 2;

            // Retrieve the data and insert into new rows.
            Object beforeRow = Type.Missing;
            string[] dates;
            while (reader.Read())
            {
                dates = reader[1].ToString().Split(' ');
                firstTable.Rows.Add(ref beforeRow);
                firstTable.Cell(intRow, 1).Range.Text = reader[0].ToString();
                firstTable.Cell(intRow, 2).Range.Text = dates[0];

                intRow += 1;
            }
            //Save the document
            object filename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Attachments\\Exams.docx";
           document.SaveAs2(ref filename);

           // document.Save();
            ((Microsoft.Office.Interop.Word._Document)document).Close(ref missing, ref missing, ref missing);
            ((Microsoft.Office.Interop.Word._Application)winword).Quit(ref missing, ref missing, ref missing);
 }
        catch (Exception ex)
        {
            ex.ToString();
        }


        }//create document

我相信您正在寻找另一个
wdHeaderFooterIndex
property
wdHeaderFooterFirstPage
就是您正在寻找的。感谢@Arun在。。。您将看到还需要将documents
PageSetup.DifferentFirstPageHeaderFooter
值设置为-1

只需将下面的第一行添加到代码中,并更改代码中的第二行,以仅获取第一页的页眉范围

document.PageSetup.DifferentFirstPageHeaderFooter = -1;  // <-- add this line
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
//Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
document.PageSetup.DifferentFirstPageHeaderFooter=-1//