http://bbs.csdn.net/topics/380257685
Import Java.io.file;import java.io.ioexception;import Java.io.inputstream;import Java.util.zip.zipentry;import Java.util.zip.zipexception;import Java.util.zip.zipfile;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Org.w3c.dom.element;import Org.w3c.dom.Document;import Org.w3c.dom.nodelist;import Org.xml.sax.saxexception;public class Test {/** * @param args */public static void main (Strin G[] args) {//unzip book1.xlsxzipfile xlsxfile;try {xlsxfile = new ZipFile (New File ("C:\\1.xlsx"));D Ocumentbuilderfactory DBF = Documentbuilderfactory.newinstance ();//First read sharedstrings.xml This file standby zipentry sharedstringxml = Xlsxfile.getentry ("Xl/sharedstrings.xml"); InputStream Sharedstringxmlis = Xlsxfile.getinputstream (sharedstringxml);D ocument sharedstring;sharedstring = Dbf.newdocumentbuilder (). Parse (Sharedstringxmlis); NodeList str = sharedstring.getelementsbytagname ("T"); String sharedstrings[] = new String[str.getlength ()];for (int n = 0; n < Str.getlength (); n++) {Element element = (Element) str.item (n); Sharedstrings[n] = Element.gettextcontent ();} Locate the Workbook.xml in the Unpacked folder, this file contains several sheetzipentry workbookxml = Xlsxfile.getentry ("Xl/workbook.xml") in this worksheet; Nputstream Workbookxmlis = Xlsxfile.getinputstream (workbookxml);D ocument doc = Dbf.newdocumentbuilder (). Parse ( Workbookxmlis);//Get a total of several sheetnodelist NL = doc.getelementsbytagname ("sheet"); for (int i = 0; i < nl.getlength (); i++) {element element = (Element) nl.item (i);//Convert node to Element to get the properties of each node System.out.println (Element.getattribute ("name "));//Output The value of the Name property of the sheet node//And then go to the Decompression folder to find the corresponding name value of the XML file, such as in Workbook.xml <sheet name=" Sheet1 "//sheetid=" 1 "r:id = "RId1"/> Node//Then you can find Sheet1.xml in the decompression folder Xl/worksheets, which is the contents of the table contained in the XML folder ZipEntry Sheetxml = Xlsxfile.getentry ( "xl/worksheets/" + element.getattribute ("name"). toLowerCase () + ". xml"); InputStream Sheetxmlis = Xlsxfile.getinputstream (sheetxml);D ocument sheetdoc = Dbf.newdocumentbuilder (). Parse (Sheetxmlis); NodeList RowdATA = Sheetdoc.getelementsbytagname ("row"); for (int j = 0; J < Rowdata.getlength (); j + +) {///Row format:/* * * * * <row R= "1" spans= "1:3" >r represents the first row, spans indicates a few columns <c r= "A1" * t= "S" >//r represents the list of the column *, t= "s" Personally think that the contents of this cell can be found in the Sharedstrings.xml file, the corresponding node * subscript is the value of the V node, that is, 0, if there is no t attribute, then the value of V is the contents of the cell <v>0</v> </c > <c r= "B1" * t= "S" > <v>1</v> </c> <c r= "C1" t= "S" > <v>2</v> </c> </ row> */element row = (Element) Rowdata.item (j);//row gets the column in each row nodelist Columndata = Row.getelementsbytagname ("C"); for (int k = 0; k < columndata.getlength (); k++) {element column = (element) Columndata.item (k); NodeList values = Column.getelementsbytagname ("V"); Element value = (element) Values.item (0), if (Column.getattribute ("t")! = null& Column.getattribute ("T"). Equals ("s" ) {//If it is a shared string, find the value of the column in Sharedstring.xml System.out.print (Sharedstrings[integer.parseint (Value.gettextcontent ())] + " ");} else {if (value! = null) {System.out.print (value.gettextContent () + "");} else {System.out.println ("J:" + j + "K:" + K + "null");}}} System.out.println ();}}} catch (Zipexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-genera Ted Catch Blocke.printstacktrace ();} catch (Saxexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Parserconfigurationexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();}}}
Java Read Excel xlsx