<!--HTML代码-->
<input type="file" name="uploadFile" id="uploadFile">
<!--JQuery代码-->
$("#uploadFile").on("change", function() {
debugger;
var obj=$("#uploadFile")[0].files;
var formData = new FormData(); // 创建一个form类型的数据
formData.append('file',obj[0]); // 获取上传文件的数据
$.ajax({
"url": "",
"type": "",
"processData": false, // 将数据转换成对象,不对数据做处理,故 processData: false
"contentType": false, // 不设置数据类型
"xhrFields": { // 这样在请求的时候会自动将浏览器中的cookie发送给后台
withCredentials: true
},
"data": formData,
success: function(data) {
console.log(data)
},
error: function(data) {
}
})
})
后端代码
// 取得上传的文件
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
// 得到文件名称
String realFileName = file.getOriginalFilename();
String suffix = realFileName.substring(realFileName.indexOf("."),
realFileName.length());
Workbook workbook = null;
//判断文件类型(可忽略)
if (".xlsx".equals(suffix)) {
workbook = new XSSFWorkbook(file.getInputStream());
} else {
workbook = new HSSFWorkbook(file.getInputStream());
}
List<WGxZdywCrm> empList = getEmpList(workbook);
message=JSONObject.fromObject(empList);
后端可以将获取的文件传到服务器,也可以将文件内容解析出来。
文件内容解析(excel文件)放入对象
List<WGxZdywCrm> wGxZdywCrmList = new ArrayList<>();
SimpleDateFormat dateFormatUtil=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
Sheet sheet = wookbook.getSheetAt(0);//统计excel的行数
int rowLen = sheet.getPhysicalNumberOfRows();//excel总行数,记录数=行数-1
for (int i = 1; i < rowLen; i++) {
WGxZdywCrm wGxZdywCrm = new WGxZdywCrm();
Row row = sheet.getRow(i);
int startCol = 0;
if (row != null) {
String fZdywSort = getValue(row.getCell(startCol++));//getValue下面贴出来了
String fZdywCrmno = getValue(row.getCell(startCol++));
String fZdywBusinesscode = getValue(row.getCell(startCol++));
String fZdywContractno = getValue(row.getCell(startCol++));
String fZdywCustomercount = getValue(row.getCell(startCol++));
String fZdywPhonecount = getValue(row.getCell(startCol++));
String fZdywAccepttime = getValue(row.getCell(startCol++));
String fZdywFinishtime = getValue(row.getCell(startCol++));
String fZdywNote = getValue(row.getCell(startCol++));
wGxZdywCrm.setFZdywSort(Long.valueOf(fZdywSort));
wGxZdywCrm.setFZdywCrmno(fZdywCrmno);
wGxZdywCrm.setFZdywBusinesscode(fZdywBusinesscode);
wGxZdywCrm.setFZdywContractno(fZdywContractno);
wGxZdywCrm.setFZdywCustomercount(fZdywCustomercount);
wGxZdywCrm.setFZdywPhonecount(fZdywPhonecount);
wGxZdywCrm.setFZdywAccepttime(dateFormatUtil.parse(fZdywAccepttime));
wGxZdywCrm.setFZdywFinishtime(dateFormatUtil.parse(fZdywFinishtime));
wGxZdywCrm.setFZdywNote(fZdywNote);
wGxZdywCrmList.add(wGxZdywCrm);
}
}
return wGxZdywCrmList;
获取内容
private String getValue(Cell cell) {
if (cell == null)
return "";
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue()).trim();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
DecimalFormat df = new DecimalFormat("#");
return String.valueOf(df.format(cell.getNumericCellValue())).trim();
} else {
cell.setCellType(cell.CELL_TYPE_STRING);
return String.valueOf(cell.getStringCellValue()).trim();
}
}
更多推荐
jquery传输文件到后端,后端处理数据。
发布评论