以下是我的導入excel工具類
public class StuExcelUtil {
public static List<Student> redexcel(File file) throws Exception{
List<Student> list=new ArrayList<Student>();
InputStream input=new FileInputStream(file);
Workbook wb = Workbook.getWorkbook(input);
Sheet sheet=wb.getSheet(0);
int row=sheet.getRows();
int colums=sheet.getColumns();
/* 如何寫入帶圖片的excel?
int imgNum=sheet.getNumberOfImages();
if(imgNum>0){
for (int i = 0; i < imgNum; i++) {
Image image=sheet.getDrawing(i);
byte[] imageData = image.getImageData();
String fileName = image.getImageFile().getName().trim();
}
}
*/
for (int i = 1; i < row; i++) {
for (int j = 0; j < colums; j++) {
Student stu=new Student();
stu.setName(sheet.getCell(j++, i).getContents());
stu.setSex(Integer.parseInt(sheet.getCell(j++, i).getContents()));
stu.setAge(Integer.parseInt(sheet.getCell(j++, i).getContents()));
stu.setGrade(Integer.parseInt(sheet.getCell(j++, i).getContents()));
stu.setWishType(Integer.parseInt(sheet.getCell(j++, i).getContents()));
stu.setWishDesc(sheet.getCell(j++, i).getContents());
stu.setWishStory(sheet.getCell(j++, i).getContents());
stu.setTeacherWords(sheet.getCell(j++, i).getContents());
stu.setTeacherMobile(DesUtils.encryptBasedDes(sheet.getCell(j++,i).getContents()));
list.add(stu);
}
}
return list;
}
}
從excel文件中獲取到數據,通過批處理發送給數據庫進行處理,
數據庫那邊寫一個存儲過程,在往裡插入數據之前,先進行查詢,
如果查到數據庫中已經有相同的數據,就不進行插入,否則就執行插入操作。