programmatically excel cells to be auto fit width & height

Sankar M picture Sankar M · Oct 3, 2012 · Viewed 45.3k times · Source

when i convert datatable to .csv my excel sheets are generated as below: enter image description here

and tried like below:

sw.Write(string.Format("=\"{0}\"", drow[i].ToString()));

then my excel sheets were like:

enter image description here

note that cells have =" " characters;

i'm trying to do like auto fit width & height of each cells programmatically. How?

Answer

Jayant Varshney picture Jayant Varshney · Jun 19, 2014

Try getting the range and then do Autofit

Range.Rows.AutoFit();
Range.Columns.AutoFit();