Finding Column Width

I am attempting to cleanup an Excel worksheet after copying the contents from another worksheet. After the copy, all of my row height and column width formatting is lost. I have a property that I have created "Properties.TopElementWidth" that I can use to reset my columns, however, I do not want all columns to be this same width.

I am trying to loop through the columns of my top row, autofit, then only if the resulting column size is greater than the value of my property, set it equal to the size defined in that property.

Everything works in the code below except where I attempt to find what the current column width is.
if (allWorksheet.Cells[0, c].Column.Width > Properties.TopElementWidth)

I can't seem to find any way to return what the width is of a given column. I understand that "column.width" is not a valid syntax, but that is essentially what I am trying to get at.

Any help would be appreciated.

Here is my example:

aRange = allWorksheet.Cells.CreateRange(0, 0, allWorksheet.Cells.MaxDataRow, allWorksheet.Cells.MaxDataColumn);

allWorksheet.AutoFitColumns();

for (Int32 c = aRange.FirstColumn; c < aRange.ColumnCount; c++)
{
if (allWorksheet.Cells[0, c].Column.Width > Properties.TopElementWidth)
{
allWorksheet.Cells[0, c].Column.Width = Properties.TopElementWidth
}
}

allWorksheet.AutoFitRows();

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.2.0.7


Please use the following code, it will copy the entire contents of your worksheet from source workbook into your destination workbook and column widths will not be affected too.

So, you do not now need to worry about column widths after modifying your code according to the code given below.

I have attached both the source and output file generated by this code for your reference. Please also see the screenshot below.

C#



string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook sourceWorkbook = new Workbook(filePath);

Workbook destWorkbook = new Workbook();


destWorkbook.Worksheets.Clear();


foreach (Worksheet sourceSheet in sourceWorkbook.Worksheets)

{

destWorkbook.Worksheets.Add();

destWorkbook.Worksheets[destWorkbook.Worksheets.Count - 1].Copy(sourceSheet);

}

destWorkbook.Save(filePath + “.out.xlsx”, SaveFormat.Xlsx);


Screenshot:

Thank you for the reply. I attempted to download and use the newer ddl and was faced with a daunting task of updating deprecated style properties. unfortunately, once I updated all of my code I ran into an issue with the license. I am using an older version of Aspose. Is there a limit to the version level that you can update to?


That issue aside, I would still like to know if there is a way to return the set width of a given column.

This seems like a pretty standard function. If I was using VBA I would simply use

If Column.Width > X Then Do Something.

It seems like I should be able to do this through Aspose as well.



Hi,


Well, if you need to get/set a column’s width, I think you use Cells.GetColumnWidth and Cells.SetColumnWidth methods. Here is the complete list of methods that you may utilize:
Get:
GetColumnWidth
GetColumnWidthInch
GetColumnWidthPixel

Set:
SetColumnWidth

SetColumnWidthInch
SetColumnWidthPixel

See the topic for your reference.
http://www.aspose.com/docs/display/cellsnet/Adjusting+Row+Height++and++Column+Width

Thank you.