JXL Importing Excel

Source: Internet
Author: User

JSP section

<div class= "Modal-dialog" >
<div class= "Modal-content" >
<div class= "Modal-body" >
<i class= "Ace-icon fa fa-home Home-icon" ></i>
Excel Import Customer Data <br/>
<form action= "${ctx}/customer/importcustomer" method= "post" enctype= "Multipart/form-data" id= "imp" >

<div class= "Form-group";
<input type= "file" name= "import"/>
<input type= "hidden" name= " Table "value=" tablename "/>
</div>
<!--/.box-body-->
<br/>
<div class=" Form-group,
&nbsp;&nbsp;<a href= "#" > Import template Download </a>        //HREF for server absolute address     Pre-deposited file path   Direct access to
</div>
<!--/.box-footer
</form>
<br/>

<!--data-dismiss= "modal" affects Ajax submissions-->
<div class= " Modal-footer "
<button class=" btn btn-sm btn-success "data-dismiss=" modal "
id=" import "><i
class= "Ace-icon fa fa-check" ></i> import
</button>&nbsp;&nbsp;
<button class= "btn btn-sm btn-warning"
onclick= "Toclosecustomergroup ();"
<i class= "Ace-icon fa fa-times" ></i> cancel
</button>
</div>
</div>
</div>

$ (document). Ready (function () {
$ (' #import '). Click (function () {
var formData = new FormData ($ ("#imp") [0]);
var file = $ ("Input[name= ' import ']"). Val ();
var suffix = file.split (".") [1];                        //intercept by decimal point   The second string   that is, the suffix
if (suffix = = null) {
Layer.msg ("Please select the uploaded excl file! ");
return;
}else if (suffix!= "xls" && suffix!= "xlsx") {
Layer.msg ("The format is not correct, please select the uploaded excl file!") ");
return;
}
$.ajax ({
URL: "${ctx}/customer/importcustomer",
Type: ' Post ',
Data:formdata,
Async:false,
Cache:false,
Contenttype:false,
Processdata:false,
Error:function () {
layer.msg (' Network error! ');
},
Success:function (data) {
if (data==1) {
Layer.msg ("Import succeeded");
Parent.location.reload ();
}else{
layer.msg (data);
}
}
});
});
});

--------------------------------------------------------------------------------------------------------------- ---------------------------

Controller part upload and then read upload file can not save read uploaded file stream

Customer Import
@RequestMapping (value = "/importcustomer", method = Requestmethod.post)
@ResponseBody
Public String Importcustomer (
HttpServletRequest request, Model model,httpsession session) {
Get Store ID
Object objshopid = Request.getsession (). getattribute ("Shopid");
Integer shopid = (integer) objshopid;
String resultname = "";
String newfilename = "";
String str= "";
Commonsmultipartfile Cf=null; is important
File stream
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Iterator item = Multipartrequest.getfilenames ();
while (Item.hasnext ()) {
String fileName = (string) item.next ();
multipartfile file = Multipartrequest.getfile (fileName);
CF = (commonsmultipartfile) file;

To intercept filenames without extensions
FileName = File.getoriginalfilename (). substring (0,
File.getoriginalfilename (). LastIndexOf ("."));
Check extension
String Fileext = File.getoriginalfilename ()
. substring (File.getoriginalfilename (). LastIndexOf (".") + 1)
. toLowerCase ();
SimpleDateFormat df = new SimpleDateFormat ("Yyyymmddhhmmss");
New file name is original + date + random number
NewFileName = Df.format (New Date ()) + "_"
+ New Random (). Nextint (+) + "." + Fileext;
Resultname = resultname + NewFileName + ";";

}
Reading Excel files
Workbook Workbook = null;
list<customer> list = new arraylist<customer> ();
try {
InputStream is = Cf.getinputstream (); Get file Stream
Get a workbook and the first work order
Workbook = Workbook.getworkbook (IS);
Sheet Sheet = workbook.getsheet (0);
SimpleDateFormat format2 = new SimpleDateFormat ("Yyyy-mm-dd");
int row = Sheet.getrows ();
Define the start line
int rowstart = 1;
Loop line, beginning of the next line of the header row
for (int i = Rowstart; i < row; i++) {
Customer customer = new Customer ();
Cell[] cells = Sheet.getrow (i);
Customer.setcustomername (Cells[0].getcontents ());
Customer.settel (Cells[1].getcontents ());
String birthday=cells[2].getcontents ();
Date time =format2.parse (birthday);
Customer.setbirthday (time);

Group name
String groupname=cells[3].getcontents ();
Customergroup Customergroup =customerservice.findgroupbyshopidandname (Shopid, GroupName);
if (customergroup!=null) {
Customer.setgroupid (Customergroup.getid ());
}else{
Customergroup cugp=new Customergroup ();
Cugp.setgroupname (groupname);
Cugp.setshopid (SHOPID);
Customerservice.insertgroupbyname (CUGP);
Customer.setgroupid (Cugp.getid ());
}
Customer.setsex (2);
Customer.setshopid (SHOPID);
List.add (customer);
}
for (Customer customeror:list) {
Customer Ccc=customerservice.findcbyshopidandtel (Customeror.getshopid (), Customeror.gettel ());
if (ccc==null) {
Customerservice.insertcustomer (Customeror);
}
}

} catch (Exception e) {
str= "Data format Error! ";
return str;
}finally{
Workbook.close ();
}
Str= "1";
return str;
}

JXL Importing Excel

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.