由于时间问题,我只来做一个多行一列,最简单最直观代码最简洁的java的excel导入
前台如下:
注意需要引入jquery.form.js
#ff0000;">
"modalfadehide"id="myCouponset"tabindex="-1"role="dialog"
aria-labelledby="myModalLabel"aria-hidden="false">
"modal-dialog">
"modal-content">
"modal-header">
"button"class="close"data-dismiss="modal"aria-hidden="true">
×
"modal-title"id="myModalLabel">
会员优惠券发放
"modal-body">
"conponFileImport.do"id="couponponsetmember"method="post"enctype="multipart/form-data">
"hidden"name="couponid"id="hiddencouponid"/>
请导入Excel
"file"name="couponFile"id="couponFile"/>
"modal-footer">
"button"id="couponuploadFile"class="btn pull-left btn-black"style="margin-left:300px;cursor:pointer;">确定
"button"class="btn btn-default"data-dismiss="modal">关闭
/.modal-content -->
-->
/.modal -->
"text/javascript">
$(function() {
$("#couponuploadFile").click(function() {
varcouponFile = $("#couponFile").val();
if(couponFile ==""|| couponFile =='undefined') {
alert("请选择文件!");
returnfalse;
}else{
if(!/\.(xls|XLS|xlsx|XLSX)$/.test(couponFile)) {
varstr = $("#couponFile").val()
varindex = str .lastIndexOf(".");
str= str.substring(index + 1, str .length);
alert("文件类型必须是.xls,.XLS,.xlsx,.XLSX,问卷类型."+str+"")
returnfalse;
}
}
$("#couponponsetmember").ajaxSubmit(options);
});
varoptions = {
type :"POST",
dataType :"text",
url :"conponFileImport.do",
async :false,
success :function(data) {
varstr = JSON.stringify(data);
varstr1 = JSON.parse(str);
varobj;
vart=0;
if(str1.indexOf("0||str1.indexOf("")>0){
t++;
}
if(t!=0){
alert("登录超时!");
document.location ='douponlist.html'
}elseif(obj.result =="success") {
}elseif(obj.result =="error") {
alert(obj.msg);
}else{
alert("导入失败!");
}
},
error :function(data) {
alert("导入失败,请参照模板输入信息!");
}
};
});
@RequestMapping(value ="/amn/conponFileImport.do", method = {
RequestMethod.GET, RequestMethod.POST})
@ResponseBody
publicObjectconponFileImport(HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value ="couponFile", required =false) MultipartFile couponFile,@RequestParam(value="couponid",required =false)Integer couponid){
Map map =newHashMap();
IntegercouponId= couponid;
String filename = couponFile.getOriginalFilename();
if(filename ==null||"".equals(filename)) {
map.put("msg","导入失败");
map.put("result","error");
returnmap;
}
InputStream input;
Workbook workbook;
Sheet sheet;
try{
input = couponFile.getInputStream();
workbook = WorkbookFactory.create(input);
sheet =workbook.getSheetAt(0);
if(sheet !=null) {
//先取最大列
Row rows = sheet.getRow(0);
intlienum= 0;
lienum =rows.getLastCellNum();
//循环每行
for(inti = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
//循环每列
for(intj = 0; j < rows.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if(j == 0) {//手机号
if(cell !=null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
String cellStrs =
cell.getStringCellValue();
System.out.println("cellStrs"+cellStrs);
}
}elseif(j == 1){
//如果有下一列的话,依次类推
}
}
}
}
}catch(IOException e) {
//TODOAuto-generated
catch block
e.printStackTrace();
}catch(InvalidFormatException es) {
es.printStackTrace();
}
returnmap;
}
控制台打印
有空我再写个代码简洁容易让人看懂的导出!
网友评论