We have purchased Aspose.Cells site license and using it successfully. Suddenly we found an issue with some excel files where column count is returned as 0 when there are columns.
Code:
string fileName = args[0];
Workbook test = new Workbook();
test.Open(fileName, FileFormatType.Excel2003);
Worksheet worksheet = test.Worksheets[0];
Console.WriteLine("Columns :" + worksheet.Cells.Columns.Count); // Returned as 0 - SHould BE 2
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. Since all the columns in the sheet are with defaults formatting, so it will give you 0. 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 and Cells.MaxDataRow attributes to get the farthest column and row indexes.
Well, MaxDataRow and MaxDataColumn work fine and give correct values. It is to be noted here, both attributes return Zero-based indexed values for rows and columns.
So, if the MaxDataColumn returns "1", it means that there are two columns in count in the worksheet and the farthest column would be B (i.e.., 0-A, 1-B)................which is true.
If the MaxDataRow returns "3", it means that there are 4 rows filled with data and the farther row is 4th row.........................which is true again.
I've been trying to find out how to get the number of cells containing data and for each property there seems to be a catch. For example, in this case, MaxDataRows and MaxDataColumns give the same result if you have a worksheet with one cell containing data, and a worksheet that is totally empty. They both give counts of zero.
Is there a way to distinguish between empty sheets and sheets with only one cell?
I think, this problem of distinguishing empty sheet or sheet with one cell can be solved, but first I need to test it myself. I will let you know as soon as possible.
Hi, I am trying to get the colum count using the attached file. But i am getting 0 columns. If i open this file and click save as then i am getting the attached popup i.e. this file may contains format which may not compatable with CSV. If i select No option in the popup and save the file again as CSV and try to get the column count with the saved file i am able to get the column count. Can you help me to get the coulumn count with the formatted an unformatted files by using the same property.
Can you please help me ASAP. This is very importent work i need to do.
Please use Cells.MaxColumn to get the last column’s index which contains data or style.
Please use Cells.MaxDataColumn to get the last column’s index which contains data only.
See the sample code that works fine with your attached file.
Sample code:
TxtLoadOptionstxtLoadOptions = new TxtLoadOptions(LoadFormat.CSV);
Workbook workbook = new Workbook(“e:\test2\CSV Sample File.csv”,txtLoadOptions);
Worksheet sheet = workbook.Worksheets[0];
Debug.WriteLine(sheet.Cells.MaxDataColumn); //Get the farthest column index which contains data = 71 - OK (since columns are zero indexed based, so total column = 72)
Debug.WriteLine(sheet.Cells.MaxColumn); //Get the farthest column index which contains data or style/formattings = 71 - OK (since columns are zero indexed based and no style/formatting is applied, so total column = 72)
Hi Thank you so much for your quick response. I am also using the Cells.MaxDataColumn. But still i am getting the column count as 0. I am using the below code.
Well, I have checked your issue and found your so called CSV file “Sample+CSV” is actually a tab delimited Unicode text file, so it is not a conventional CSV file. For confirmation, you may open the file into notepad or open the file into MS Excel and try to save it as (do not do this though), you will see “Unicode text” as save as type in the Save dialog box in MS Excel. I used the following code which works absolutely fine with our latest version v7.4.2.x(please try it downloading from Aspose site (Downloads module/section)):
Sample code:
TxtLoadOptions txtLoadOptions = new TxtLoadOptions(LoadFormat.TabDelimited);
txtLoadOptions.Encoding = Encoding.Unicode;
Workbook workbook = new Workbook(“e:\test2\Sample+CSV.csv”, txtLoadOptions);
Worksheet sheet = workbook.Worksheets[0];
Debug.WriteLine(sheet.Cells.MaxDataColumn); //Get the farthest column index which contains data = 71 - OK (since columns are zero indexed based, so total column = 72)
Debug.WriteLine(sheet.Cells.MaxColumn); //Get the farthest column index which contains data or style/formattings = 71 - OK (since columns are zero indexed based and no style/formatting is applied, so total column = 72)
As you are using some older version i.e. v4.7.x of the product for which I am not sure if it has these new attributes or options, but you may try to specify Tab Delimited as FileFormatType while loading the file if it works. If it does not work as expected, I am afraid, you have to upgrade to and use the latest versions of the product as there is no other alternative to sort it out.
For your information, over the years we have made tremendous enhancements and included many new features and functionality, the product has become more robust and feature rich component now.