poi读取excel2010
package com.icss.test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ImportExcelToDatabase {
public static void main(String[] args) {
//String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;
String excel2010 = "C:\\Users\\gls\\Desktop\\移动手机号码测试格式.xlsx";
List<NodeAddress> list = readExcel(excel2010);
if(list!=null){
for (NodeAddress n : list) {
System.out.println("time="+n.getTime()+"地区="+n.getArea()+"县市="+n.getCounty()+"区域="+n.getDistrict()
+"乡镇="+n.getVillage()+"电话号码="+n.getTelephone());
}
}
}
private static List<NodeAddress> readExcel(String path){
List<NodeAddress> list = new ArrayList<NodeAddress>();
try {
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
NodeAddress nodeAddress = null;
//读sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if(xssfSheet==null){
continue;
}
//读行
for(int rowNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if(xssfRow!=null){
nodeAddress = new NodeAddress();
XSSFCell time = xssfRow.getCell(0);
XSSFCell area = xssfRow.getCell(1);
XSSFCell county = xssfRow.getCell(2);
XSSFCell district = xssfRow.getCell(3);
XSSFCell village = xssfRow.getCell(4);
XSSFCell telehone = xssfRow.getCell(5);
nodeAddress.setTime(getValue(time));
nodeAddress.setArea(getValue(area));
nodeAddress.setCounty(getValue(county));
nodeAddress.setDistrict(getValue(district));
nodeAddress.setVillage(getValue(village));
nodeAddress.setTelephone(getValue(telehone));
list.add(nodeAddress);
}
}
return list;
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static String getValue(XSSFCell xssfRow){
if(xssfRow.getCellType()==xssfRow.CELL_TYPE_BOOLEAN){
return String.valueOf(xssfRow.getBooleanCellValue());
}else if(xssfRow.getCellType()==xssfRow.CELL_TYPE_NUMERIC){
return String.valueOf(xssfRow.getNumericCellValue());
}else{
return String.valueOf(xssfRow.getStringCellValue());
}
}
}