所用jar :
cos.jar、
poi-3.12-20150511.jar、
poi-examples-3.12-20150511.jar、
poi-excelant-3.12-20150511.jar、
poi-ooxml-3.12-20150511.jar、
poi-ooxml-schemas-3.12-20150511.jar、
poi-scratchpad-3.12-20150511.jar、
xmlbeans-2.5.0.jar
_____________________________________________________
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream" %>
<%@page import="java.io.InputStream" %>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.ss.usermodel.Cell"%>
<%@page import="org.apache.poi.ss.usermodel.Row"%>
<%@page import="org.apache.poi.ss.usermodel.Sheet"%>
<%@page import="org.apache.poi.ss.usermodel.DateUtil"%>
<%@page import="org.apache.poi.hssf.usermodel.*" %>
<%@page import="org.apache.poi.xssf.usermodel.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%
MultipartRequest mr=null;
//用来限制用户上传文件大小的
int maxPostSize = 500 * 1024 * 1024;
String pathUp=getServletContext().getRealPath("/")+"userList";
//System.out.println("pathUp============"+pathUp);
//第一个参数为传过来的请求HttpServletRequest,
//第二个参数为上传文件要存储在服务器端的目录名称
//第三个参数是用来限制用户上传文件大小
//第四个参数可以设定用何种编码方式来上传文件名称,可以解决中文问题
mr = new MultipartRequest(request, pathUp, maxPostSize, "UTF-8");
//传回所有文件输入类型的名称
Enumeration files = mr.getFileNames();
String fileName = "";
String filePath="";
while (files.hasMoreElements()) {
fileName = (String) files.nextElement();
//System.out.println("FileName============"+fileName);
//用此方法得到上传文件的真正的文件名,这里的fileName指文件输入类型的名称
filePath = mr.getFilesystemName(fileName);
System.out.println("FilePath============"+filePath);
//此方法得到一个文件对象,代表储存在服务器上的fileName文件
File f = mr.getFile(fileName);
if (null != f) {
java.util.Date dt = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
int n = filePath.indexOf(".");
String newName = pathUp+"\\"+fmt.format(dt)+"-userList"+filePath.substring(n);
//System.out.println("newName============"+newName);
File newF = new File(newName);
f.renameTo(newF);
//读数据文件存为json,回传
String errors = "";
ArrayList excelA = new ArrayList();
StringBuffer jsonS = new StringBuffer();
try {
if(filePath.substring(n).equals(".xls")){
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(newF));
HSSFSheet sheet = wb.getSheetAt(0);
int rowCount = sheet.getLastRowNum();
if(rowCount>4){
//遍历每一行 rowCount行号从0开始
for (int r = 0; r < rowCount+1; r++) {
boolean va = false;
ArrayList temp = new ArrayList();
HSSFRow row = sheet.getRow(r);
int cellCount = row.getLastCellNum(); //获取总列数
//遍历每一列
for (int c = 0; c < cellCount; c++) {
HSSFCell cell = row.getCell((short)c);
int cellType = cell.getCellType();
String cellValue = "";
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
/*if(HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(date);//日期型
}
else {*/
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
// }
break;
default:
cellValue = "";
}
if(cellValue.length()>0){
va = true;
}
temp.add(cellValue);
}
if (va) {
excelA.add(temp);
}
}
}
}
else{
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(newName));
XSSFSheet sheet = wb.getSheetAt(0);
int rowCount = sheet.getLastRowNum();
if(rowCount>4){
//遍历每一行
for (int r = 5; r < rowCount-1; r++) {
boolean va = false;
ArrayList temp = new ArrayList();
XSSFRow row = sheet.getRow(r);
int cellCount = row.getLastCellNum(); //获取总列数
//遍历每一列
for (int c = 0; c < cellCount; c++) {
XSSFCell cell = row.getCell((short)c);
int cellType = cell.getCellType();
String cellValue = "";
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
break;
default:
cellValue = "";
}
if(cellValue.length()>0){
va = true;
}
temp.add(cellValue);
}
if (va) {
excelA.add(temp);
}
}
}
}
}catch(Exception e){
System.out.println(e);
}
}
}
%>