|
@@ -0,0 +1,173 @@
|
|
|
|
+package cn.com.qmth.export;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+
|
|
|
|
+public class CreatePaperByExcel {
|
|
|
|
+ private static String paperSuff = "(211)";
|
|
|
|
+ private static int maxqc = 100;
|
|
|
|
+ private static String excelDir = "D:\\kd_excel_export\\";
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ try {
|
|
|
|
+ File excelFolder = new File(excelDir);
|
|
|
|
+ if (excelFolder.exists()) {
|
|
|
|
+ FileUtil.clearDirectory(excelDir);
|
|
|
|
+ } else {
|
|
|
|
+ excelFolder.mkdir();
|
|
|
|
+ }
|
|
|
|
+ Course c=new Course();
|
|
|
|
+ c.setName("中外建筑史");
|
|
|
|
+ c.setCode("10454");
|
|
|
|
+ List<KdQuestion> qs=getQus(read());
|
|
|
|
+ List<KdQuestion> single = new ArrayList<>();
|
|
|
|
+ List<KdQuestion> muti = new ArrayList<>();
|
|
|
|
+ List<KdQuestion> boo = new ArrayList<>();
|
|
|
|
+ for (KdQuestion q : qs) {
|
|
|
|
+ if (q.getStructType() == 1) {
|
|
|
|
+ single.add(q);
|
|
|
|
+ } else if (q.getStructType() == 2) {
|
|
|
|
+ muti.add(q);
|
|
|
|
+ } else if (q.getStructType() == 3) {
|
|
|
|
+ boo.add(q);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ createPapers(single, c, 1);
|
|
|
|
+ createPapers(muti, c, 2);
|
|
|
|
+ createPapers(boo, c, 3);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private static void createPapers(List<KdQuestion> qs, Course c, int structType) throws IOException {
|
|
|
|
+ if (qs == null || qs.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (qs.size() <= maxqc) {
|
|
|
|
+ createPaper(qs, c, structType, 1);
|
|
|
|
+ } else {
|
|
|
|
+ int size = qs.size();
|
|
|
|
+ int len = maxqc;
|
|
|
|
+ int count = (size + len - 1) / len;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
|
+ List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
|
|
|
|
+ createPaper(subList, c, structType, i + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void createPaper(List<KdQuestion> qs, Course c, int structType, int indx) throws IOException {
|
|
|
|
+ if (qs.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String detailName="";
|
|
|
|
+ if (structType == 1) {
|
|
|
|
+ detailName="单选题";
|
|
|
|
+ } else if (structType == 2) {
|
|
|
|
+ detailName="多选题";
|
|
|
|
+ } else if (structType == 3) {
|
|
|
|
+ detailName="判断题";
|
|
|
|
+ }
|
|
|
|
+ KdPaper paper = new KdPaper();
|
|
|
|
+ paper.setTotalScore((double) qs.size());
|
|
|
|
+ paper.setName(c.getName()+paperSuff+detailName+"_"+indx);
|
|
|
|
+ paper.setCourseCode(c.getCode());
|
|
|
|
+ List<KdDetail> des = new ArrayList<>();
|
|
|
|
+ KdDetail d = new KdDetail();
|
|
|
|
+ d.setName(detailName);
|
|
|
|
+ d.setNumber(1);
|
|
|
|
+ d.setQuestionCount(qs.size());
|
|
|
|
+ d.setTotalScore((double) qs.size());
|
|
|
|
+ d.setQuestions(qs);
|
|
|
|
+ des.add(d);
|
|
|
|
+ paper.setDetails(des);
|
|
|
|
+ paper.setDetailCount(1);
|
|
|
|
+ File paperdir = new File(excelDir + c.getCode() + "\\paper"+structType+"_"+indx+"\\");
|
|
|
|
+ paperdir.mkdirs();
|
|
|
|
+ FileUtil.writeFile(paperdir.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
|
|
|
|
+ }
|
|
|
|
+ public static List<KdQuestion> getQus(List<Question> qs) {
|
|
|
|
+ List<KdQuestion> kqs = new ArrayList<>();
|
|
|
|
+ for (Question q : qs) {
|
|
|
|
+ KdQuestion kq = new KdQuestion();
|
|
|
|
+ kqs.add(kq);
|
|
|
|
+ kq.setBody(q.getBody());
|
|
|
|
+ AnswerOb ao = JSONObject.parseObject(q.getAnswer(), AnswerOb.class);
|
|
|
|
+ if ("判断题".equals(q.getType().trim())) {
|
|
|
|
+ kq.setStructType(3);
|
|
|
|
+ if("1".equals(ao.getAnswer().get(0).trim())) {
|
|
|
|
+ kq.setAnswer("true");
|
|
|
|
+ }else {
|
|
|
|
+ kq.setAnswer("false");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else if ("单项选择题".equals(q.getType().trim()) || "多项选择题".equals(q.getType().trim())) {
|
|
|
|
+ int index = 0;
|
|
|
|
+ List<KdQuesOption> ops = new ArrayList<>();
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (String a : ao.getSubArea()) {
|
|
|
|
+ index++;
|
|
|
|
+ KdQuesOption op = new KdQuesOption();
|
|
|
|
+ op.setNumber(index);
|
|
|
|
+ op.setBody(a);
|
|
|
|
+ if (ao.getAnswer().contains((index-1)+"")) {
|
|
|
|
+ op.setSelect(true);
|
|
|
|
+ sb.append(index).append(",");
|
|
|
|
+ } else {
|
|
|
|
+ op.setSelect(false);
|
|
|
|
+ }
|
|
|
|
+ ops.add(op);
|
|
|
|
+ }
|
|
|
|
+ kq.setOptions(ops);
|
|
|
|
+ if (sb.length() > 0) {
|
|
|
|
+ sb.deleteCharAt(sb.length() - 1);
|
|
|
|
+ }
|
|
|
|
+ if (sb.indexOf(",") > 0) {
|
|
|
|
+ kq.setStructType(2);
|
|
|
|
+ } else {
|
|
|
|
+ kq.setStructType(1);
|
|
|
|
+ }
|
|
|
|
+ kq.setAnswer(sb.toString());
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("错误");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return kqs;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Question> read() throws IOException {
|
|
|
|
+ List<Question> list = new ArrayList<>();
|
|
|
|
+ XSSFWorkbook wb = null;
|
|
|
|
+ try {
|
|
|
|
+ wb = new XSSFWorkbook("d:\\p.xlsx");
|
|
|
|
+ XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
+ int rows = sheet.getLastRowNum();
|
|
|
|
+ for (int i = 1; i <= rows; i++) {
|
|
|
|
+ Question q = new Question();
|
|
|
|
+ XSSFRow row = sheet.getRow(i);
|
|
|
|
+ String b = row.getCell(1).getStringCellValue().trim();
|
|
|
|
+ String a = row.getCell(2).getStringCellValue().trim();
|
|
|
|
+ String t = row.getCell(4).getStringCellValue().trim();
|
|
|
|
+ q.setBody(b);
|
|
|
|
+ q.setAnswer(a);
|
|
|
|
+ q.setType(t);
|
|
|
|
+ list.add(q);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ if (wb != null) {
|
|
|
|
+ wb.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+}
|