@SuppressWarnings("serial")
public class ImportLawsuitDataFromExcel extends BaseService implements Serializable{
private static final Logger log = LoggerFactory
.getLogger(ImportLawsuitDataFromExcel.class);
public String importUser(File file) {
String message = "";
try {
message = insertExcelDatabase(file);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
private static String returnNotNullValue(String str){
return "null".equalsIgnoreCase(str) ? "" : str;
}
public String insertExcelDatabase(File file) {
try {
String message = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
// 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")
HSSFSheet sheet = workbook.getSheet("Sheet1");
// 设置共有多少列
String[] arrayExcelRow = null;
List<Lawsuit> list=new ArrayList<Lawsuit>();
for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) {
// i代表excel表中的行
HSSFRow row = sheet.getRow(i);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
// 每次一定要重新初始化,否则数据会对不上,不指定空字符串,数据中会出现null
arrayExcelRow = new String[23];
for(int j=0;j<arrayExcelRow.length;j++){
arrayExcelRow[j] = "";
}
for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
try {
arrayExcelRow[c] = returnNotNullValue(String.valueOf(cell
.getNumericCellValue()));
} catch (IllegalStateException e) {
arrayExcelRow[c] = returnNotNullValue(String.valueOf(cell
.getStringCellValue()));
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
Date date = HSSFDateUtil.getJavaDate(d);
arrayExcelRow[c] = returnNotNullValue(sdf.format(date));
} else {
arrayExcelRow[c] = returnNotNullValue(String.valueOf((float) cell.getNumericCellValue()));
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
arrayExcelRow[c] = returnNotNullValue(cell.getStringCellValue());
}
}
}
if (arrayExcelRow[0] != null&&!"".equals(arrayExcelRow[0].trim())) {
list.add(setLawsuitPojo(arrayExcelRow));
}
}
}
if (list != null)
commonDao.saveOrUpdateAll(list); //批量保存excel中的数据到数据库
if ("".equals(message)) {
return "导入成功";
} else {
return "无工号为" + message.substring(1, message.length())
+ "的员工,请确认信息。";
}
} catch (Exception e) {
e.printStackTrace();
return "导入失败,请检查excel格式是否正确。";
}
}
/*
* 给lawsuit对象赋值并返回
* param:arrayExcelRow excel一行的值
*/
public Lawsuit setLawsuitPojo(String[] arrayExcelRow){
Lawsuit lawsuit =new Lawsuit();
//以下是从excel中导入的数据
lawsuit.setLawsuitIdentifier(arrayExcelRow[0]);
lawsuit.setAgreementIdentifier(arrayExcelRow[1]);
if("".equals(arrayExcelRow[2]))
lawsuit.setRegistDate(null);
else
lawsuit.setRegistDate(DateUtil.parseDate(arrayExcelRow[2],DateUtil.format3));
lawsuit.setCaseProperty(arrayExcelRow[3]);
lawsuit.setPlaintiff(arrayExcelRow[4]);
lawsuit.setDefendant(arrayExcelRow[5]);
if("".equals(arrayExcelRow[6]))
lawsuit.setTortDate(null);
else
lawsuit.setTortDate(DateUtil.parseDate(arrayExcelRow[6],DateUtil.format3));
lawsuit.setCaseStage(arrayExcelRow[7]);
lawsuit.setTortType(arrayExcelRow[8]);
lawsuit.setCaseIdentifier(arrayExcelRow[9]);
lawsuit.setAcceptCourt(arrayExcelRow[10]);
lawsuit.setIsAppeal(getIsAppealString(arrayExcelRow[11]));
lawsuit.setCaseReason(arrayExcelRow[12]);
lawsuit.setOpusType(arrayExcelRow[13]);
if("".equals(arrayExcelRow[14]))
lawsuit.setBaseAccount(null);
else
lawsuit.setBaseAccount(Float.parseFloat(arrayExcelRow[14]));
lawsuit.setLawyerOfcaseName(arrayExcelRow[15]);
lawsuit.setCooperateLawyer(arrayExcelRow[16]);
lawsuit.setCaseProgress(arrayExcelRow[17]);
if("".equals(arrayExcelRow[18]))
lawsuit.setSupportUndertakeAccount(null);
else
lawsuit.setSupportUndertakeAccount(Float.parseFloat(arrayExcelRow[18]));
if("".equals(arrayExcelRow[19]))
lawsuit.setContributeAccount(null);
else
lawsuit.setContributeAccount(Float.parseFloat(arrayExcelRow[19]));
lawsuit.setCloseCaseIdentifier(arrayExcelRow[20]);
if("".equals(arrayExcelRow[21]))
lawsuit.setCloseCaseDate(null);
else
lawsuit.setCloseCaseDate(DateUtil.parseDate(arrayExcelRow[21],DateUtil.format3));
lawsuit.setRemarks(arrayExcelRow[22]);
return lawsuit;
}
//将excel中的是否上诉转换为1(是)或者0(否)
public String getIsAppealString(String isAppeal) {
if (isAppeal == null) {
return "";
} else if ("是".equalsIgnoreCase(isAppeal)) {
return "1";
} else {
return "0";
}
}
}
public class ImportLawsuitDataFromExcel extends BaseService implements Serializable{
private static final Logger log = LoggerFactory
.getLogger(ImportLawsuitDataFromExcel.class);
public String importUser(File file) {
String message = "";
try {
message = insertExcelDatabase(file);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
private static String returnNotNullValue(String str){
return "null".equalsIgnoreCase(str) ? "" : str;
}
public String insertExcelDatabase(File file) {
try {
String message = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
// 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")
HSSFSheet sheet = workbook.getSheet("Sheet1");
// 设置共有多少列
String[] arrayExcelRow = null;
List<Lawsuit> list=new ArrayList<Lawsuit>();
for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) {
// i代表excel表中的行
HSSFRow row = sheet.getRow(i);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
// 每次一定要重新初始化,否则数据会对不上,不指定空字符串,数据中会出现null
arrayExcelRow = new String[23];
for(int j=0;j<arrayExcelRow.length;j++){
arrayExcelRow[j] = "";
}
for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
try {
arrayExcelRow[c] = returnNotNullValue(String.valueOf(cell
.getNumericCellValue()));
} catch (IllegalStateException e) {
arrayExcelRow[c] = returnNotNullValue(String.valueOf(cell
.getStringCellValue()));
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
Date date = HSSFDateUtil.getJavaDate(d);
arrayExcelRow[c] = returnNotNullValue(sdf.format(date));
} else {
arrayExcelRow[c] = returnNotNullValue(String.valueOf((float) cell.getNumericCellValue()));
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
arrayExcelRow[c] = returnNotNullValue(cell.getStringCellValue());
}
}
}
if (arrayExcelRow[0] != null&&!"".equals(arrayExcelRow[0].trim())) {
list.add(setLawsuitPojo(arrayExcelRow));
}
}
}
if (list != null)
commonDao.saveOrUpdateAll(list); //批量保存excel中的数据到数据库
if ("".equals(message)) {
return "导入成功";
} else {
return "无工号为" + message.substring(1, message.length())
+ "的员工,请确认信息。";
}
} catch (Exception e) {
e.printStackTrace();
return "导入失败,请检查excel格式是否正确。";
}
}
/*
* 给lawsuit对象赋值并返回
* param:arrayExcelRow excel一行的值
*/
public Lawsuit setLawsuitPojo(String[] arrayExcelRow){
Lawsuit lawsuit =new Lawsuit();
//以下是从excel中导入的数据
lawsuit.setLawsuitIdentifier(arrayExcelRow[0]);
lawsuit.setAgreementIdentifier(arrayExcelRow[1]);
if("".equals(arrayExcelRow[2]))
lawsuit.setRegistDate(null);
else
lawsuit.setRegistDate(DateUtil.parseDate(arrayExcelRow[2],DateUtil.format3));
lawsuit.setCaseProperty(arrayExcelRow[3]);
lawsuit.setPlaintiff(arrayExcelRow[4]);
lawsuit.setDefendant(arrayExcelRow[5]);
if("".equals(arrayExcelRow[6]))
lawsuit.setTortDate(null);
else
lawsuit.setTortDate(DateUtil.parseDate(arrayExcelRow[6],DateUtil.format3));
lawsuit.setCaseStage(arrayExcelRow[7]);
lawsuit.setTortType(arrayExcelRow[8]);
lawsuit.setCaseIdentifier(arrayExcelRow[9]);
lawsuit.setAcceptCourt(arrayExcelRow[10]);
lawsuit.setIsAppeal(getIsAppealString(arrayExcelRow[11]));
lawsuit.setCaseReason(arrayExcelRow[12]);
lawsuit.setOpusType(arrayExcelRow[13]);
if("".equals(arrayExcelRow[14]))
lawsuit.setBaseAccount(null);
else
lawsuit.setBaseAccount(Float.parseFloat(arrayExcelRow[14]));
lawsuit.setLawyerOfcaseName(arrayExcelRow[15]);
lawsuit.setCooperateLawyer(arrayExcelRow[16]);
lawsuit.setCaseProgress(arrayExcelRow[17]);
if("".equals(arrayExcelRow[18]))
lawsuit.setSupportUndertakeAccount(null);
else
lawsuit.setSupportUndertakeAccount(Float.parseFloat(arrayExcelRow[18]));
if("".equals(arrayExcelRow[19]))
lawsuit.setContributeAccount(null);
else
lawsuit.setContributeAccount(Float.parseFloat(arrayExcelRow[19]));
lawsuit.setCloseCaseIdentifier(arrayExcelRow[20]);
if("".equals(arrayExcelRow[21]))
lawsuit.setCloseCaseDate(null);
else
lawsuit.setCloseCaseDate(DateUtil.parseDate(arrayExcelRow[21],DateUtil.format3));
lawsuit.setRemarks(arrayExcelRow[22]);
return lawsuit;
}
//将excel中的是否上诉转换为1(是)或者0(否)
public String getIsAppealString(String isAppeal) {
if (isAppeal == null) {
return "";
} else if ("是".equalsIgnoreCase(isAppeal)) {
return "1";
} else {
return "0";
}
}
}