Page 1 of 1

Getting cell values

PostPosted: Thu Nov 19, 2015 4:19 am
by finder
Hello. I just started learning about xlsx4j. I'm having difficulty get the values from the cells. I have a simple excel file with a few text found in the rows and columns, these are "hello" and "there".
Below is my code:

SpreadsheetMLPackage spread = SpreadsheetMLPackage.load(new java.io.File(excelPath));

WorkbookPart wbPart = spread.getWorkbookPart();
WorksheetPart wsPart = wbPart.getWorksheet(0);

SheetData sheetData = wsPart.getContents().getSheetData();

List<Row> rows = wsPart.getContents().getSheetData().getRow();

for(int i =0; i<rows.size();i++){
List<Cell> cell = rows.get(i).getC();
for(int j=0;j<cell.size();j++){
Cell c = cell.get(j);
System.out.println(c.getR() + " contains --> " + c.getV());
}
}

the result is:
A1 contains --> 0
B1 contains --> 1
A2 contains --> 0
B2 contains --> 1
A3 contains --> 0
B3 contains --> 1
A4 contains --> 0
B4 contains --> 1
A5 contains --> 0
B5 contains --> 1

Any ideas why it's returning a number rather than a text?

Thanks in advance :)

Re: Getting cell values

PostPosted: Thu Nov 19, 2015 6:20 am
by finder
I just found out to use sharedstrings to get the values. just weird why there is a sharedstrings rather than accessing via cell location.

Re: Getting cell values

PostPosted: Fri Jun 16, 2017 10:53 pm
by schlebe
Yes, I agree with finder.

Why not simply write a function that return the value when Type is String but also even Type is Integer or Date.

I have seen a similar function that do that but when I try it

Code: Select all
import org.apache.poi.ss.usermodel.DataFormatter;
DataFormatter formatter = new DataFormatter();
s = formatter.formatCellValue(r.getC().get(0));

the Java compiler display "incompatible types: org.xlsx4j.sml.Cell cannot be converted to org.apache.poi.ss.usermodel.Cell"

But I'm not sure that DataFormatter class must be imported from Apache-POI !

Re: Getting cell values

PostPosted: Sat Jun 17, 2017 6:33 am
by jason
See https://github.com/plutext/docx4j/blob/ ... actor.java
which uses org.xlsx4j.org.apache.poi.ss.usermodel.DataFormatter

Re: Getting cell values

PostPosted: Mon Jul 03, 2017 4:28 pm
by schlebe
Thanks for your help.

My error is to use org.apache.poi.hssf.usermodel.HSSFDateUtil;
instead of import org.xlsx4j.org.apache.poi.ss.usermodel.DataFormatter;