Package com.sun.test;
Import java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.io.InputStream;
Import java.util.Date;
Import java.util.HashMap;
Import java.util.Map;
Import org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import org.apache.poi.ss.usermodel.Cell;
Import org.apache.poi.ss.usermodel.DateUtil;
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;
Import org.slf4j.Logger;
Import org.slf4j.LoggerFactory;
/**
* Read Excel
*
* @author Zengwendong
*/
public class Readexcelutils {
Private Logger Logger = Loggerfactory.getlogger (readexcelutils.class);
Private Workbook wb;
Private Sheet Sheet;
Private Row row;
Public Readexcelutils (String Filepath) {
If (filepath==null) {
Return
}
String ext = filepath.substring (filepath.lastindexof ("."));
try {
InputStream is = new FileInputStream (filepath);
If (". xls". equals (ext)) {
WB = new Hssfworkbook (is);
}else if (". xlsx". equals (ext)) {
WB = new Xssfworkbook (is);
}else{
wb=null;
}
} catch (filenotfoundexception E) {
Logger.error ("filenotfoundexception", e);
} catch (ioexception E) {
Logger.error ("ioexception", e);
}
}
/**
* Read the contents of the Excel table header
*
* @param inputstream
* @return Array of String header contents
* @author Zengwendong
*/
Public string[] Readexceltitle () throws exception{
If (wb==null) {
Throw new Exception ("workbook object is empty! ");
}
Sheet = Wb.getsheetat (0);
row = Sheet.getrow (0);
Total number of header columns
int colnum = Row.getphysicalnumberofcells ();
System.out.println ("colnum:" + colnum);
string[] title = new string[colnum];
for (int i = 0; i < colnum; i++) {
title[i] = Getstringcellvalue (row.getcell ((short) i));
title[i] = Row.getcell (i). getcellformula ();
}
Return title;
}
/**
* Read the contents of the Excel data
*
* @param inputstream
* @return Map object containing the contents of the cell data
* @author Zengwendong
* /
public map<integer, map<integer,object>> readexcelcontent () throws exception{
if (wb==null) {
Throw new Exception ("workbook object is empty! ");
}
map<integer, map<integer,object>> content = new hashmap<integer, map<integer,object> > ();
Sheet = wb.getsheetat (0);
Get total number of rows
int rowNum = Sheet.getlastrownum ();
row = Sheet.getrow (0);
int colnum = Row.getphysicalnumberofcells (); The
//body content should start with the second line, the title of the first behavior header
for (int i = 1; i <= rowNum; i++) {
row = Sheet.getrow (i);
Int J = 0;
map<integer,object> cellvalue = new hashmap<integer, object> ();
While (j < Colnum) {
Object obj = getcellformatvalue (row.getcell (j));
Cellvalue.put (j, obj);
J + +;
}
Content.put (i, cellvalue);
}
Return content;
}
/**
*
* Set data based on cell type
*
* @param cell
* @return
* @author zengwendong
*/
Private Object Getcellformatvalue (cell cell) {
Object cellvalue = "";
If (cell! = Null) {
//determine The current Cell's TYPE
Switch (cell.getcelltype ()) {
case cell.cell_type_numeric:// If the TYPE of the current cell is numeric
case Cell.cell_type_formula: {
//determines Whether the current cell is a date
if (dateutil.iscelldateformatted (cell)) {
//if the date type, the conversion to data format
//data format is time and seconds: 2013-7-10 0:00:00
//cellvalue = Cell.getdatecellvalue (). toLocaleString (); The
//data format is no time and seconds: 2013-7-10
Date date = Cell.getdatecellvalue ();
Cellvalue = date;
} Else {//if It is a pure number
//get the value of the current cell
Cellvalue = string.valueof (cell.getnumericcellvalue ());
}
break;
}
Case cell.cell_type_string://if The TYPE of the current cell is string
//gets the current cell string
Cellvalue = Cell.getrichstringcellvalue (). getString ();
break;
Default://default cell Value
Cellvalue = "";
}
} else {
cellvalue = "";
}
Return cellvalue;
}
public static void main (string[] Args) {
try {
String filepath = "c:\\users\\xsp034\\desktop\\ce.xls";
Readexcelutils excelreader = new Readexcelutils (filepath);
Test for reading Excel table headers
/*string[] title = Excelreader.readexceltitle ();
SYSTEM.OUT.PRINTLN ("get the Excel table title:");
For (String S:title) {
System.out.print (s + "");
}*/
Test for reading Excel table content
map<integer, map<integer,object>> Map = excelreader.readexcelcontent ();
System.out.println ("get The contents of the Excel table:");
for (int i = 1; i <= map.size (); i++) {
System.out.println (map.get (i));
}
} catch (filenotfoundexception E) {
SYSTEM.OUT.PRINTLN ("file specified path not found!");
E.printstacktrace ();
}catch (Exception E) {
E.printstacktrace ();
}
}
}
Java read Excel (map structure) xls