Sankar M
Sankar M

Reputation: 4789

programmatically excel cells to be auto fit width & height

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?

Upvotes: 12

Views: 59280

Answers (3)

Khamul
Khamul

Reputation: 51

This questions just helped me to solve part of a problem. I had to copy the data to a secondary, auxiliary sheet, then send it to the datagrid, but when I did, it would display in the datagrid the sequence of ######### for some of my data that was larger than the field itself. So I used **sheets.UsedRange.Columns.AutoFit();** to solve the problem every time a new column is created. Where sheets is my variable who received the **Microsoft.Office.Interop.Excel.Worksheet**.

Thank you guys very much.

Upvotes: 2

Jayant Varshney
Jayant Varshney

Reputation: 1825

Try getting the range and then do Autofit

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

Upvotes: 19

user2125311
user2125311

Reputation: 67

I found this on another page:

C#

http://www.spreadsheetgear.com/support/help/spreadsheetgear.net.3.0/SpreadsheetGear~SpreadsheetGear.IRange~AutoFit.html

// Automatically set the width on columns B and C. worksheet.Cells["B:C"].Columns.AutoFit();

// Set the row height to automatic on rows 7 through 9. worksheet.Cells["7:9"].Rows.AutoFit();

Upvotes: 1

Related Questions