excel练习题
js读取导入的名称到对应的位置显示
$(function() {
$('#myFile').on('change', function() {
var originName = $(this).val();
var fileName = originName.substring(originName.lastindexof('\\')+1);
fileName = '<span class="am-badge">' + fileName + '</span>';
$('#file-list').html(fileName);
});
});
function importexcel() {
var myFile = $("#myFile").val();
if (myFile.length <= 0) {
dialogFun("批量导入试题", "请选择导入内容", 0);
return false;
}
$("#importP").submit();
}
html代码实现
<p class="am-u-sm-8 am-u-md-6">
<p class="am-form-group am-form-file">
<button class="am-btn am-btn-danger am-btn-sm" type="button">
<i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
<input id="myFile" type="file" value="" accept=".xls" name="myFile"/>
</p>
<p id="file-list"></p>
</p>
<p class="am-hide-sm-only am-u-md-4"></p>
<form action="/admin/quest/importExcel" method="post" id="importP" enctype="multipart/form-data" class="am-form">
</form>
后台代码实现
/**
* 批量导入试题
* /admin/quest/importExcel
* @param request
* @param file
* @return
*/
@RequestMAPPing("/quest/importExcel")
public String importExcel(HttpServletRequest request, @RequestParam("myFile") MultipartFile file) {
try {
Long companyId = SingletonLoginUtils.getCurrentCompanyId(request);
logger.info("myFile:" + file.getName());
examQuestionService.updateImportQuestionExcel(file, companyId);
request.setattribute("msg", "操作成功");
} catch (BaseException e) {
logger.ERROR("QuestionAction.importExcel", e);
request.setAttribute("msg", e.getmessage());
return msgError;
} catch (RecordFormatException e) {
logger.error("QuestionAction.importExcel", e);
request.setAttribute("msg", "图片格式化失败,请勿导入图片");
return msgError;
}
catch (Exception e) {
logger.error("AdminQuestionAction.randomQuestion", e);
request.setAttribute("msg", e.getMessage());
return msgError;
}
return "/common/success";
}
方法实现
/**
* 批量导入试题
* @param file 文件
* @param companyId 公司ID
* @return
* @throws Exception
*/
public String updateImportQuestionExcel(MultipartFile file, Long companyId) throws Exception {
// datalist拼装List<String[]> deadliest,
HSSFWorkbook wookbook = new HSSFWorkbook(file.getInputStream());
HSSFSheet sheet = wookbook.getSheetAt(0);
// 指的行数,一共有多少行+
int rows = sheet.getLastrownum();
calendar calendar = Calendar.getinstance();
List<Nxbsubject> subjectR = nxbSubjectService.getSubjectListByType(SubjectType.EXAM.toString());
for (int i = 1; i <= rows; i++) {
// 读取左上端单元格
HSSFRow row = sheet.getRow(i);
// 行不为空
if (row != null) {
// 获取到Excel文件中的所有的列
int maxcell = row.getLastCellNum();
// **读取cell**
String content = getCellValue(row.getCell((short) 0));// 试题内容
String subjectId = getCellValue(row.getCell((short) 1));// 专业ID
String pointId = trimZero(getCellValue(row.getCell((short) 2)));// 考点
String isAsr = getCellValue(row.getCell((short) 3));// 正确答案
String type = getCellValue(row.getCell((short) 4));// 题型
String level = trimZero(getCellValue(row.getCell((short) 5)));// 试题难度
String analyze = getCellValue(row.getCell((short) 6));// 解析
String optionA = getCellValue(row.getCell((short) 7)); // A
String optionB = getCellValue(row.getCell((short) 8)); // B
String optionC = getCellValue(row.getCell((short) 9)); // C
String optionD = getCellValue(row.getCell((short) 10)); // D
String optionE = getCellValue(row.getCell((short) 11)); // E
String optionF = getCellValue(row.getCell((short) 12)); // F
String optionG = getCellValue(row.getCell((short) 13)); // D
if(StringUtils.isempty(content)&&
StringUtils.isEmpty(subjectId)&&
StringUtils.isEmpty(isAsr)&&
StringUtils.isEmpty(type)&&
StringUtils.isEmpty(level)&&
StringUtils.isEmpty(analyze)){
break;
}
// 试题内容,专业,正确答案,试题类型,试题难度
if (StringUtils.isEmpty(content) || StringUtils.isEmpty(subjectId) || StringUtils.isEmpty(isAsr) || StringUtils.isEmpty(type) || StringUtils.isEmpty(level)) {
throw new BaseException(
"第" + i + "行,试题内容为<" + content + ">的那条数据数据不能为空(试题内容,专业,正确答案,试题类型,试题难度)");
}
logger.info("试题类型:" + type);
switch (type) {
case "单选":
type = "1";
break;
case "多选":
type = "2";
break;
case "判断":
type = "3";
break;
case "不定项":
type = "5";
break;
default:
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据试题类型不正确(试题类型只能输入单选,多选,判断,不定项)");
}
// 专业ID必须是大于0的正整数
if (!StringUtils.isnumber(subjectId)||Long.parseLong(subjectId)<=0) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据专业ID必须是大于0的正整数");
}
//该 专业ID必须是该公司所有
NxbQuerySubject nxbQuerySubject=new NxbQuerySubject();
nxbQuerySubject.setCompanyId(companyId);
nxbQuerySubject.setSubjectId(Long.parseLong(subjectId));
List<NxbSubject> subjectList = nxbSubjectDao.getSubjectList(nxbQuerySubject);
if (subjectList==null||subjectList.size()==0) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据专业ID必须是该分公司所拥有的");
}
// // 考点必须是大于0的正整数
// if (!StringUtils.isNumber(pointId)||Long.parseLong(pointId)<=0) {
// throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据考点ID必须是大于0的正整数");
// }
// 试题难度
logger.info("试题难度:" + level);
switch (level) {
case "一级":
level = "1";
break;
case "二级":
level = "2";
break;
case "三级":
level = "3";
break;
default:
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据的试题难度必须是一级、二级、三级其中的一个");
}
int typeInt = ConvertUtils.objectToInt(type);
// 如果为判断题最多2个选项
if (typeInt == 3) {
if (StringUtils.isEmpty(optionA) || StringUtils.isEmpty(optionB)) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据为判断题,选项A或选项B不能为空");
}
if (StringUtils.isNotEmpty(optionD) || StringUtils.isNotEmpty(optionE)
|| StringUtils.isNotEmpty(optionF) || StringUtils.isNotEmpty(optionG)) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据为判断题,选项D,E,F,G必须为空");
}
}
// 如果不是判断题,选项必须大于等于4个小于等于7个选项
if (typeInt != 3) {
if (StringUtils.isEmpty(optionA) || StringUtils.isEmpty(optionB) ) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据为选择题,选项A,B不能为空");
}
}
// 如果为多选题正确答案必须在两个以上
if (typeInt == 2 && isAsr.trim().length() < 2) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据的为多选题,正确答案必须在两个以上(例:AB)");
}
// 如果为单选题或者判断题答案只能有一个
if (typeInt == 1 || typeInt == 3) {
if (isAsr.trim().length() > 1) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据的正确答案只能有一个(例:A)");
}
}
// 选项不能超过7个字符
if (isAsr.trim().length() > 7) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据正确答案不能超过7个字符(例AB)");
}
// 验证正确答案不能输入其他字符
char[] asr = isAsr.toString().trim().toCharArray();
String asrStr = "";
for (int y = 0; y < asr.length; y++) {
asrStr += asr[y] + ",";
if ("ABCDEFG".indexOf(String.valueOf(asr[y])) == -1) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据正确答案输入字符格式不正确(例AB)");
}
}
isAsr = asrStr.substring(0, asrStr.length() - 1);
Question question = new Question();
question.setStatus(1);
// 试题类型
int qstType = typeInt;
question.setQstType(qstType);
// 验证项目ID
Long subjectIdLong = Long.valueOf(subjectId.trim());
for (int x = 0; x < subjectR.size(); x++) {
if (subjectR.get(x).getSubjectId().longValue() == subjectIdLong.longValue()) {
question.setSubjectId(subjectIdLong);
break;
}
}
// 如果输入的专业不匹配
if (question.getSubjectId() == null || question.getSubjectId().intValue() <= 0) {
throw new BaseException("第" + i + "行,试题内容为<" + content + ">的那条数据的专业id不匹配");
}
// 验证考点
if(StringUtils.isNotEmpty(pointId)){
Long pointIdLong = Long.valueOf(pointId.trim());
ExamPoint point = new ExamPoint();
point.setSubjectId(subjectIdLong);
point.setId(pointIdLong);
List<ExamPoint> pointList = pointDao.getPointList(point);
if (!pointList.isEmpty()) {
question.setPointId(pointIdLong);
}
}
//调整答案顺序
if(typeInt==2){
String[] chars = isAsr.split(",");
arrays.sort(chars);
if(ObjectUtils.isnotnull(chars)&&chars.length>1){
stringbuilder isAsrs=new StringBuilder();
for (String s:chars){
isAsrs.append(s);
isAsrs.append(",");
}
isAsr=isAsrs.substring(0,isAsrs.length()-1);
}
}
question.setLevel(ConvertUtils.objectToInt(level));
question.setQstContent(content);
question.setIsAsr(isAsr);
question.setQstAnalyze(analyze);
question.setAddTime(new Date());
question.setAuthor("admin");
question.setCompanyId(companyId); // 公司ID
questionDao.addOneQuestion(question);
int AASC = 64;
List<String> str = new ArrayList<String>();
// 把选项的值放入list中
str.add(optionA);
str.add(optionB);
str.add(optionC);
str.add(optionD);
str.add(optionE);
str.add(optionF);
str.add(optionG);
List<QuestionOption> optionList = new ArrayList<>();
for (int k = 0; k < str.size(); k++) {
// 如果选项为空字符串则不添加该选项
if (!"".equals(str.get(k).trim())) {
QuestionOption option = new QuestionOption();
option.setAddTime(new Date());
option.setOptAnswer(question.getIsAsr());
option.setOptContent(str.get(k).trim());
option.setQstId(question.getId());
char data[] = { backchar(AASC += 1) };
option.setOptorder(String.valueOf(data));
optionList.add(option);
}
}
Map<String, List<QuestionOption>> map = new HashMap<>();
map.put("optionList", optionList);
optionDao.addOptionBATch(map);
}
}
return null;
}
/**
* 获得Hsscell内容
*
* @param cell
* @return
*/
public String getCellValue(HSSFCell cell) {
String value = "";
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = cell.getNumericCellValue() + "";
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue().trim();
break;
default:
value = "";
break;
}
}
return value.trim();
}
String trimZero(String str) {
if (StringUtils.isNotEmpty(str)) {
return str.replace(".0", "");
}
return str;
}
工具类
public abstract class ConvertUtils {
private static final decimalformat simpleFormat = new DecimalFormat("####");
public static final boolean objectToBoolean(Object o) {
return o != null?Boolean.valueOf(o.toString()).booleanValue():false;
}
public static final int objectToInt(Object o) {
if(o instanceof Number) {
return ((Number)o).intValue();
} else {
try {
if(o == null) {
return -1;
} else {
bigdecimal bigDecimal = new BigDecimal(o.toString());
bigDecimal.intValue();
return bigDecimal.intValue();
}
} catch (Exception var2) {
var2.printstacktrace();
return -1;
}
}
}
}
试题模板如下所示:
项目中所用的依赖为:
<!-- poi excel -->
<dependency>
<groupId>poi</groupId>
<artifactId>poi-2.5.1-final</artifactId>
<version>20040804</version>
</dependency>
<!-- poi excel -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.3</version>
</dependency>
数据库插入
<insert id="addOptionBatch" parameterType="java.util.HashMap">
INSERT INTO nxb_exam_options (
<include refid="exam_options_columns" />
) VALUES
<foreach collection="optionList" index="index" item="item"
separator=",">
(#{item.id},#{item.qstId},#{item.optContent},
#{item.optOrder}, #{item.optAnswer},#{item.addTime})
</foreach>
</insert>
相关阅读
补全下列代码,判断给定整数的二进制表示,从右边数第三位是不是1Bool IsThirdBitOne(int number){Return ;} C#中值类型和引
概述 最近想要通过Date类的getXXX方法自定义格式化时间字符串,结果发现结果和预想的不同,主要问题在getMonth和getDay两个接口上。
Socket的来龙去脉 下面的分析主要是参阅了计算机网络(谢希仁第7版)进行总结的,从系统调用—>应用编程接口API—>套接字接口来分析So
回头看看, 我进入Java 领域已经快15个年头了, 虽然学的也一般, 但是分享下我的心得,估计也能帮大家少走点弯路。[入门]我在2001年之
问题描述:给出4个[0,13]的数值,经过任意加减乘除后得到24.思路: 假设四个数为a1,a2,a3,a4,其中四个符号为t1,t2,t3,t4。 我们先计算