VB.NET 中的 Excel 自动调整列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19359866/
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
Excel auto fit column in VB.NET
提问by user2878699
I have here my code in VB6 and its working well.
我在 VB6 中有我的代码并且它运行良好。
For CLms = 1 To 10
ws.Columns(CLms).AutoFit '<---Autofilt data on XL sheet
Next CLms
I have searched on what is the equivalent or alternative on how to make the excel columns width autofit in VB.NET, but I'm still not getting the exact AutoFit function.
我已经搜索了如何在 VB.NET 中使 excel 列宽自动调整的等效或替代方法,但我仍然没有得到确切的 AutoFit 功能。
Any help will be appreciated.
任何帮助将不胜感激。
回答by aphoria
This is how I've done it in vb.net.
这就是我在 vb.net 中所做的。
xlSheet.Range("A1:X1").EntireColumn.AutoFit()
回答by Jorge Valdes
For example if you don't know the number of columns you can autofit the complete Row.
例如,如果您不知道列数,则可以自动调整整个行。
xlSheet.Rows.Item(1).EntireColumn.AutoFit()
回答by ziad mansour
hello i try all these solution..but no one was work.. please take the right solution:
你好,我尝试了所有这些解决方案..但没有人工作..请采取正确的解决方案:
With oSheet
.PageSetup.Zoom = False
.PageSetup.FitToPagesWide = 1
.PageSetup.FitToPagesTall = False
End With
回答by Matt Wilko
The methods for manipulating an Excel column are the same regardless of whether you are calling the code from VB6 or VB.NET. So there should be no change to your code if it works in VB6.
无论您是从 VB6 还是 VB.NET 调用代码,操作 Excel 列的方法都是相同的。因此,如果它在 VB6 中工作,则不应更改您的代码。
However for some help - Have a look at this full examplein VB.NET that includes autofit
回答by Caves
I used the ff. codes to autofit the used range.
我用过ff。代码以自动适应使用的范围。
xlWorkSheet.UsedRange.EntireColumn.AutoFit()