programmatically excel cells to be auto fit width & height

时间秒杀一切 提交于 2020-01-02 01:20:13

问题


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



来源:https://stackoverflow.com/questions/12711163/programmatically-excel-cells-to-be-auto-fit-width-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!