vb.net 如何更改Excel列宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23127536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change Excel column width
提问by E R
I'm handling an excel file using visual studio 2013, visual basic, windows Forms.
我正在使用 Visual Studio 2013、visual basic、windows Forms 处理 excel 文件。
- I add new sheet to excel named sheetReport
- 我将新工作表添加到名为 sheetReport 的 Excel
How to change the column width
of b to be 30?
如何将width
b的列更改为 30?
I don't need to use autofit
, cause it make text small to fit in cell with it's default width.
我不需要使用autofit
,因为它使文本变小以适应具有默认宽度的单元格。
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(FileName)
SheetReport = CType(xlBook.Sheets.Add(), Excel.Worksheet)
SheetReport.Name = "Report"
SheetReport.Range("B2").Value = "Agent Name"
' need resize column b
回答by SysDragon
Simply:
简单地:
SheetReport.Range("B2").ColumnWidth = 30
To learn the code with Excel API I'll give you a trick you can use; just open a Excel Sheet, start recording a Macro, and then do the thing you want to do programatically. Then stop the recording and look for the code recorded of the macro.
要学习 Excel API 的代码,我会给你一个你可以使用的技巧;只需打开一个 Excel 工作表,开始录制宏,然后以编程方式执行您想做的事情。然后停止录制,查找录制的宏代码。
It will contain the instructions in VBA to make it possible. With a little modification, that's what you need to do in VB.NET with the Excel API.
它将包含 VBA 中的说明以使其成为可能。稍加修改,这就是您需要在 VB.NET 中使用 Excel API 执行的操作。