文件的导入

本文介绍了一个Java程序,用于从Excel文件中导入诉讼案件数据到数据库。程序详细展示了如何读取Excel表格的内容,并将数据映射到特定的Java对象,最终批量保存到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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";
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值