Hi,
see attached sample: it loads a template with a named style and writes some cell values. The column width is set using “SetColumnWidthInch”, and I convert a millimeter value to inch.
The result is saved to “bin\Debug\result.xlsx” - also contained in zip. ColWidth.zip (32.5 KB)
Column B shall have a width of 35mm (1.378 inch)
Now I check the original column width: in the “Normal” view column B has a width of 21.17. In the “Page layout” view, column B has a width of 3,18cm. This is a bit away from the 3.5 cm specified in the code: colwidth_original.png (26.8 KB)
So, in the “Page Layout” I modify the col width to “3.5 cm”. Excel rounds this value to “3.49cm”. The col width in “Normal” view is now “23.5”: colwidth_modified.png (25.6 KB)
This is the col width that I expect as a result of my sample code.
It looks like an MS Excel’s behavior and nothing to do with Aspose.Cells APIs. I tried to manually extend widths of a few columns in Excel manually and found the same thing as you mentioned.
Could you perform the task in MS Excel manually and check if you find the same thing or not and do let us know.
The column width is 132 pixel in “Normal” view. In 100% DPI setting of your machine, 3.5 cm = 3.5 /2.54 * 96 pixel = 132 pixel , it is right. See ColumnWidth_NormalView.png (71.5 KB)
The “Page layout” view is something like the printview.(In Microsoft Excel, File->Print). It has some scale in horizontal and vertical direction.
Well, I am not convinced that it is only an excel issue.
Attached is a “real” excel file. Unfortunately you would need some customers fonts, but I hope you see the issue anyway:
When creating the report, I set the width of col B to “3.5cm” and col C to “2.8cm”.
But in the “Page Layout” view, the widths are 3.11 cm and 2.49 cm. This is a difference of 10%. Wochenplan (Excel)_2021-03-11_10-17-29.zip (9.3 KB)
When printing the file, the path widths of the printed paper match the “wrong” widths of the page layout view.
I expect a width of 3.5cm (or a value quite near) when I set the inch width of the column. The 35mm are 1.37795 inch in my code, which matches your value.
It seems that you want to set column width in “Page layout” view. Please first set Sheet.ViewType to ViewType.PageLayoutView, then use the API Cells.SetViewColumnWidthPixel(int column, int pixels) to set column witdth in “Page layout” view.
Code:
Workbook workbook = new Workbook("..\\..\\template_Calibri.xlsx");
Worksheet sheet = workbook.Worksheets[0];
//first set sheet ViewType to PageLayoutView
sheet.ViewType = ViewType.PageLayoutView;
...
//column B
sheet.Cells.SetViewColumnWidthPixel(1, (int)(35.0 / 25.4 * 96));
//column C
sheet.Cells.SetViewColumnWidthPixel(2, (int)(28.0 / 25.4 * 96));
workbook.Save("result.xlsx");
Can you confirm that the column will now have the correct width when printing? This is my final goal - setting a column width so that it will have the correct mm width when printing it.
Do you have any explanation why this works different than “SetColumnWidthInch”?
Can you confirm that the column will now have the correct width when printing? This is my final goal - setting a column width so that it will have the correct mm width when printing it.
sorry, I still don’t understand why “SetColumnWidthInch” does not work as expected. Could you please provide an explanation?
To me, it still seems to be a bug - so please convince me that I am wrong ;-).
It seems “SetColumnWidthInch” is quite useless, because it will never have the expected result.
In my sample, when calling “sheet.Cells.SetColumnWidth(1, 23.5);”, the resulting col width in excel is as expected, but calls to “GetColumnWidthInch” return wrong values. So I think that you do the conversion wrong?
Even when using an empty Workbook instead of a template file, the results are wrong.
I still don’t understand why “SetColumnWidthInch” does not work as expected. Could you please provide an explanation?
I have explained:
The Cells.SetColumnWidthInch API is not aware of Sheet ViewType , it always set column width in ‘Normal’ view.
In my sample, when calling “sheet.Cells.SetColumnWidth(1, 23.5);”, the resulting col width in excel is as expected, but calls to “GetColumnWidthInch” return wrong values. So I think that you do the conversion wrong?
Cells.SetColumnWidthInch and Cells.SetColumnWidth are different. The value set to Cells.SetColumnWidthInch is in inch. But the value set to Cells.SetColumnWidth is in numbers of charactors.
I tried the following code, and check the column width in “Normal” view in Microsoft Exce, it is OK.
I still don’t understand why Excel has this strange behavior. But I give up here and I will change our code to use “SetViewColumnWidthPixel” - this should set the exact columns widths for printing.
Yes, using the API Cells.SetViewColumnWidthPixel(int column, int pixels) will set column width in “Page layout” view and it will give you correct width when printing.
In the event of further queries or issue, feel free to write us back.