互操作词:有没有办法将多个表格或段落添加到表格单元格中而不将它们合并在一起?

问题描述

我在向表格单元格添加多个段落或表格时遇到问题。它们都融合在一起。

[结果]

1

  • 我希望我的两个表格一个一个,而不是合并到我的单元格范围内
  • 我希望将红色文本(我在 word 中放入红色)作为我的表格标题,我之前在代码添加了它,但它仍然出现在之后。

这是我的代码

Paragraph pBT_Text = document.Paragraphs.Add(cell.Range);
pBT_Text.Range.Font.Bold = 1;
pBT_Text.Range.Font.Size = 7;
pBT_Text.Range.Font.Underline = WdUnderline.wdUnderlinesingle;
pBT_Text.Range.Text = "x-content";
pBT_Text.Range.InsertParagraphAfter();

Paragraph pIntroTable_BT = document.Paragraphs.Add(cell.Range);
Table Bt_Tech_Table = document.Tables.Add(pIntroTable_BT.Range,5,7,ref missing,ref missing);
Bt_Tech_Table.Borders.Enable = 1;
Bt_Tech_Table.Range.Font.Underline = WdUnderline.wdUnderlineNone;
Bt_Tech_Table.Range.Paragraphs.Alignment = WdParagraphAlignment.wdalignParagraphCenter;
Bt_Tech_Table.AllowAutoFit = true;

Bt_Tech_Table.Rows[1].Cells[1].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[1].Shading.BackgroundPatternColor = WdColor.wdColorLightTurquoise;

Bt_Tech_Table.Rows[1].Cells[2].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[2].Shading.BackgroundPatternColor = WdColor.wdColorLightGreen;

Bt_Tech_Table.Rows[1].Cells[3].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[3].Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;

Bt_Tech_Table.Rows[1].Cells[4].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[4].Shading.BackgroundPatternColor = WdColor.wdColorLavender;

Bt_Tech_Table.Rows[1].Cells[5].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[5].Shading.BackgroundPatternColor = WdColor.wdColorLightOrange;

Bt_Tech_Table.Rows[1].Cells[6].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[6].Shading.BackgroundPatternColor = WdColor.wdColorLightYellow;

Bt_Tech_Table.Rows[1].Cells[7].Range.Text += "x-content";
Bt_Tech_Table.Rows[1].Cells[7].Shading.BackgroundPatternColor = WdColor.wdColorGray35;

Bt_Tech_Table.Columns.AutoFit();

for ( int i =0; i< 4; i++)
{
    for (int j = 0; j < 7; j++)
    {
        cell.Range.Font.Bold = 0;
        Bt_Tech_Table.Rows[i+2].Cells[j+1].Range.Text += this.dgv_BT.Rows[i].Cells[j].Value.ToString();

        if ((i+2) == 5 || (j+1) == 7)
        {
            Bt_Tech_Table.Rows[i + 2].Cells[j + 1].Shading.BackgroundPatternColor = WdColor.wdColorGray35;
        }
    }
}

pIntroTable_BT.Range.InsertParagraphAfter();

Paragraph pMP_Text = document.Paragraphs.Add(cell.Range);
pMP_Text.Range.Font.Bold = 1;
pMP_Text.Range.Font.Size = 7;
pMP_Text.Range.Font.Underline = WdUnderline.wdUnderlinesingle;
pMP_Text.Range.Text = "x-content";
pMP_Text.Range.InsertParagraphAfter();

Paragraph pIntroTable_MP = document.Paragraphs.Add(cell.Range);
Table Bt_MP_Table = document.Tables.Add(pIntroTable_MP.Range,2,ref missing);
Bt_MP_Table.Borders.Enable = 1;
Bt_MP_Table.Range.Font.Underline = WdUnderline.wdUnderlineNone;
Bt_MP_Table.Range.Paragraphs.Alignment = WdParagraphAlignment.wdalignParagraphCenter;
Bt_MP_Table.AllowAutoFit = true;

Bt_MP_Table.Rows[1].Cells[1].Range.Text += "x-content";
Bt_MP_Table.Rows[1].Cells[1].Shading.BackgroundPatternColor = WdColor.wdColorLightTurquoise;

Bt_MP_Table.Rows[1].Cells[2].Range.Text += "x-content";
Bt_MP_Table.Rows[1].Cells[2].Shading.BackgroundPatternColor = WdColor.wdColorLightGreen;

Bt_MP_Table.Rows[1].Cells[3].Range.Text += "x-content";
Bt_MP_Table.Rows[1].Cells[3].Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;

Bt_MP_Table.Rows[1].Cells[4].Range.Text += "x-content";
Bt_MP_Table.Rows[1].Cells[4].Shading.BackgroundPatternColor = WdColor.wdColorLavender;

Bt_MP_Table.Rows[1].Cells[5].Range.Text += "x-content";
Bt_MP_Table.Rows[1].Cells[5].Shading.BackgroundPatternColor = WdColor.wdColorLightOrange;

Bt_MP_Table.Columns.AutoFit();

for (int j = 0; j < 5; j++)
{
    Bt_MP_Table.Rows[2].Cells[j + 1].Range.Text += this.dgv_Crit.Rows[0].Cells[j].Value.ToString();
}
pIntroTable_MP.Range.InsertParagraphAfter();

我尝试了很多对范围的操作,添加了段落,试图理解一种方法来做到这一点。到目前为止,我按照自己的意愿成功放置它们的唯一合法方法添加不引用 cell.Range 的段落。

我知道表格完全“吃掉”了范围,这就是为什么我认为使用不同的段落会成功。

非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)