標籤:截取 檢查 home data index 錯誤 ssd 擴充 第一個
jsp部分
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<h3>
<i class="ace-icon fa fa-home home-icon"></i>
Excel匯入客戶資料 </h3>
<hr/>
<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">
<a href="#">匯入模板下載</a> //href為伺服器絕對位址 事前存放好的檔案路徑 直接擷取
</div>
<!-- /.box-footer -->
</form>
<br/>
<!-- data-dismiss="modal" 影響ajax提交 -->
<div class="modal-footer">
<button class="btn btn-sm btn-success" data-dismiss="modal"
id="import"><i
class="ace-icon fa fa-check"></i> 匯入
</button>
<button class="btn btn-sm btn-warning"
onclick="toclosecustomergroup();">
<i class="ace-icon fa fa-times"></i> 取消
</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]; //根據小數點截取 第二個字串 也就是尾碼名
if(suffix == null){
layer.msg("請選擇上傳的Excl檔案!");
return;
}else if(suffix!="xls" && suffix!="xlsx" ){
layer.msg("格式不正確,請選擇上傳的Excl檔案!");
return;
}
$.ajax({
url: "${ctx}/customer/importcustomer",
type: ‘post‘,
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
error : function() {
layer.msg(‘網路錯誤!‘);
},
success: function(data) {
if(data==1){
layer.msg("匯入成功");
parent.location.reload();
}else{
layer.msg(data);
}
}
});
});
});
------------------------------------------------------------------------------------------------------------------------------------------
controller部分 先上傳再讀取 上傳檔案可以不儲存 讀取上傳的檔案流
//客戶匯入
@RequestMapping(value = "/importcustomer", method = RequestMethod.POST)
@ResponseBody
public String importcustomer(
HttpServletRequest request, Model model,HttpSession session) {
//擷取店id
Object objshopid = request.getSession().getAttribute("shopid");
Integer shopid = (Integer) objshopid;
String resultName = "";
String newFileName = "";
String str="";
CommonsMultipartFile cf=null; //很重要
// 檔案流
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator item = multipartRequest.getFileNames();
while (item.hasNext()) {
String fileName = (String) item.next();
MultipartFile file = multipartRequest.getFile(fileName);
cf = (CommonsMultipartFile) file;
//截取不帶副檔名的檔案名稱
fileName = file.getOriginalFilename().substring(0,
file.getOriginalFilename().lastIndexOf("."));
// 檢查副檔名
String fileExt = file.getOriginalFilename()
.substring(file.getOriginalFilename().lastIndexOf(".") + 1)
.toLowerCase();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
// 新檔案名稱為原名+日期+隨機數
newFileName = df.format(new Date()) + "_"
+ new Random().nextInt(1000) + "." + fileExt;
resultName = resultName + newFileName + ";";
}
//讀取Excel檔案
Workbook workbook = null;
List<Customer> list = new ArrayList<Customer>();
try {
InputStream is = cf.getInputStream(); //擷取檔案流
//擷取工作薄和第一個工作單
workbook = Workbook.getWorkbook(is);
Sheet sheet = workbook.getSheet(0);
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
int row = sheet.getRows();
//定義開始的一行
int rowStart = 1;
//迴圈行,標題列的下一行開始
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);
//分組名稱
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="資料格式錯誤!";
return str;
}finally{
workbook.close();
}
str="1";
return str;
}
jxl 匯入excel