Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Auto size Excel ClosedXml cells in c#

Tags:

c#

excel

autosize

I am trying to resize cells so that it fits the maximum length of the text using ClosedXMl.Excel but the cell is giving me this error message:

Severity Code Description Project File Line Suppression State Error CS0119 'IXLRangeBase.Cells()' is a method, which is not valid in the given context

C#:

var workbook = new XLWorkbook();     //creates the workbook
var wsDetailedData = workbook.AddWorksheet("data"); //creates the worksheet with sheetname 'data'

wsDetailedData.Cell(1, 1).InsertTable(dataList); //inserts the data to cell A1 including default column name
wsDetailedData.Cells.SizeToContent = true;
workbook.SaveAs(@"C:\data.xlsx"); //saves the workbook
like image 905
Decoder94 Avatar asked Sep 13 '17 14:09

Decoder94


People also ask

How do I wrap text in ClosedXML in Excel?

Style. Alignment. WrapText = true; generated. SaveAs("Generated.

What is ClosedXML Excel?

ClosedXML is a . NET library for reading, manipulating and writing Excel 2007+ (. xlsx, . xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.


1 Answers

You can try to adjust the column & Rows instead of adjusting the cell:

wsDetailedData.Columns().AdjustToContents();  // Adjust column width
wsDetailedData.Rows().AdjustToContents();     // Adjust row heights
like image 154
Rahul Tripathi Avatar answered Oct 19 '22 12:10

Rahul Tripathi