Read Excel data with Java Custom jar package support excel2007 and excel2003
Download the required packages in http://download.csdn.net/detail/u010792467/8079355
If you need excel2003 and excel2007 files you can go to
http://download.csdn.net/detail/u010792467/8072009 Download
Download Project in http://download.csdn.net/detail/u010792467/8079345
Example of custom jar package application
<span style= "FONT-SIZE:18PX;" >package com.readexcel;import Java.util.list;import Excel.importexecltool;public class Exceltest {public static void Main (string[] args) {//string FilePath = "d:\\excel\\excel2007 test. xlsx"; String filepath= "d:\\excel\\excel2003 test. xls"; Importexecltool impexcel = new Importexecltool ();//All Sheet page Contents//List <List<String>> list= Impexcel.readexcel (FilePath);//FilePath Excel file Path//1 represents the first sheet page 2 for the first sheet page ... list<list<string>> list = Impexcel.readexcel (FilePath, 1), if (List! = null) {for (int i = 0; i < list.size ( ); i++) {System.out.print ("+ (i) +" line "); list<string> Listcell = List.get (i); for (String S:listcell) {System.out.print ("" + s);} System.out.println ();}} System.out.println ("Run Complete");}} </span>
Custom jar Package Source code
<span style= "FONT-SIZE:18PX;" >package excel;import java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.io.inputstream;import Java.util.arraylist;import Java.util.list;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.Sheet ; Import Org.apache.poi.ss.usermodel.workbook;import Org.apache.poi.xssf.usermodel.xssfworkbook;public class Importexecltool {private String errorinfo;private static int readsheet = 0;private static Boolean readsheetnum = False;pri Vate static Boolean isgtsheetnum = False;public boolean validateexcel (String filePath) {if (FilePath = = null| |! Is2003excel (FilePath) | | Is2007excel (FilePath)) {errorinfo = "file name is not in Excel format"; return false;} File File = new file (FilePath), if (file = = NULL | |!file.exists ()) {errorinfo = "Excel file does not exist"; return false;} return true;} Public List<list<striNg>> Readexcel (String filePath) {list<list<string>> dataList = new arraylist<list<string> > (); InputStream is = null;try {if (!validateexcel (FilePath)) {System.out.println (); list<string> list3 = new arraylist<string> () List3.add (errorinfo);d atalist.add (LIST3); return dataList;} Boolean is2003excel = True;if (Is2007excel (FilePath)) {is2003excel = false;} File File = new file (FilePath), is = new FileInputStream (file);d atalist = ReadFile (is, is2003excel); Is.close ();} catch (Exception ex) {ex.printstacktrace ();} finally {if (is! = null) {try {is.close ();} catch (IOException e) {is = null; E.printstacktrace ();}}} return dataList;} Public list<list<string>> readFile (InputStream inputstream,boolean is2003excel) {list<list<string >> datalists = null;try {Workbook wb = null;if (is2003excel) {wb = new Hssfworkbook (inputstream);} else {wb = new XS Sfworkbook (InputStream);} int sheetnum = sheetcirculation (WB); if (Readsheet >= sheetnum) {List<String> list2 = new arraylist<string> () List2.add ("Number of input pages is greater than actual sheet pages!!!"); List list4 = new ArrayList (); List4.add (list2); this.isgtsheetnum = True;return list4;} list<list<string>> dataList = new arraylist<list<string>> (); if (readsheetnum) {dataLists = Read (DataList, WB, Readsheet);} else {for (int i = 0; i < Sheetnum; i++) {datalists = read (dataList, WB, I);}}} catch (IOException e) {e.printstacktrace ();} return datalists;} Private list<list<string>> Read (list<list<string>> dataList, Workbook wb,int sheets) {int Totalrows = 0;int Totalcells = 0; Sheet Sheet = Wb.getsheetat (sheets); totalrows = Sheet.getphysicalnumberofrows (); if (totalrows >= 1 && Sheet.getrow (0) = null) {totalcells = Sheet.getrow (0). Getphysicalnumberofcells (); for (int r = 0; r < totalrows; r++) {Row row = Sheet.getrow (r); if (Row = = null) {continue;} list<string> rowdatalist = new arraylist<string> (); for (int c = 0; c < totalcells; C + +) {Cell cell = Row.getcell (c); String cellvalue = ""; if (null! = cell) {switch (Cell.getcelltype ()) {case Hssfcell.cell_type_numeric://Digital Cellvalue = CE Ll.getnumericcellvalue () + ""; Break;case hssfcell.cell_type_string://String cellvalue = Cell.getstringcellvalue (); break Case Hssfcell.cell_type_boolean://Booleancellvalue = Cell.getbooleancellvalue () + ""; Break;case HSSFCell.CELL_TYPE_ FORMULA://Formula Cellvalue = Cell.getcellformula () + ""; Break;case Hssfcell.cell_type_blank://null value Cellvalue = ""; Break;case H Ssfcell.cell_type_error://fault cellvalue = "illegal character"; break;default:cellvalue = "unknown type"; break;}} Rowdatalist.add (Cellvalue);} Datalist.add (rowdatalist);} return dataList;} private int sheetcirculation (Workbook wb) {int sheetcount = -1;sheetcount = Wb.getnumberofsheets (); return sheetcount;} public static Boolean Is2003excel (String filePath) {return filepath.matches ("^.+\\. i) (XLS) $ ");} public static Boolean Is2007excel (String filePath) {return filepath.matches ("^.+\\. i) (xlsx) $ ");} Public ImportexeCltool () {}public String GetErrorInfo () {return errorinfo;} Public list<list<string>> Readexcel (String filePath, int num) {this.readsheet = Num-1;this.readsheetnum = tr Ue;if (num <= 0) {List list0 = new ArrayList (); List list5 = new ArrayList (); List0.add ("Excel must be greater than 0 pages!!!"); List5.add (list0); return list5;} list<list<string>> List = Readexcel (FilePath), if (!this.isgtsheetnum && list.size () = = 0) {list< String> List1 = new arraylist<string> () List1.add ("The data in the sheet page is empty!!!"); List.add (List1);} return list;}} </span>
Lost Jian file
Reprint Please specify link: http://blog.csdn.net/awenluck/article/details/40442207
Java Custom jar Package reads Excel (contains 2003 and 2007) data and illustrates