0

I am using Aspose to read a CSV file.

I do not beforehand know the number of cells for each row of the file, but I will need to know it for further processing.

Unfortunately, I see no way to find out the number of cells in a CSV row.

Imagine the following row in the CSV file. It contains 7 cells, 4 of which are empty:

1,2,,4,,,

Using

row.iterator();

Aspose will only return 3 cells, as it ignores all empty cells.

As an alternative, I now do the following:

    Cell lastCell = row.getLastCell();

    int count = 0;
    do {
        cell = row.getCellOrNull(count);
        String cellValue = cell == null ? "" : cell.getStringValueWithoutFormat();

        //do something with the cell value...

        count++;
    } while (cell == null || ! lastCell.equals(cell));

This works better, as it returns the first 4 cells. However, it still ignores the last 3 cells .

Is there any way to get information about the missing cells? (It would be sufficient for me if Aspose could return the original Row as a String - I could then count the number of commas and find out the number of cells this way)

stefan.m
  • 1,912
  • 4
  • 20
  • 36

1 Answers1

0

You may use Worksheet.getCells().getMaxDisplayRange() method to get the maximum display range.

Please consider this CSV. If you open it in MS-Excel and check the last cell, you will find it is Q2

Book1.csv

2,,,,1,,,,,,,,,,,,,
,,3,,,,

Aspose.Cells returns the same via the following code.

TxtLoadOptions opts = new TxtLoadOptions(LoadFormat.CSV);

Workbook wb = new Workbook("Book1.csv", opts);
Worksheet ws = wb.getWorksheets().get(0);
Range rng = ws.getCells().getMaxDisplayRange();

System.out.println(rng);

Here is the console output of the code.

Console Output

Aspose.Cells.Range [ Sheet1!A1:Q2 ]

Note: I am working as Developer Evangelist at Aspose

shakeel
  • 1,717
  • 10
  • 14
  • Unfortunately, I have different rows with different number of cells each. With your solution, I only get the number of cells for the longest row. – stefan.m Apr 28 '16 at 12:29
  • I have looked into this issue and I thought, instead of using Aspose.Cells, you can directly read csv lines using Java APIs and then count commas inside it. I tried some Java codes but unfortunately, those codes do not read empty csv lines, but they do read those lines which have at least one cell non-empty. You can also post your issue in Aspose.Cells forums, then we will add a new feature in Aspose.Cells to fulfill your requirements. – shakeel Apr 30 '16 at 09:29
  • Thanks, I posted this here: http://www.aspose.com/community/forums/showthread.aspx?PostID=719442&Subj=aspose-cells-for-java-get-original-csv-row#719442 – stefan.m May 02 '16 at 11:07
  • @stefan.m, Please check your thread in Aspose.Cells support forum for possible solutions for presented scenario. http://www.aspose.com/community/forums/permalink/719442/719558/showthread.aspx#719558 – Prorata May 05 '16 at 08:18
  • Note: Aspose informed me that this will not be implemented. See http://www.aspose.com/community/forums/showthread.aspx?PostID=719442&Subj=aspose-cells-for-java-get-original-csv-row#719442 – stefan.m Jun 20 '16 at 15:05