Hi, If i try to get the number of data columns in a worksheet by using following piece of code:
worksheet.getCells().getColumns().getCount();
I dont get the actual count. I get the count as 1 even though there are more than one data columns.
Thanks & Regards,
Rohan .
Hi,
Thanks for your posting and using Aspose.Cells for Java.
Please download and use the latest version:
Aspose.Cells for Java (Latest Version)
You should use MaxColumn property to access the maximum column including its data and style.
And you should use MaxDataColumn property to access the maximum column including its data only.
Please try one of these methods for your needs.
Java
worksheet.getCells().getMaxColumn();
worksheet.getCells().getMaxDataColumn();
Hi. Thanks. Also there isn't any documentation on use of Iterators i.e. columns iterator, row iterator and cells iterator. Can i get some sample code on this?
Thanks & Regards,
Rohan.
Hi,
Please see the code below. It provides the example how to use iterator.
The code basically creates a range from first cell to last cell. Please see the source.xlsx file.
Once, the range is created, it gets its iterator and then prints all the values of cell using a while loop.
Please see its output.
Java
String path = “F:\Shak-Data-RW\Downloads\Source.xlsx”;
Workbook workbook = new Workbook(path);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cell cell = worksheet.getCells().getLastCell();
String lastCellName = cell.getName();
//System.out.println(lastCellName);
//Create range that will include all cells
//and print the values of all cells
Range rng = worksheet.getCells().createRange(“A1”, lastCellName);
Iterator allCells = rng.iterator();
while(allCells.hasNext())
{
Cell nextCell = allCells.next();
//Print cell value
System.out.println(nextCell.getStringValue());
}
Output:
434
312
564
505
132
885
520
307
749
695
160
507
1000
590
53
626
563
656
363
945
52
115
751
405
820
785
405
896
391
20
953
584
58
867
464
432
962
651
473
646
494
951
660
787
558
886
973
319
756
681
142
25
145
436
411
726
479
52
496
463
257
584
880
439
480
437
545
403
497
334
450
273
797
45
584
998
4
114
74
738
827
280
601
602
586
667
474
260
474
372
874
446
30
459
752
149
674
630
695
263
468
879
591
134
976
315
723
520
407
268
903
385
108
904
22
84
996
147
459
639
220
870
895
329
359
42
517
446
56
217
538
326
899
325
27
700
42
541
203
807
724
737
832
272
178
348
927
641
83
447
589
226
264
81
343
47
55
462
326
661
351
75
747
395
659
919
69
726
444
64
398
619
246
112
85
84
605
302
851
644
904
973
180
770
Hi. I want the use of Column and Row iterators. i.e.
worksheet.getCells().getColumns().iterator() and worksheet.getCells().getRows().iterator()
For iterating thru a particular column.
Hi,
Please see this code.
It iterate all the rows and all the cells inside those rows using Iterator.
Please see the source.xlsx file and output of this code below.
Java
String filePath = “c:\source.xlsx”;
Workbook workbook = new Workbook(filePath);
Worksheet worksheet = workbook.getWorksheets().get(0);
RowCollection rows = worksheet.getCells().getRows();
Object obj = rows.iterator().next();
Iterator rowIterator = worksheet.getCells().getRows().iterator();
while(rowIterator.hasNext())
{
Row r = rowIterator.next();
Iterator cellIterator = r.iterator();
while(cellIterator.hasNext())
{
Cell cell= cellIterator.next();
System.out.println(cell.getStringValue());
}
}
Output:3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
3
4
5
6
7
Hi,
Thanks. And can i iterate through a particular column using ColumnIterator?
Hi,
You need to create a column iterator using the createRange method.
Please use the following code to iterate column cells. You can make this code generic.
Java
//Iteratre the first 1000 cells of column A
Iterator columnCells = cells.createRange(“A1:A1000”).iterator();