facebooktwittermenuarrow-up

GemBox Support Forum

Cell width problem

i try set cellwidth, with all type lengthunit,but the size always different with default width size
i using microsoft excel 2007.
what size lengthunit same as default excel.

Hi,

I’m not sure what was the default in Microsoft Excel 2007.
Nevertheless, in Microsoft Excel 2019 the default font is Calibri 11pt which results in default column width of 48pt (or 64px).

So, try the following:

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Columns[0].SetWidth(64, LengthUnit.Pixel);
workbook.Save("Output.xlsx");

Does this solve your issue?

Regards,
Mario

Hi,

I am a friend who asked before.
This code:

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Columns[0].SetWidth(64, LengthUnit.Pixel);
workbook.Save("Output.xlsx");

did not solve our problem.

When we use worksheet.Columns[0].SetWidth(64, LengthUnit.Pixel); then the width in excel is 8.
When we use worksheet.Columns[0].SetWidth(8*8, LengthUnit.Pixel); then the width in excel is 8.

so we test by making worksheet.Columns[0].SetWidth(8*9, LengthUnit.Pixel); but the result is 10.

we don’t know how the calculation is, here are some cases we made:
.Columns("A").SetWidth(64, LengthUnit.Pixel) => the result in excel 8
.Columns("B").SetWidth(8 * 9, LengthUnit.Pixel) => the result in excel 10
.Columns("C").SetWidth(8 * 10, LengthUnit.Pixel) => the result in excel 11
.Columns("D").SetWidth(8 * 11, LengthUnit.Pixel) => the result in excel 12
.Columns("E").SetWidth(8 * 16, LengthUnit.Pixel) => the result in excel 18
.Columns("F").SetWidth(8 * 17, LengthUnit.Pixel) => the result in excel 19

please help.

Hi Ade,

Try using this method:

private static void SetWidth(ExcelColumn column, double width)
{
    column.SetWidth(
        Math.Truncate((width * 7 + 5) / 7 * 256) / 256,
        LengthUnit.ZeroCharacterWidth);
}

Here is a small sample code that uses it:

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");

SetWidth(worksheet.Columns["A"], 8);
SetWidth(worksheet.Columns["B"], 9);
SetWidth(worksheet.Columns["C"], 10);
SetWidth(worksheet.Columns["D"], 11);
SetWidth(worksheet.Columns["E"], 16);
SetWidth(worksheet.Columns["E"], 17);

workbook.Save("Output.xlsx");

I hope this helps.

Regards,
Mario

Hi Mario,

The code solves our problem. With that Code now the column values ​​are in accordance with what we set.

Thank you very much