简体   繁体   中英

Aspose Cells for Java: get original CSV row

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM