java导入Excel文件

在题库管理系统中,经常需要批量导入题目信息,本文讲述如何批量导入存储在Excel工作表中的题目信息。

假设题目存储格式如下(只导入单选、多选和判断题):

题干 题型 难易度 答案 默认分数 选项A 选项B 选项C 选项D 选项E 选项F 。。。




从第六列开始为选项,选项个数不定。第六列对应选项A,第七列对应选项B,依次类推。。

单选题的答案只能是单个大写字母,多选题答案至少为两个,以大写字母表示,且中间不以任何符号分隔(如:ABC),判断题的答案用0或1表示,0表示错误,1表示正确。


下载jxl.jar,放入类路径中。
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

。。。


try {
//读取EXCEL文件的值
InputStream is = new FileInputStream(excelfile);
Workbook wbk = Workbook.getWorkbook(is);
//读取工作表的数据
for(int k=0; k<wbk.getNumberOfSheets();k++){
Sheet rs = wbk.getSheet(k);
saveExcelToElement(rs);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

。。。
//检查Excel文件是否合法,遇见不合法的地方就跳出,并返回提示信息
public String checkExcel(Sheet r){
String info = "";
String[] QuestionFlag ={"A", "B", "C", "D", "E", "F","G","H","I","J"};
//共有多少行
int row = rs.getRows();

//循环输出
Cell c0 = null;
Cell c1 = null;
Cell c2 = null;
Cell c3 = null;
Cell c4 = null;
Cell cn = null;
for(int k=1;k<row;k++)
{
int rownum = k+1;
Cell[] cells = rs.getRow(k);
int len = cells.length;
c0 = rs.getCell(0, k);//题干
c1 = rs.getCell(1, k);//类型
c2 = rs.getCell(2, k);//难易度
c3 = rs.getCell(3, k);//答案("A","AB")
c4 = rs.getCell(4, k);//默认得分
if(c0.getContents()==null||c0.getContents().equals("")){
info = "题干不能为空,第"+rownum+"行题干内容为空";
break;
}
if(c1.getContents()==null||c1.getContents().equals("")){
info = "题目类型不能为空,第"+rownum+"行题目类型内容为空";
break;
}
else{
if(!(c1.getContents().equals("1")||c1.getContents().equals("2")||c1.getContents().equals("3"))){
info = "题目类型只能为1,2,3.其中1代表单选题,2代表多选题,3代表判断题。请检查第"+rownum+"行的题型字段";
break;
}
}
if(c2.getContents()!=null&&(Double.parseDouble(c2.getContents())>1||Double.parseDouble(c2.getContents())<0)){
info = "难易度只能是大于0小于1的小数,请检查第"+rownum+"行";
break;
}
String c3Content = c3.getContents();
if(c3Content==null||c3Content.equals("")){
info = "答案不能为空,请检查第"+rownum+"行";
break;
}
else{
String[] str = new String[c3Content.length()];
for(int i=0;i<c3Content.length();i++){
str[i] = String.valueOf(c3Content.charAt(i));
}
String letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0;i<str.length;i++){
if(!c1.getContents().equals("3")&&letter.indexOf(str[i])==-1){
info = "选择题答案只能包含大写字母。请检查第"+rownum+"行";
break;
}
}
if(c1.getContents().equals("1")){
if(str.length>1){
info = "单选题只能有一个答案,请检查第"+rownum+"行";
break;
}

}
if(c1.getContents().equals("2")){
if(str.length<2){
info = "多选题至少有两个答案,请检查第"+rownum+"行";
break;
}

}
if(c1.getContents().equals("3")){
if(!(c3.getContents().equals("0")||c3.getContents().equals("1"))){
info = "判断题的答案只能是0或1,其中0表示错误,1表示正确,请检查第"+rownum+"行";
break;
}
}

}

String c4Content = c4.getContents();
String number = "0123456789";
for (int i = 0; i < c4Content.length(); i++) {
if (number.indexOf(c4Content.charAt(i)) == -1) {
info = "默认得分只能是数字,请检查第"+rownum+"行";
break;
}
}

if(len-5<2){
info = "至少需要两个选项,请确定第"+rownum+"行至少有两个选项";
break;
}
if(info!=null&&!info.equals("")){
break;
}
}
return info;
}

//将工作表中的内容写入数据库
public String saveExcelToElement(Sheet r){
String info = checkExcel(r); //对Excel文件进行校验
String[] QuestionFlag ={"A", "B", "C", "D", "E", "F","G","H","I","J"};
//共有多少行
int row = rs.getRows();

//循环输出
Cell c0 = null;
Cell c1 = null;
Cell c2 = null;
Cell c3 = null;
Cell c4 = null;
Cell cn = null;


/**题干,类型,难易度,答案,选项A,选项B,选项C,选项D,选项E,选项F,选项G.......**/
if(info==null||info.equals("")){
for(int i=1;i<row;i++)
{
Cell[] cells = rs.getRow(i);
int len = cells.length;
c0 = rs.getCell(0, i);//题干
c1 = rs.getCell(1, i);//类型
c2 = rs.getCell(2, i);
c3 = rs.getCell(3, i);//答案("A","A,B")
c4 = rs.getCell(4,i);//默认得分
ArrayList optionList = new ArrayList();


//写入数据库
ZjQuestion question = new ZjQuestion();
question.setQuestioncontext(c0.getContents());
question.setQtype(c1.getContents());
question.setDifficulty(c2.getContents());
String answer = "";
for(int j=0;j<c3.getContents().length();j++){
answer = answer + String.valueOf(c3.getContents().charAt(i));
}
question.setAnswers(answer);
question.setDefaultgrade(new Long(c4.getContents()));
....
//第六列开始为选项,保存选项信息
for(int j=5;j<len;j++){
cn = rs.getCell(j, i);
if(cn.getContents()!=null&&!cn.getContents().equals("")){
ZjQuestionOption option = new ZjQuestionOption();
option.setOptioncontext(cn.getContents());
option.setViewflag(QuestionFlag[j-4]);
optionList.add(option);
}

}
question.setOptionList(optionList);
if(!this.getQuestionService().isHave(question))
this.getQuestionService().saveQuestion(question);

}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值