Servlet:
Private static final Long file_max_size = 4 * 1024 * 1024;
if (! Servletfileupload.ismultipartcontent (Request)) {
SYSTEM.OUT.PRINTLN ("Failure 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<fileitem> 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 ("Failure 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 (); Yes
int Cols=rs.getcolumns (); Column--Getcell (column, row)
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:
<script type= "Text/javascript" >
function Checkexcel () {
var mime = document.getElementById (' Excelfile '). Value;
MIME = Mime.tolowercase (). substr (Mime.lastindexof ("."));
if (! ( MIME = = ". xls")) {
Alert ("Please import the correct Excel file, only the XLS format is supported!");
return false;
}
}
</script>
<body>
<form action= "Servlet/exceltest" enctype= "Multipart/form-data" method= "POST" onsubmit= "return CheckExcel ()" >
<input type= "File" Name= "Excelfile" onchange= "Checkexcel (This)" id= "Excelfile"/>
<input type= "Submit" value= "read Excel"/>
</form>
</body>
Javaweb JSP page upload Excel file