java - Printing contents of xlsx sheet -
i'm using docx4j in eclipse contents of excel sheet i'm getting numbers. simplicity, assume sheet:
| asd | sd |
| hgn |
the code i'm using load contents is:
public static void load(string outputfilepath) throws filenotfoundexception{ try { spreadsheetmlpackage exc = spreadsheetmlpackage .load(new java.io.file(outputfilepath)); worksheetpart sheet = exc.getworkbookpart().getworksheet(0); system.out.println(sheet.getpartname().getname()); worksheet ws = sheet.getjaxbelement(); sheetdata data = ws.getsheetdata(); int ic = 0; (row r : data.getrow()) { system.out.println("row " + ic); int ir = 0; (cell c : r.getc()) { system.out.println("cell " + ir + " contains " + c.getv().tostring()); ir++; } ic++; } system.out.println("\ndone"); } catch (docx4jexception e) { e.printstacktrace(); } catch (xlsx4jexception e) { e.printstacktrace(); } }
and output:
/xl/worksheets/sheet1.xml row 0 cell 0 contains 0 cell 1 contains 1 row 1 cell 0 contains 2 done
what should actual content?
note: problem occurs strings. ie if there numbers in cell them without problems.
within xlsx text contents of cells not stored directly in sheet xml. there sharedstrings.xml
in xlsx archive.
thats why docx4j
not read text content getv()
index of content in sharedstrings.xml
. have index , can content sharedstrings.
see example: https://github.com/plutext/docx4j/blob/master/src/samples/xlsx4j/org/xlsx4j/samples/partslist.java
... (cell c : r.getc() ) { if (c.gett().equals(stcelltype.s)) { system.out.println( " " + c.getr() + " contains " + sharedstrings.getjaxbelement().getsi().get(integer.parseint(c.getv())).gett() ); } else { // todo: handle other cell types system.out.println( " " + c.getr() + " contains " + c.getv() ); } } ...
note have read first relationshipspart
sharedstrings
. done in example within public static void printinfo(part p, stringbuilder sb, string indent)
while traversing relationships in public static void traverserelationships
.
numbers , formulas directly stored in sheet xml. contents getv()
content directly.
Comments
Post a Comment