有一个插件
exl
好像是这个名字,自己去搜索一下

解决方案 »

  1.   

    /**
         * 根据formatExcel文件中的格式读取dataExcel中的数据
         *
         * @param formatExcel excel模板的指针
         * @param dataExcel   excle格式存放的数据
         * @return
         */
        public Map readImportdata(HSSFWorkbook formatExcel, HSSFWorkbook dataExcel) {
            int sheetNumber = 0;
            int rowNumber = 0;
            int cellNumber = 0;
            int allSheet = 0;
            boolean needSkipFirstRow = true;
            List resultList = new ArrayList();        /**
             * resultMap 内含有如下内容:
             * flag         String     true:正常执行,可以取结果集 false:程序执行流程错误,但没有异常
             * msg          String     如果程序出错,此内容保存的是出错信息。
             * resultList   List       如果程序正常执行,这里保存的是结果集
             */
            Map resultMap = new HashMap();
            String flag = "true";
            StringBuffer msg = new StringBuffer("");
            resultMap.put("flag", flag);
            resultMap.put("msg", msg.toString());
            if (formatExcel == null) return resultMap;
            if (dataExcel == null) return resultMap;        initialize();
            getImportColumns(formatExcel);
            allSheet = dataExcel.getNumberOfSheets();        for (sheetNumber = 0; sheetNumber < allSheet; sheetNumber++) {//读取所有的sheet页
                HSSFSheet dataSheet = dataExcel.getSheetAt(sheetNumber);
                int allrows = dataSheet.getLastRowNum();
                Iterator rowIt = dataSheet.rowIterator();            //如果没有内容,返回,判断是否需要跳过第一行
                if (rowIt.hasNext()) {
                    if (needSkipFirstRow) {
                        rowIt.next();
                        rowNumber++;
                    }
                }            //循环读数据
                while (rowIt.hasNext()) {
                    HSSFRow dataRow = (HSSFRow) rowIt.next();
    //                HSSFRow dataRow = (HSSFRow)dataSheet.getRow(currentrow);
                    int allcells = dataRow.getLastCellNum();
                    short currentCol = 0;
    //                Iterator cellIt = dataRow.cellIterator();
                    int columnsize = importColumns.size();
                    cellNumber = 0;
                    Map oneRow = new HashMap();
                    for (currentCol = 0; currentCol < allcells; currentCol++) {
    //                while(cellIt.hasNext() && currentCol <columnsize){
    //                    HSSFCell dataCell = (HSSFCell)cellIt.next();
                        HSSFCell dataCell = dataRow.getCell(currentCol);
    //                    currentCol = dataCell.getCellNum();
                        cellNumber = currentCol;
                        Object colType = importColumns.get(currentCol);
                        if (colType instanceof Map) {
                            //进行字段的校验和转换
                            Map tempcolType = (Map) colType;
                            String cellContent = getStringFromCell(dataCell);                        String allownull = (String) tempcolType.get("allownull");
                            String dbname = (String) tempcolType.get("dbname");
                            String format = (String) tempcolType.get("format");
                            Integer maxlength = (Integer) tempcolType.get("maxlength");
                            String dictionary = (String) tempcolType.get("dictionary");
                            System.out.println(dbname + " = " + cellContent);
                            if (dbname == null) dbname = "";
                            //进行非空检验
                            if (allownull == null) allownull = "true";
                            if (allownull.equals("false")) {
                                if (cellContent == null || cellContent.trim().equals("")) {
                                    flag = "false";
                                    msg.append(
                                            " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元格[" +
                                            cellContent +
                                            "]不能为空。\n");
                                    oneRow = null;
                                    break;
                                }
                            }
                            //进行maxlength校验
                            if (maxlength == null) {
                            } else {
                                if (cellContent.trim().length() > maxlength.intValue()) {
                                    flag = "false";
                                    msg.append(
                                            " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" +
                                            cellContent +
                                            "]太长。\n");
                                    oneRow = null;
                                    break;
                                }
                            }
                            //进行format校验
                            if (format == null || format.trim().equals("")) {
                            } else {
                                if (!checkFormat(cellContent, format)) {
                                    flag = "false";
                                    msg.append(
                                            " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" +
                                            cellContent +
                                            "]格式不对。\n");
                                    oneRow = null;
                                    break;
                                }
                            }
                            //进行dictionary处理
                            if (dictionary == null || dictionary.trim().equals("")) {
                                oneRow.put(dbname, cellContent);
                            } else {
                                Object newvalue = fromContentToCode(cellContent, dictionary);
                                oneRow.put(dbname, newvalue);
                            }
                        } else {
                            //对这个cell不做处理                    }
    //                    currentCol ++;
                        cellNumber++;
                    }                if (oneRow != null && oneRow.size() > 0) resultList.add(oneRow);
                    rowNumber++;            }
            }
            resultMap.put("flag", flag);
            resultMap.put("msg", msg.toString());
            resultMap.put("resultList", resultList);
            return resultMap;
        }
      

  2.   

    分不走吧:
    先会什么样把数据从excel 取出,再倒入数据库