如何自动调整大小 Excel C# 中的 ClosedXml 单元格

How to Auto size Excel ClosedXml cells in c#

我正在尝试使用 ClosedXMl.Excel 调整单元格大小以适合文本的最大长度,但单元格给我以下错误消息:

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

您可以尝试调整列和行,而不是调整单元格:

wsDetailedData.Columns().AdjustToContents();  // Adjust column width
wsDetailedData.Rows().AdjustToContents();     // Adjust row heights