再导入excel的时候出现问题:只能导入excel表的最后一行,报错是:Duplicate entry '142' for key 'PRIMARY'。
package com.kefu.dao;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.kefu.bean.VipInfo;
public class LzxDao {
public List<VipInfo> readXls(String filePath) throws IOException {
List<VipInfo> list = new ArrayList<VipInfo>();
FileInputStream fis = new FileInputStream(filePath);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook workbook = new HSSFWorkbook(fs);//创建Excel工作簿对象
HSSFSheet sheet = workbook.getSheetAt(0);//获取第1个工作表
for(int i=1;i<sheet.getPhysicalNumberOfRows();i++){
HSSFRow row = sheet.getRow(i);
VipInfo emp=new VipInfo();
if(row==null){
continue;
}
String orgId = row.getCell(0).toString();
String orgId1 = orgId.substring(0, orgId.indexOf('.'));
emp.setVipid(Integer.parseInt((orgId1)));
HSSFCell cell2 = row.getCell(1);
if(cell2==null){
continue;
}
String card = cell2.getStringCellValue();//获取第i行的第2个单元格的数据
emp.setCard(card);
HSSFCell cell3 = row.getCell(2);
if(cell3==null){
continue;
}
String vipname = cell3.getStringCellValue();//获取第i行的第3个单元格的数据
emp.setVipname(vipname);
HSSFCell cell4 = row.getCell(3);
if(cell4==null){
continue;
}
String vipsex= cell4.getStringCellValue();//获取第i行的第4个单元格的数据
emp.setVipsex(vipsex);
HSSFCell cell5 = row.getCell(4);
if(cell5==null){
continue;
}
String vipjob = cell5.getStringCellValue();//获取第i行的第5个单元格的数据
emp.setVipjob(vipjob);
HSSFCell cell6 = row.getCell(5);
if(cell6==null){
continue;
}
String vipgrad=cell6.getStringCellValue();
emp.setVipgrad(vipgrad);
HSSFCell cell7 = row.getCell(6);
if(cell7==null){
continue;
}
String phonef=cell7.getStringCellValue();
emp.setPhonef(phonef);
HSSFCell cell8 = row.getCell(7);
if(cell8==null){
continue;
}
String phones=cell8.getStringCellValue();
emp.setPhones(phones);
HSSFCell cell9 = row.getCell(8);
if(cell9==null){
continue;
}
String comname=cell9.getStringCellValue();
emp.setComname(comname);
HSSFCell cell10 = row.getCell(9);
if(cell10==null){
continue;
}
String comadd=cell10.getStringCellValue();
emp.setComadd(comadd);
HSSFCell cell11 = row.getCell(10);
if(cell11==null){
continue;
}
String homadd=cell11.getStringCellValue();
emp.setHomadd(homadd);
HSSFCell cell12 = row.getCell(11);
if(cell12==null){
continue;
}