'Set Excel column width using Interop.Excel in .Net 6
I am upgrading a .net Framework 4.7.2 project to .net 6 (using VS2022).
In the 10-yr old project we used Microsoft.Office.Interop.Excel (from Visual Studio PIA folder) for a C# statement:
ExcelApp.Columns[0].ColumnWidth = 60;
where of-course ExcelApp is created with a statement like:
Excel._Application ExcelApp = new Excel.Application();
This has been working for years but when I upgrade to .net 6 the property .ColumnWidth will not compile. I have tried all microsoft.office.interop.excel nupkg packages but all result in a yellow yield symbol (I assume this means not compatible with .Net 6 or x64). Hence, my only option is to reference the com assembly 'Microsoft Excel 16.0 Object Library'. This works well for many excel statements but even it will not accept that ColumnWidth property.
How do I set an excel column width in .Net 6 using 'Microsoft Excel 16.0 Object Library'?
Solution 1:[1]
This works - note it must be two lines of code!
ExcelApp.Range colA = ExcelApp.get_Range("A:A");
colA.ColumnWidth = 60;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CsharpSqlGuy |
