简体   繁体   中英

How to merge cells of a table created in MS Word using C#.NET?

I need to print a table to the Word document using C# code. I am facing issues with the alignment of text in the cells.

My code is as follows:

private void CreateTableInDoc()
    {
        object oMissing = System.Reflection.Missing.Value;
        object oEndOfDoc = "\\endofdoc";
        Microsoft.Office.Interop.Word._Application objWord;
        Microsoft.Office.Interop.Word._Document objDoc;
        objWord = new Microsoft.Office.Interop.Word.Application();
        objWord.Visible = true;
        objDoc = objWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        int i = 0;
        int j = 0;
        Microsoft.Office.Interop.Word.Table objTable;
        Microsoft.Office.Interop.Word.Range wrdRng = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

        string strText;
        objTable = objDoc.Tables.Add(wrdRng, 1, 1, ref oMissing, ref oMissing);
        objTable.Range.ParagraphFormat.SpaceAfter = 7;
        strText = "ABC Company";
        objTable.Rows[1].Range.Text = strText;
        strText = "";
        objTable.Rows[1].Range.Font.Bold = 1;
        objTable.Rows[1].Range.Font.Size = 24;
        objTable.Rows[1].Range.Font.Position = 1;
        objTable.Rows[1].Range.Font.Name = "Times New Roman";

        Microsoft.Office.Interop.Word.Table objNewTable;
        objNewTable = objDoc.Tables.Add(wrdRng, 3, 2, ref oMissing, ref oMissing);
        objNewTable.Rows[1].Range.Font.Name = "Times New Roman";
        objNewTable.Rows[1].Range.Font.Bold = 1;
        objNewTable.Rows[1].Range.Font.Italic = 1;
        objNewTable.Rows[1].Range.Font.Size = 14;
        objNewTable.Cell(1, 1).Range.Text = "Item Name";
        objNewTable.Cell(1, 2).Range.Text = "Price";

        for (i = 2; i <= 3; i++)
        {
            objNewTable.Rows[i].Range.Font.Name = "Times New Roman";
            objNewTable.Rows[i].Range.Font.Bold = 0;
            objNewTable.Rows[i].Range.Font.Italic = 0;
            objNewTable.Rows[i].Range.Font.Size = 12;
            for (j = 1; j <= 2; j++)
            {
                if (j == 1)
                    objNewTable.Cell(i, j).Range.Text = "Item " + (i-1);
                else
                    objNewTable.Cell(i, j).Range.Text = "Price of " + (i-1);
            }
        }

        try
        {
            objTable.Borders.Shadow = true;
            objNewTable.Borders.Shadow = true;
        }
        catch
        {
        }
        this.Close();
    }

The output that I get is as follows: 在此输入图像描述

My expected output is as follows: 在此输入图像描述

You don't need to create another table for header. It can done by merging two cell for very first row and set the paragraph alignment for the cell individually. here is the code.

public void CreateTableInDoc()
{
    object oMissing = System.Reflection.Missing.Value;
    object oEndOfDoc = "\\endofdoc"; 
    Microsoft.Office.Interop.Word._Application objWord;
    Microsoft.Office.Interop.Word._Document objDoc;
    objWord = new Microsoft.Office.Interop.Word.Application();
    objWord.Visible = true;
    objDoc = objWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    int i = 0;
    int j = 0;
    Microsoft.Office.Interop.Word.Table objTable;
    Microsoft.Office.Interop.Word.Range wrdRng = objDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

    string strText;
    objTable = objDoc.Tables.Add(wrdRng, 4, 2, ref oMissing, ref oMissing);
    objTable.Range.ParagraphFormat.SpaceAfter = 7;
    strText = "ABC Company";
    objTable.Rows[1].Range.Text = strText;
    objTable.Rows[1].Range.Font.Bold = 1;
    objTable.Rows[1].Range.Font.Size = 24;
    objTable.Rows[1].Range.Font.Position = 1;
    objTable.Rows[1].Range.Font.Name = "Times New Roman";
    objTable.Rows[1].Cells[1].Merge(objTable.Rows[1].Cells[2]);
    objTable.Cell(1,1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

    objTable.Rows[2].Range.Font.Italic = 1;
    objTable.Rows[2].Range.Font.Size = 14;
    objTable.Cell(2, 1).Range.Text = "Item Name";
    objTable.Cell(2, 2).Range.Text = "Price";
    objTable.Cell(2, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    objTable.Cell(2, 2).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

    for (i = 3; i <= 4; i++)
    {
        objTable.Rows[i].Range.Font.Bold = 0;
        objTable.Rows[i].Range.Font.Italic = 0;
        objTable.Rows[i].Range.Font.Size = 12;
        for (j = 1; j <= 2; j++)
        {
            if (j == 1)
                objTable.Cell(i, j).Range.Text = "Item " + (i - 1);
            else
                objTable.Cell(i, j).Range.Text = "Price of " + (i - 1);
        }
    }

        try
        {
            objTable.Borders.Shadow = true;
            objTable.Borders.Shadow = true;
        }
        catch
        {
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM