Java reads Excel 2003 (XLS format) using poi.3.10

Source: Internet
Author: User
Tags log log

Recently in an Excel import database case, collate documents for your reference.

1. Download the latest POI

Http://poi.apache.org/download.html 2, unzip the relevant JAR package introduction Project 3, Case source
ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.text.DecimalFormat;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;Importorg.junit.Test; Public classTestexcel {//record the output information of a class    StaticLog log = Logfactory.getlog (testexcel.class); //get the path to an Excel document     Public StaticString FilePath = Testexcel.class. GetResource ("/test.xls"). GetPath (); @Test Public Static voidMain (string[] args) {Try {            //Create a reference to an Excel workbook fileHssfworkbook Wookbook =NewHssfworkbook (NewFileInputStream (FilePath)); //In an Excel document, the default index for worksheet one is 0//Its statement is: hssfsheet sheet = workbook.getsheetat (0);Hssfsheet sheet = wookbook.getsheet ("Sheet1"); //gets the number of rows in the Excel file            introws =sheet.getphysicalnumberofrows (); //traversing rows, the index starts at 0, and line No. 0 can be used as a table header, not fetched.              for(inti = 1; i < rows; i++) {                //read upper left cellHssfrow row =Sheet.getrow (i); //line is not empty                if(Row! =NULL) {                    //get all the columns in the Excel file//int cells = Row.getphysicalnumberofcells ();String value = ""; //traversing Columns//prevent cell.getnumbericcellvalue () when using POI to process Excel//when the length is larger, it becomes the scientific notation form. DecimalFormat DF =NewDecimalFormat ("0"); //* * * Below 3 is the value of cells, replace with fixed number of columns, solve the problem of NULL value * * *                     for(intj = 0; J < 5; J + +) {                        //gets the value to the columnHssfcell cell =Row.getcell (j); if(Cell! =NULL) {                            Switch(Cell.getcelltype ()) { CaseHssfcell.cell_type_formula: Break;  CaseHSSFCell.CELL_TYPE_NUMERIC:value+=Df.format (Cell.getnumericcellvalue ())+ ",";  Break;  CaseHSSFCell.CELL_TYPE_STRING:value+ = Cell.getstringcellvalue () + ",";  Break; default: Value+= "#"+",";  Break; }                        } Else{Value+= "#" + ","; }                    }                    //inserting data into the MySQL databaseString[] val = Value.split (","); System.out.println (val[0]); System.out.println (val[1]); System.out.println (val[2]); System.out.println (val[3]); System.out.println (val[4]); System.out.println (" "); //******* here to save the data to the database ******                }            }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

This method can only read the Excel2003 version, read version 2010 I am learning.

Java reads Excel 2003 (XLS format) using poi.3.10

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.