Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# 创建Word文档时如何更改表的列宽_C#_.net_Ms Word - Fatal编程技术网

C# 创建Word文档时如何更改表的列宽

C# 创建Word文档时如何更改表的列宽,c#,.net,ms-word,C#,.net,Ms Word,我提出了一个新问题,因为我在这里或其他地方找到的答案都不适合我 我使用C#(Office 16.0 Interop)中的Word创建了一个表 我的目标是:缩小某一列的宽度,即适合其内容。 以下是我创建表的方式: var table = doc.Tables.Add(menuParagraph.Range, rowCount, 2, ref _objMiss, ref _objMiss); table.Borders.InsideLineStyle = Word.WdLineStyle.wdLin

我提出了一个新问题,因为我在这里或其他地方找到的答案都不适合我

我使用C#(Office 16.0 Interop)中的Word创建了一个表

我的目标是:缩小某一列的宽度,即适合其内容。

以下是我创建表的方式:

var table = doc.Tables.Add(menuParagraph.Range, rowCount, 2, ref _objMiss, ref _objMiss);
table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;

// Label
table.Cell(1, 1).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
table.Cell(1, 1).Range.Text = "Label";
table.Cell(1, 2).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
table.Cell(1, 2).Range.Text = $"{recordItem.TextId.EnglishTranslation} ({recordItem.TextId.GermanTranslation})";
// Type and length
table.Cell(2, 1).Range.Text = "DataType";
table.Cell(2, 2).Range.Text = $"{recordItem.DataType} (Bit length: {recordItem.BitLength})";
// Byte offset
table.Cell(3, 1).Range.Text = "Byte offset";
table.Cell(3, 2).Range.Text = $"{recordItem.ByteOffset}";
// Default value
table.Cell(4, 1).Range.Text = "Default value";
table.Cell(4, 2).Range.Text = $"{recordItem.DefaultValue}";
到目前为止,我找到的任何解决方案都没有解决我的问题。
事实上,这些对我的桌子都没有任何影响

table.Columns[0].AutoFit()被忽略,没有任何效果

table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent)也是如此

即使我直接设置宽度,它也会被忽略:

table.Columns[0].Width = app.PixelsToPoints(100f);
输出总是相同的。每列宽度相同的表格


如何强制一个表使一列根据其内容调整其宽度(并且仍然使用整个页面的宽度)。

您是否尝试过:table.Columns[0]。PreferredWidth? 也许这样行。
下面是一些关于它的文档:

对于自动装配第一列,类似的方法应该可以工作。它使用


如果所有的问题都写得这么好,格式也这么好,那么这个世界就值得生活。首先,感谢您指出,第一列是[1],因为VB中基于1的索引。此外,这是可行的,但我的问题是另一个。奇怪的是,每当我更改与COM公开的Word内容相关的代码时,我都必须重新构建我的解决方案。。。在我的配置中似乎有问题…`第一列是[1],因为VB中基于1的索引`-@Exa,VB.Net索引不是基于1的。这是为Office集合类型选择的约定,而不是语言约束。VB.Net默认数组和.Net库中的集合都是基于零的。关于您的重建问题,您是否在使用调试器的“编辑并继续”功能方面做出了声明?如果是这样,这可能是因为Office接口程序集的
Embed Interop Type
设置为
True
table.AllowAutoFit = true;
Word.Column firstCol = table.Columns[1];
firstCol.AutoFit(); // force fit sizing
Single firstColAutoWidth = firstCol.Width; // store autofit width
table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow); // fill page width
firstCol.SetWidth(firstColAutoWidth, Word.WdRulerStyle.wdAdjustFirstColumn); // reset width keeping right table margin