alert(“失败”)
}
});
})
这个是js脚本内容,注意的是 processData,contentType这两个属性必须为false;
至此前端内容就结束了,可以直接copy了改就可以了
接下来再看看后端代码,我会贴出excel解析的部分,有需要的可以作为参考:
- controller层
@ResponseBody
@RequestMapping(value = “/excleimport”, method = RequestMethod.POST)
public Map<String, Object> excleimport(@RequestParam MultipartFile excelFile,
HttpServletResponse response) throws IOException {
response.setHeader(“Access-Control-Allow-Origin”, “*”);
Map<String, Object> map = new HashMap<String, Object>();
String name = excelFile.getOriginalFilename();
if (!name.endsWith(“.xls”) && !name.endsWith(“.xlsx”)) {
System.out.println(“文件不是excel类型”);
map.put(“filecondition”, “文件类型错误”);
} else {
ExcelImport.getDataFromExcel(excelFile.getInputStream());
}
map.put(“result”, “success”);
return map;
}
到此所有的文件传值与接收就结束了,下面在附上ExcelImport.getDataFromExcel方法,也就是解析excel的方法:
package com.boot.zhiyi.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelImport {
public static void getDataFromExcel(InputStream InputStream )
{
Workbook wookbook = null;
try
{
//2003版本的excel,用.xls结尾
wookbook = new HSSFWorkbook(InputStream);//得到工作簿
}
catch (Exception ex)
{
//ex.printStackTrace();
try
{
//2007版本的excel,用.xlsx结尾
wookbook = new XSSFWorkbook(InputStream);//得到工作簿
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//得到一个工作表
Sheet sheet = wookbook.getSheetAt(0);
//获得表头
Row rowHead = sheet.getRow(0);
//判断表头是否正确
if(rowHead.getPhysicalNumberOfCells()<1 )
{
System.out.println(“表头的数量不对!”);
}
//获得数据的总行数
int totalRowNum = sheet.getLastRowNum();
//要获得属性
Long phoneNumber=null ;
String deptName=null;
String userName = “”;
//获得所有数据
for(int i = 1 ; i <= totalRowNum ; i++)
{
//获得第i行对象
Row row = sheet.getRow(i);
//获得获得第i行第0列的 String类型对象
Cell cell = row.getCell((short)0);
phoneNumber =(long) cell.getNumericCellValue();
//获得一个字符串类型的数据
cell = row.getCell((short)1);
deptName = cell.getStringCellValue().toString();
cell = row.getCell((short)2);
userName = cell.getStringCellValue().toString();
/*
最后
我可以将最近整理的前端面试题分享出来,其中包含HTML、CSS、JavaScript、服务端与网络、Vue、浏览器、数据结构与算法等等,还在持续整理更新中,希望大家都能找到心仪的工作。
篇幅有限,仅展示部分截图: