excelファイルをアップロードします
3495 ワード
javascript:
// xls
var button = $('#importExcel');
log(button);
new AjaxUpload(button, {
action : 'phoneNoMarket/xlsupload',
name : 'file',
onSubmit : function(file, ext) {
if (!(ext && /^(xls)$/.test(ext))) {
alert(' , xls !', ' ');
return false;
}
this.disable();
},
onComplete : function(file, response) {
this.enable();
var k = response.replace("<pre style=\"word-wrap: break-word; white-space: pre-wrap;\">", "").replace("</pre>", "");
k=eval('('+k+')');
importlist = k.importData;
convtohtml2add(k.importData);
return false;
}
});
java:@RequestMapping(value = "/xlsupload", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
public @ResponseBody String xlsupload(HttpServletRequest request
, HttpServletResponse response,HttpSession session
, <span style="color:#ff0000;">@RequestParam("file") MultipartFile file</span>) {
String strResult = "";
CondQueryRepHmglView repview = new CondQueryRepHmglView();
List<HmglAddGroupNumRepView> pagedata = new ArrayList<HmglAddGroupNumRepView>();
<span style="color:#ff0000;">Workbook workbook;</span>
if (!file.isEmpty()) {
try {
//
<span style="color:#ff0000;">String filePath = request.getSession().getServletContext()
.getRealPath("/")
+ "upload/" + file.getOriginalFilename();</span>
File newfile = new File(filePath);
if (!newfile.exists()) {
//
file.transferTo(new File(filePath));
} else {
newfile.delete();
file.transferTo(new File(filePath));
}
workbook = Workbook.getWorkbook(new File(filePath));
// excel
Sheet sheet = workbook.getSheet(0);
String delimiter = ",";
List<String> dataList = new ArrayList<String>();
for (int i = 1; i < sheet.getRows(); i++) {
String string = "";
for (int j = 0; j < sheet.getColumns(); j++) {
// <span style="color:#ff0000;">sheet.getCell(j,i).getContents(); </span>
string += sheet.getCell(j, i).getContents().equals("") ? " "
+ delimiter
: sheet.getCell(j, i).getContents() + delimiter;
}
dataList.add(string);
}
for (int i = 0; i < dataList.size(); i++) {
String data = dataList.get(i);
String dataArray[] = data.split(",");
HmglAddGroupNumRepView repRecord = new HmglAddGroupNumRepView();
//
repRecord.setPhonenumber(dataArray[0]);
// TODO:
//
repRecord.setAreaname(dataArray[2]);
//
repRecord.setOperatorsname(dataArray[3]);
//
repRecord.setCreatedatetime("");
//
repRecord.setSelectdatetime(dataArray[4]);
//
repRecord.setAngentno(dataArray[5]);
// : 0
repRecord.setIsselect("0");
pagedata.add(repRecord);
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
repview.setImportData(pagedata);
strResult = JsonUtil.toJson(repview);
return strResult;
}