How to properly set Column Width upon creating Excel file? (Column properties)
I normally do this in VB and its easier because Excel records macros in VB. So I normally go to Excel and save the macro I want to do.
So that's what I did now and I got this code:
Columns("E:E").ColumnWidth = 17.29;
Range("E3").Interior.Pattern = xlSolid;
Range("E3").Interior.PatternColorIndex = xlAutomatic;
Range("E3").Interior.Color = 65535;
Range("E3").Interior.TintAndShade = 0;
Range("E3").Interior.PatternTintAndShade = 0;
I think you can do something like this:
xlWorkSheet.Columns[5].ColumnWidth = 18;
For your last question what you need to do is loop trough the columns you want to set their width:
for (int i = 1; i <= 10; i++) // this will apply it from col 1 to 10
{
xlWorkSheet.Columns[i].ColumnWidth = 18;
}
I did it this way:
var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Item[1];
xlWorkSheet.Columns.AutoFit();
With this way, columns always fit to text width inside cells.
Hope it helps to someone!