OpenXML:在 Excel 中自动调整列宽 [英] OpenXML: Auto Size column width in Excel

查看:175
本文介绍了OpenXML:在 Excel 中自动调整列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码来使用 OpenXML 生成 Excel 文件.下面是在 Excel 中生成列的代码.

I have written a code to generate Excel file using OpenXML. Below is the code which generates the Columns in the Excel.

Worksheet worksheet = new Worksheet();
Columns columns = new Columns();
int numCols = dt1.Columns.Count;
for (int col = 0; col < numCols; col++)
{
    Column c = CreateColumnData((UInt32)col + 1, (UInt32)numCols + 1, 20.42578125D);

    columns.Append(c);
}
worksheet.Append(columns);

另外,我尝试在下面的行中创建列.

Also, I tried below line to create columns.

Column c = new Column
{
    Min = (UInt32Value)1U,
    Max = (UInt32Value)1U,
    Width = 25.42578125D,
    BestFit = true,
    CustomWidth = true
};

我认为使用 BestFit 应该可以.但它不会设置自动大小.

I thought using BestFit it should work. But it doesn't set the auto size.

推荐答案

BestFit 属性是一个信息属性(可能用于 Excel 优化).您仍然需要提供列的宽度.这意味着您必须根据单元格内容实际计算列宽.Open XML SDK 不会自动为您执行此操作,因此您最好为此使用第三方库.

The BestFit property is an information property (possibly for optimisation by Excel). You still need to provide the Width for the Column. This means you have to actually calculate the column width depending on the cell contents. Open XML SDK doesn't do this automatically for you, so it's better that you use a third-party library for this.

这篇关于OpenXML:在 Excel 中自动调整列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆