我正在尝试将 Microsoft.Office.Interop.Word.Shape 放入表格单元格中,就像这个简短的示例一样:
Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), 4, 1);
Word.Cell oCell1 = oTable.Cell(1,1);
Word.Shape oShape1 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell1.Range);
Word.Cell oCell2 = oTable.Cell(2, 1);
Word.Shape oShape2 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell2.Range);
Word.Cell oCell3 = oTable.Cell(3, 1);
Word.Shape oShape3 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell3.Range);
Word.Cell oCell4 = oTable.Cell(4, 1);
Word.Shape oShape4 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell4.Range);
oWord.Visible = true;
矩形只出现在文档的左上角。 我不确定我做错了什么,因为我将形状 anchor 设置为单元格范围。
12/07/2016:
好的,看看这个, 我现在有 5 列和 100 行,并尝试将形状放入第三列。我正在使用“与文本一致”属性。
现在,在前两页上,所有形状都放在第一行/第一列。从第三页开始,它看起来是正确的...
我使用的是 Office 2013。
Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();
int numRows = 100;
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), numRows, 5);
oTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= numRows; ++r)
{
Word.Range anchorRange = oTable.Cell(r, 3).Range;
Word.Shape oShape = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, anchorRange);
oShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline;
}
oWord.Visible = true;
最佳答案
如果您单击添加的矩形,并启用段落/格式符号(它是“主页”>“段落”中向后看的 P 按钮),您会注意到 anchor 确实在表格单元格上。文字环绕的形状还需要其他设置才能在页面上具有特定位置。弄乱“大小和位置”对话框以感受一下。
关于C# Microsoft Interop Word 将形状放入表格单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216941/