jxl 匯入excel

來源:互聯網
上載者:User

標籤:截取   檢查   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">
&nbsp;&nbsp;<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>&nbsp;&nbsp;
<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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.