文件上传之解析EXCEL

Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。
下面是一个解析EXCL文件的完整代码:
解析Excle模板如下:
这里写图片描述

代码如下:

    /**
     * 读取问卷试题
     * @param file 文件路径
     * @return
     */
    private List<QsbknewQuestionPage> readQuestion(String file) {
        //记录各字段所在列
        int cqt = 0;//题目类型
        int cqn = 0;//问题编号
        int cqd = 0;//问题描述
        int cqa1 = 0;//答案项1
        int cqa2 = 0;//答案项2
        int cqa3 = 0;//答案项3
        int cqa4 = 0;//答案项4
        int cqa5 = 0;//答案项5
        int cqa6 = 0;//答案项5

        //存储问题
        List<QsbknewQuestionPage> qsbList = new ArrayList<QsbknewQuestionPage>();

        try{
            //获取工作簿
            Workbook book = Workbook.getWorkbook(new File(file));
            Sheet[] sheets = book.getSheets();
            Sheet sheet = sheets[0];
            int rows = sheet.getRows();
            //第2行为标题行
            Cell [] cells = sheet.getRow(1);
            for(int i = 0;i < cells.length;i++){
                String cellValue = cells[i].getContents();
                // 获取各字段所在的列
                if (!cellValue.trim().contains("答案项")) {
                     if ("题目类型".equals(cellValue.trim())) {
                        cqt = i;
                    } else if ("问题编号".equals(cellValue.trim())) {
                        cqn = i;
                    } else if ("题目描述".equals(cellValue.trim())) {
                        cqd = i;
                    }
                } else {
                    // 答案项
                    if ("答案项1".equals(cellValue.trim())) {
                        cqa1 = i;
                    } else if ("答案项2".equals(cellValue.trim())) {
                        cqa2 = i;
                    } else if ("答案项3".equals(cellValue.trim())) {
                        cqa3 = i;
                    } else if ("答案项4".equals(cellValue.trim())) {
                        cqa4 = i;
                    } else if ("答案项5".equals(cellValue.trim())) {
                        cqa5 = i;
                    } else if ("答案项6".equals(cellValue.trim())) {
                        cqa6 = i;
                    }

                }
            }
            //取值
            Cell cell = null;
            String cValue = "";
            for(int r = 2; r < rows; r++){
                QsbknewQuestionPage qsbp = new QsbknewQuestionPage();
                QsbkQuestion qsb = new QsbkQuestion();
                StringBuffer answer = new StringBuffer();
                // 题目类型
                cell = sheet.getCell(cqt, r);
                cValue = cell.getContents().trim();
                ///单选题1、多选题2、是否题3、简答题5
                if("单选题".equals(cValue.trim())){
                    cValue = "1";
                }else if("多选题".equals(cValue.trim())){
                    cValue = "2";
                }else if("是否题".equals(cValue.trim())){
                    cValue = "3";
                }else if("简答题".equals(cValue.trim())){
                    cValue = "5";
                }else{
                    continue;
                }
                qsb.setQuestionType(cValue);

                // 问题编号
                cell = sheet.getCell(cqn, r);
                cValue = cell.getContents().trim();
                ///问题编号必须是6位
                Pattern pt = Pattern.compile("^\\d*$");
                Matcher mc = pt.matcher(cValue);
                if(mc.find()&&cValue.length()==6){
                qsb.setReserve2(cValue);
                }else{
                    continue;
                }

                // 题目描述
                cell = sheet.getCell(cqd, r);
                cValue = cell.getContents().trim();
                ///题目不能为空
                if(!cValue.isEmpty()){
                qsb.setQuestionTitle(cValue);
                }else{
                    continue;
                }

                // 答案项1
                cell = sheet.getCell(cqa1, r);
                cValue = cell.getContents().trim();
                ///答案长度不能超过2000
                if (!cValue.isEmpty()&&cValue.length()<2000) {
                    answer.append(cValue + ",");
                }

                // 答案项2
                cell = sheet.getCell(cqa2, r);
                cValue = cell.getContents().trim();
                if (!cValue.isEmpty()&&cValue.length()<2000) {
                    answer.append(cValue + ",");
                }

                // 答案项3
                cell = sheet.getCell(cqa3, r);
                cValue = cell.getContents().trim();
                if (!cValue.isEmpty()&&cValue.length()<2000) {
                    answer.append(cValue + ",");
                }

                // 答案项4
                cell = sheet.getCell(cqa4, r);
                cValue = cell.getContents().trim();
                if (!cValue.isEmpty()&&cValue.length()<2000) {
                    answer.append(cValue + ",");
                }

                // 答案项5
                cell = sheet.getCell(cqa5, r);
                cValue = cell.getContents().trim();
                if (!cValue.isEmpty()&&cValue.length()<2000) {
                    answer.append(cValue + ",");
                }

                // 答案项6
                cell = sheet.getCell(cqa6, r);
                cValue = cell.getContents().trim();
                if(!cValue.isEmpty()&&cValue.length()<2000){
                    answer.append(cValue);
                }

                // 答案页中暂存答案项,然后获取时以逗号分开存入数组
                qsb.setQuestionRemark(answer.toString());
                if(!qsb.getQuestionRemark().isEmpty()&&!qsb.getQuestionType().isEmpty()&&!qsb.getQuestionTitle().isEmpty()){
                qsbp.setQsbkQuestion(qsb);
                qsbList.add(qsbp);
                }

            }
            System.out.println(qsbList.size());
            book.close();

        }catch(Exception e){
            e.printStackTrace();
        }

        for(QsbknewQuestionPage q:qsbList){
            System.out.println(q.getQsbkQuestion().getQuestionType()+"| "
                    +q.getQsbkQuestion().getReserve2()+"|   "
                    +q.getQsbkQuestion().getQuestionTitle()+"|  "
                    +q.getQsbkQuestion().getReserve4()+"| ");
        }
        return qsbList;

    }

当然需要导入一些包:这个不多做解释

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

比如调用的时候:

这里写图片描述

本文件没有过多的解释,因为网上已经有很多,我主要是想记录一个比较完整的应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值