Excel – 列宽不匹配多个加起来

我正在使用C#Excel Interop,并试图使合并单元格的高度适合其内容。 因为某些原因,合并单元格上的自动调整无法完成,所以我使用下面的方法来完成它

private void AutoFitAndMerge(Range toMerge, Range contentCell) { // Get Width of entire Merged Cell double width = 0.0; foreach (Range col in toMerge.Columns) { width += col.ColumnWidth; } // mimic merged cell with regular cell var cellWidth = contentCell.ColumnWidth; contentCell.ColumnWidth = width; contentCell.WrapText = true; // autofit the regular cell and copy that to merged contentCell.Rows.AutoFit(); var wantedHeight = contentCell.RowHeight; // set it back contentCell.ColumnWidth = cellWidth; toMerge.Merge(); toMerge.WrapText = true; toMerge.RowHeight = wantedHeight; } 

这工作很好,除了在开始,当我把contentCell的宽度设置为合并的单元格总宽度,它不正确地加起来。
问题在于contentCell稍微小一些,导致内容有时再次回绕,并为合并的单元分配太多空间。

这也发生在Excel中,当你加上2个或更多列的宽度,并将它分配给一个单元格的宽度,它最终略微变小。
单元格是否具有某种填充/边距,我该如何考虑?

在宽度计算之后,结束添加以下代码:

 // excel has some cell padding or whatever? This is a workaround for it width += .514 * ((toMerge.Columns.Count-1) * 2); 

这是一个黑客,但它符合我的要求,为我工作。 它所做的基本上是假设所有具有特定值的单元格都有一个填充,并将其添加到总数中。

既然不接受这个答案,那就不是。