C# EPPlus 中的自动列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15642950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:27:18  来源:igfitidea点击:

Auto column width in EPPlus

c#.netvb.netepplus

提问by Pengan

How to make columns to be auto width when texts in columns are long?

当列中的文本很长时,如何使列自动宽度?

I use this code

我用这个代码

 Worksheet.Column(colIndex).AutoFitColumn() 'on all columns'
 Worksheet.cells.AutoFitColumns()
 Worksheet.Column(colIndex).BestFit = True  'on all columns'

None of these methods are working

这些方法都不起作用

Are there any ways to make it work?

有什么方法可以使它工作吗?

Note: Some of my texts use Unicode.

注意:我的一些文本使用 Unicode。

回答by Tim Schmelter

Use AutoFitColumns, but you have to specify the cells, i assume the entire worksheet:

使用AutoFitColumns,但您必须指定单元格,我假设整个工作表:

VB.NET

网络

Worksheet.Cells(Worksheet.Dimension.Address).AutoFitColumns()

C#

C#

Worksheet.Cells[Worksheet.Dimension.Address].AutoFitColumns();

Please note you need to call this method after filling the worksheet.

请注意,您需要在填写工作表后调用此方法。

回答by ffffff01

You will need to calculate the width. There is no autosizing function in the library that will work as you intend.

您将需要计算宽度。库中没有自动调整功能可以按您的意愿工作。

Autofitcolumn will not work with wrapped text and cells with formulas.

Autofitcolumn 不适用于带公式的换行文本和单元格。

Look at http://epplus.codeplex.com/discussions/218294?ProjectName=epplusfor examples of how you can solve the problem.

查看http://epplus.codeplex.com/discussions/218294?ProjectName=epplus有关如何解决问题的示例。

回答by Daniele Armanasco

I have used this code with the version 3.1.3.0 of EPPlus and it is working:

我已将此代码与 EPPlus 3.1.3.0 版一起使用,并且正在运行:

worksheet.Column(1).AutoFit();

where a worksheet is the variable referencing the worksheet I have created in my code (not a class with a static method!).

其中工作表是引用我在代码中创建的工作表的变量(不是具有静态方法的类!)。

Obviously you have to call this method after you have filled the columns.

显然,您必须在填充完列后调用此方法

回答by xtds

Had to use worksheet.Column(1).AutoFit(0);AutoFit() wasn't doing the trick.

不得不使用worksheet.Column(1).AutoFit(0);AutoFit() 并不能解决问题。

回答by Jhonny Nina

I use this and is working well.

我使用这个并且运行良好。

Dim objExcel As New ExcelPackage
Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName")
Sheet.Cells("B1:BN").AutoFitColumns()

回答by Jose

I know is a little bit late but I've had the same problem today. If you have a worksheet.DefaultColWidthdefined, it won't work. I've removed that line and added Worksheet.cells.AutoFitColumns();and it works now.

我知道有点晚了,但我今天遇到了同样的问题。如果你有一个worksheet.DefaultColWidth定义,它不会工作。我已经删除了该行并添加了Worksheet.cells.AutoFitColumns();它,现在它可以工作了。

回答by TrailTrackers

I know this is an old question, but I use the code below and it seems to directly address what you have tried to do.

我知道这是一个老问题,但我使用下面的代码,它似乎直接解决了您尝试做的事情。

using (var xls = new ExcelPackage())
{
    var ws = xls.Workbook.Worksheets.Add("Some Name");

    //**Add Column Names to worksheet!**
    //**Add data to worksheet!**

    const double minWidth = 0.00;
    const double maxWidth = 50.00;

    ws.Cells.AutoFitColumns(minWidth, maxWidth);

    return pkg.GetAsByteArray();
}

回答by leiit

It's working just fine for me.

它对我来说很好用。

Try:

尝试:

ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
ExcelPkg.SaveAs();

回答by johnny 5

Just wanted to point out you can fit cells with out specifying the range, just make sure to call this after you've formatted all columns etc:

只是想指出您可以在不指定范围的情况下适应单元格,只需确保在格式化所有列等后调用它:

worksheet.Cells.AutoFitColumns()

回答by Ondrej Rozinek

The .NET Core as a successor of .NET doesn't support anymore the function autofit cells with EPPplus library.

.NET Core 作为 .NET 的后继者不再支持带有 EPPplus 库的函数自动调整单元。

worksheet.Cells.AutoFitColumns();

or

或者

worksheet.Column(1).AutoFit();

causes exception:

导致异常:

"System.Drawing is not supported on this platform."

The System.Drawing assembly is dependent on GDI and Windows specific libraries which have to be replaced by another solution. The solution for this issue is to me unknown.

System.Drawing 程序集依赖于 GDI 和 Windows 特定的库,这些库必须由另一个解决方案替换。这个问题的解决方案对我来说是未知的。