Read data from excle in plain Java code (for. xlsx files)

Source: Internet
Author: User

Reference Links:

Program code:

 Packagedemo;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.util.zip.ZipEntry;Importjava.util.zip.ZipException;ImportJava.util.zip.ZipFile;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.w3c.dom.Element;Importorg.w3c.dom.Document;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {//Decompression Book1.xlsxZipFile Xlsxfile; Try{xlsxfile=NewZipFile (NewFile ("C:\\text1.xlsx")); Documentbuilderfactory DBF=documentbuilderfactory.newinstance (); //first read the Sharedstrings.xml this file backupZipEntry sharedstringxml = xlsxfile.getentry ("Xl/sharedstrings.xml"); InputStream Sharedstringxmlis=xlsxfile. getInputStream (Sharedstringxml);            Document sharedstring; Sharedstring=Dbf.newdocumentbuilder (). Parse (Sharedstringxmlis); NodeList Str= Sharedstring.getelementsbytagname ("T"); String sharedstrings[]=Newstring[str.getlength ()];  for(intn = 0; N < Str.getlength (); n++) {element element=(Element) Str.item (n); Sharedstrings[n]=element.gettextcontent (); }            //find the Workbook.xml in the Unzip folder, this file contains several sheet in this worksheetZipEntry workbookxml = xlsxfile.getentry ("Xl/workbook.xml"); InputStream Workbookxmlis=Xlsxfile.getinputstream (Workbookxml); Document Doc=Dbf.newdocumentbuilder (). Parse (Workbookxmlis); //get a total of a few sheetNodeList nl = doc.getelementsbytagname ("Sheet");  for(inti = 0; I < nl.getlength (); i++) {element element= (Element) nl.item (i);//Convert node to element to get the properties of each nodeSystem.out.println (Element.getattribute ("name"));//the value of the Name property of the output sheet node//Next, 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 the Sheet1.xml in the Xl/worksheets folder, 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); Document Sheetdoc=Dbf.newdocumentbuilder (). Parse (Sheetxmlis); NodeList RowData= Sheetdoc.getelementsbytagname ("Row");  for(intj = 0; J < Rowdata.getlength (); J + +) {                    //get each row//the format of the row:                    /** <row r= "1" spans= "1:3" >r represents the first row, spans indicates a few columns <c r= "A1" * t= "s" >//r table Show 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 not T property, 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); //get the columns in each row based on the rowNodeList columndata = Row.getelementsbytagname ("C");  for(intk = 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, look for 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-generated Catch blockE.printstacktrace (); } Catch(saxexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(parserconfigurationexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }}
View Code

Read data from excle in plain Java code (for. xlsx files)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.