简体   繁体   English

Java的Aspose Cells:获取原始CSV行

[英]Aspose Cells for Java: get original CSV row

I am using Aspose to read a CSV file. 我正在使用Aspose读取CSV文件。

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. 不幸的是,我无法找到CSV行中的单元格数量。

Imagine the following row in the CSV file. 想象一下CSV文件中的以下行。 It contains 7 cells, 4 of which are empty: 它包含7个单元格,其中4个为空:

1,2,,4,,,

Using 使用

row.iterator();

Aspose will only return 3 cells, as it ignores all empty cells. Aspose将仅返回3个单元格,因为它将忽略所有空单元格。

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. 这样效果更好,因为它返回前4个单元格。 However, it still ignores the last 3 cells . 但是,它仍然忽略最后3个单元格。

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) (如果Aspose可以将原始的Row作为字符串返回,这对我就足够了-然后我可以计算逗号的数量并以此方式找出单元格的数量)

You may use Worksheet.getCells().getMaxDisplayRange() method to get the maximum display range. 您可以使用Worksheet.getCells()。getMaxDisplayRange()方法来获取最大显示范围。

Please consider this CSV. 请考虑此CSV。 If you open it in MS-Excel and check the last cell, you will find it is Q2 如果您在MS-Excel中打开它并检查最后一个单元格,则会发现它是Q2

Book1.csv Book1.csv

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

Aspose.Cells returns the same via the following code. Aspose.Cells通过以下代码返回相同的结果。

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 注意:我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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