servlet:
private static final long FILE_MAX_SIZE = 4 * 1024 * 1024;
if (!ServletFileUpload.isMultipartContent(request)) {
System.out.println("失败1");
}
String fileTempPath = this.getServletContext().getRealPath("/") + "filetemp";
File tempDir = new File(fileTempPath);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
FileItemFactory factory = new DiskFileItemFactory(4096, tempDir);
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setFileSizeMax(FILE_MAX_SIZE);
List fileItems = null;
try {
fileItems = sfu.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
if (e instanceof SizeLimitExceededException) {
}
return;
}
if (fileItems == null || fileItems.size() == 0) {
System.out.println("失败2" );
return;
}
Workbook rwb;
try {
rwb = Workbook.getWorkbook(fileItems.get(0).getInputStream());
int sheetCount=rwb.getNumberOfSheets();
Sheet rs=rwb.getSheet(0);
int rows=rs.getRows(); //行
int cols=rs.getColumns(); //列 -- getCell(列,行)
System.out.println(rows+"-*-*-*-"+cols);
for (int i = 2; i < rows ; i++) {
Cell cell = rs.getCell(i, 0);
System.out.print(rs.getCell(0, i).getContents()+"-*-");
System.out.print(rs.getCell(1, i).getContents()+"-*-");
System.out.print(rs.getCell(2, i).getContents()+"-*-");
System.out.print(rs.getCell(3, i).getContents()+"-*-");
System.out.print(rs.getCell(4, i).getContents()+"-*-");
System.out.print(rs.getCell(5, i).getContents()+"-*-");
System.out.print(rs.getCell(6, i).getContents()+"-*-");
System.out.print(rs.getCell(7, i).getContents()+"-*-");
System.out.print(rs.getCell(8, i).getContents()+"-*-");
System.out.print(rs.getCell(9, i).getContents()+"-*-");
System.out.println();
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
fileItems.get(0).getInputStream().close();
}
jsp:
function CheckExcel() {
var mime = document.getElementById(‘Excelfile‘).value;
mime = mime.toLowerCase().substr(mime.lastIndexOf("."));
if (!(mime == ".xls")) {
alert("请导入正确的EXCEL文件,仅支持xls格式!");
return false;
}
}
原文:http://www.cnblogs.com/mingjian/p/5226093.html