Javaweb JSP page upload Excel file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.