Free Support Forum - aspose.com

Columns.Count reads as 0. Shouldn't it be 3?

Sorry if I’m using the library incorrectly. Why does this program get 0 rather than 3 for the columCount? I’m using 6.0.0.0


private static void Doit() {
var sample=@“a,b,c
1,2,3
”;
var wb=new Workbook(new MemoryStream(Encoding.ASCII.GetBytes(sample)), new LoadOptions(LoadFormat.CSV));
var sht=wb.Worksheets[0];
var numCells=sht.Cells.Count; //returns 6
var columnCount=sht.Cells.Columns.Count; // returns 0
Debug.Print(“numCells={0}, columnCount={1}”, numCells, columnCount);
}

Hi,

Well, you slightly misunderstood the Cells.Columns.Count attribute. The property would give you columns count but, the columns with standard (default) settings / formatting are not included to be counted. For your information, the Rows.Count and Columns.Count are mainly useful if you want to retrieve the number of rows / columns which are initialized / used in a worksheet and also those rows / columns other than with default formatting/setting in a worksheet.

Please always use Cells.MaxDataColumn / Cells.MaxDataRow and Cells.MaxColumn/Cells.MaxRow attributes to get the farthest column and row indexes. So, you should change your sample code accordingly.

For complete reference for using these attributes and others in that series, please check the thread: http://www.aspose.com/community/forums/178764/column-and-row-count-invalid/showthread.aspx#178764

If you still have any confusion, let us know.

Thank you.

Thank you for your reply. Below is a similar program that does not use the "Count" property but instead iterates over the collection of columns. This new program also behaves the same way.

I suppose you will say that this is also the expected behavior. My question is: (a) why should it work this way rather than the more intuitive way; and (b) if it has to work this way, why is this not documented? The documentation for Cells.ColumnCollection simpy says "Gets the collection of Column objects that represents the individual columns in this worksheet.". The documentation says nothing about only returning special columns with non-default formatting.

I believe the behavior should be changed. But at the very least, the documentation should be fixed.

Documentation: http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/aspose.cells.cells.columns.html

Program:

private static void Doit() {
var sample=@"a,b,c
1,2,3
";
File.WriteAllText("test.csv", sample);
var wb=new Workbook("test.csv", new LoadOptions(LoadFormat.CSV));
var sht=wb.Worksheets[0];
var firstCount=sht.Cells.Columns.Cast().ToList().Count; // returns 0, showing that the IEnumerable returns no items
var col0=sht.Cells.Columns.GetColumnByIndex(0);
var secondCount=sht.Cells.Columns.Cast().ToList().Count; // returns 1, showing that the IEnumerable returns one item
}

Hi,

Well, for Cells.Columns.Count, the columns with standard (default) settings / formatting are not included. And, we will update the related API documents soon.
Thank you.