programmatically excel cells to be auto fit width

2020-08-26 03:04发布

问题:

when i convert datatable to .csv my excel sheets are generated as below:

and tried like below:

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

then my excel sheets were like:

note that cells have =" " characters;

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

回答1:

Try getting the range and then do Autofit

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


回答2:

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.



回答3:

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();