如何通过C#Excel Interop获取和设置Excel列宽 [英] How to get and set excel column width through c# excel interop

查看:426
本文介绍了如何通过C#Excel Interop获取和设置Excel列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建excel时使用了excel interop.现在,我必须获取一列的大小作为下一列大小的基础.我尝试了以下代码:

I am using excel interop in creating excel. Now, I have to get the size of a column as basis of what the next column's size would be. I tried the following code:

width = xlWorksheet.Range[xlWorksheet.Cells[1, 4], xlWorksheet.Cells[1, 11]].Width;
xlWorksheet.Columns[5].ColumnWidth = width;

width_of_first_col = xlWorksheet.Range[xlWorksheet.Cells[1, 1], xlWorksheet.Cells[1, 1]].Width;
xlWorksheet.Columns[6].ColumnWidth = width_of_first_col;

结果应该是第4列和第5列的大小以及第1列和第6列的大小相同.但这并不符合确切的结果.

The result should be column 4 and column 5 have the same size, as well as column 1 and 6. But this is not true to what the exact result looks like.

将一个单元格的with设置为另一个单元格的最佳方法是什么.谢谢.

What is the best thing to do to set the with of a cell to another cell. Thanks.

推荐答案

Excel以多种方式管理列的宽度.

Excel manages width of columns in multiple ways.

  • Columnwidth 属性将列的宽度返回或设置为可以容纳在一列中的普通字体的字符数.请参考此链接.
  • width 属性以指定的
  • The Columnwidth property returns or sets the width of columns as the number of characters of the normal font that can be accommodated in one column. Please refer to this link.
  • The width property gives the width in points of a range as specified here.

这篇关于如何通过C#Excel Interop获取和设置Excel列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆