Effect:
code
package com.wangyun.util;
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;
/**
* @ClassName: excel导入工具类
* @Description:
* @author: wangyun
* @date: 2018-3-20 上午10:27:52
*/
public class ImportExcelUtil {
private Workbook wb;
private Sheet sheet;
private Row row;
public ImportExcelUtil(String filepath) { if(filepath==null){ return; } String ext = filepath.substring(filepath.lastIndexOf("."));//获取文件名后缀 try { InputStream is = new FileInputStream(filepath); if(".xls".equals(ext)){//2003- 版本的excel wb = new HSSFWorkbook(is); }else if(".xlsx".equals(ext)){//2007+ 版本的excel wb = new XSSFWorkbook(is); }else{ wb=null; } } catch ( FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取excel表头的内容
* @return
* @throws Exception
*/
public String[] readExcelTitle() throws Exception{
if(wb==null){
throw new Exception("Workbook对象为空!");
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0); // 标题总列数 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));
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
title[i] = row.getCell(i).getStringCellValue();
}
return title;
}
/**
* 读取excel内容
* @return
* @throws Exception
*/
public Map<Integer, Map<String,Object>> readExcelContent() throws Exception{
if(wb==null){
throw new Exception("Workbook对象为空!");
}
Map<Integer, Map<String,Object>> content = new HashMap<Integer, Map<String,Object>>();
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// The body content should start from the second row, the first row is the title of the header
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
Map<String,Object> cellValue = new HashMap<String, Object>();
while (j < colNum) {
Object obj = getCellFormatValue(row.getCell(j));
Object objStr=getCellFormatValue(sheet.getRow(0).getCell(j));
//Get the title of the first column
cellValue.put((String) objStr, obj);
j++;
}
content.put(i, cellValue);
}
return content; } /** * Set data parameters according to cell type * @param cell
* @return
*/
private Object getCellFormatValue(Cell cell) {
Object cellvalue = "";
if (cell != null) {
// Determine the Type
switch of the current Cell (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// If the Type of the current Cell is NUMERIC
case Cell.CELL_TYPE_FORMULA: {
// Determine whether the current cell is a Date
if (DateUtil.isCellDateFormatted(cell)) {
// If it is a Date type, convert it to Data format
// The data format is with Hours, minutes and seconds: 2013-7-10 0:00:00
// cellvalue = cell.getDateCellValue().toLocaleString();
// data format is without hours, minutes 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
// Get the current Cell string
cellvalue = cell.getRichStringCellValue().getString() ;
break;
default:// default Cell value
cellvalue = "";
}
} else {
cellvalue = "";
}
return cellvalue; } public static void main(String[] args) { String filepath = "C:\\Users \\wangyun\\Desktop\\County and city data 2014.xls"; ImportExcelUtil eu=new ImportExcelUtil(filepath); String[] title; try { title = eu.readExcelTitle(); /*
System.out.println("Get the title of the Excel table:"); for (String s : title) { System.out.print(s + " "); }*/ // Test for reading the contents of the Excel table Map< Integer, Map<String,Object>> map = eu.readExcelContent(); System.out.println("Get the content of the Excel table:"); for (int i = 0; i <= map.size(); i++ ) { System.out.println(map.get(i)); } } catch (FileNotFoundException e) { System.out.println("The file at the specified path was not found!"); e.printStackTrace(); }catch ( Exception e) { e.printStackTrace(); } } }