依赖
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
通用controller
package com.olio;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
public class TestDemo {
public void readExcel(String path,String strURL) {
try {
Workbook workbook = Workbook.getWorkbook(new File(path));
Sheet sheet = workbook.getSheet(0);
for (int i = 1; i < sheet.getRows(); i++) {
String id = sheet.getCell(0, i).getContents();
String name = sheet.getCell(1,i).getContents();
String sex = sheet.getCell(2, i).getContents();
String grade = sheet.getCell(3, i).getContents();
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
}
具体实操
@PostMapping("/addlist")
@ResponseBody
public List<CommonDict> addlist( MultipartFile file) {
try {
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheet(0);
for (int i = 0; i < sheet.getRows(); i++) {
CommonDict commonDict = new CommonDict();
String code = sheet.getCell(0, i).getContents();
String name = sheet.getCell(1, i).getContents();
String sex = sheet.getCell(2, i).getContents();
commonDict.setDictCode(code);
commonDict.setDictName(name);
List<CommonDict> commonDicts = commonDictService.selectCommonDictByCode(sex);
if (commonDicts!=null&&commonDicts.size()>0){
if(commonDicts.get(0)!=null){
commonDict.setParentId( commonDicts.get(0).getId());
commonDict.setOrderNum(0);
commonDictService.insertCommonDict(commonDict);
}
}
}
return null;
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return null;
}
excel只能xls格式!!!!