蓝墨云目前是免费且使用广泛的一个教学平台。但是普通用户目前只支持导入试题功能,却不支持把试题从蓝墨云上进行导出。
接下来,将实现导出的方法简要讲述一下:
①把蓝墨云上的试卷或测试导出
②导出的压缩包中有一个《汇总与详情》的Excel文件,这里包含了试题的全部内容。
③用C#编程读取《汇总与详情》的试题内容,生成符合试卷格式的文档。
接下来,我们就开始操作吧!
一、蓝墨云导出
步骤1:
步骤2:
步骤3:
步骤4:
二、解压
步骤1
步骤2:
步骤3:
三、通过编程希望达到的试卷输出效果(试卷样张)
四、程序界面如下:
小贴士:通过选择【显示答案】复选框,可以生成带答案的试卷。
五、程序代码如下:
using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.XWPF.UserModel;using System;using System.Collections.Generic;using System.IO;using System.Windows.Forms;namespace LmyExamExport{public partial class frmExamExport : Form{public frmExamExport(){InitializeComponent();}private void btnSelect_Click(object sender, EventArgs e){string strQQQun = "485532597";OpenFileDialog ofd = new OpenFileDialog();ofd.InitialDirectory = Application.StartupPath;ofd.Filter = "*.xlsx|*.xlsx|*.xls|*.xls|*.*|*.*";ofd.FileName = "";DialogResult dr = ofd.ShowDialog();if (dr == DialogResult.OK){txtPath.Text = ofd.FileName;}}List<RdoQuestion> lstRdoQuestion = new List<RdoQuestion>();RdoQuestion rdoQuestion;List<GapFilling> lstGapFilling = new List<GapFilling>();GapFilling gapFilling;List<TFQuestion> lstTFQuestion = new List<TFQuestion>();TFQuestion tfQuestion;/*****************写入word*************/FileStream fs2;XWPFDocument MyDoc = new XWPFDocument();//打开07(.docx)以上的版本的文档XWPFParagraph MyParagraph = null;XWPFParagraph paragraph = null;XWPFRun run = null;private string ReplaceStr(string str1,string str2){int start = 0;int end = 0;while((start=str1.IndexOf("【"))>=0&&(end= str1.IndexOf("】"))>= 0&&end>start){string tmpStr = str1.Substring(start, end - start + 1);str1 = str1.Replace(tmpStr,str2);}return str1;}private void btnExport_Click(object sender, EventArgs e){SaveFileDialog sfd = new SaveFileDialog();sfd.FileName = "";sfd.Filter = "*.docx|*.docx|*.*|*.*";DialogResult dr = sfd.ShowDialog();if(dr==DialogResult.OK){fs2 = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);}string strExt = Path.GetExtension(txtPath.Text).ToLower();var fs = File.OpenRead(txtPath.Text);HSSFWorkbook workbook1 = null;XSSFWorkbook workbook2 = null;ISheet sheet = null;switch (strExt){case ".xls":workbook1 = new HSSFWorkbook(fs);sheet = workbook1.GetSheetAt(0);break;case ".xlsx":workbook2 = new XSSFWorkbook(fs);sheet = workbook2.GetSheetAt(0);break;default:MessageBox.Show("文件格式不支持!", "提示");break;}bool flag = false;//去首行for (var j = 1; j <= sheet.LastRowNum; j++){var row = sheet.GetRow(j);if (row != null){string tx = row.GetCell(2).ToString();switch(tx){case "单选题":var RDOcell = row.GetCell(1) + "()";rdoQuestion = new RdoQuestion();rdoQuestion.Stem = RDOcell.ToString();rdoQuestion.Option.Enqueue(row.GetCell(6).ToString() + "." + row.GetCell(7).ToString());rdoQuestion.Answer = row.GetCell(4).ToString();row = sheet.GetRow(++j);while (row!= null && row.GetCell(2).ToString() == ""){rdoQuestion.Option.Enqueue(row.GetCell(6).ToString() + "." + row.GetCell(7).ToString());row = sheet.GetRow(++j);}j--;lstRdoQuestion.Add(rdoQuestion);break;case "判断题":var TFcell = row.GetCell(1);tfQuestion = new TFQuestion();tfQuestion.Stem = TFcell.ToString()+"( )";tfQuestion.Answer = row.GetCell(4).ToString();lstTFQuestion.Add(tfQuestion);break;case "填空题":var GFcell = row.GetCell(1);string repStr = GFcell.ToString();gapFilling = new GapFilling();gapFilling.Stem = GFcell.ToString();gapFilling.Answer.Enqueue(row.GetCell(4).ToString());row = sheet.GetRow(++j);while ( row != null && row.GetCell(2).ToString() == ""){gapFilling.Answer.Enqueue(row.GetCell(4).ToString());row = sheet.GetRow(++j);}j--;lstGapFilling.Add(gapFilling);break;default:MessageBox.Show(tx + "目前不支持,请联系QQ2351637");break;}}}//单选题WriteTitle("单选题(每题2分)\r\n");int i = 1;foreach (RdoQuestion rdoq in lstRdoQuestion){string str = i++.ToString() + "." + rdoq.Stem + "\r\n";WriteContent(str);while (rdoq.Option.Count > 0)WriteContent( rdoq.Option.Dequeue());if(chkAnswer.Checked){WriteContent("答案:" + rdoq.Answer);}}//判断题WriteTitle("判断题(每题1分)\r\n");i = 1;foreach(TFQuestion tfQuestion in lstTFQuestion){string str = i++.ToString() + "." + tfQuestion.Stem + "\r\n";WriteContent(str);if(chkAnswer.Checked){WriteContent("答案:" + tfQuestion.Answer);}}//填空题i = 1;WriteTitle("填空题(每空1分)\r\n");foreach(GapFilling gapFilling in lstGapFilling){string str = i++.ToString() + "." + gapFilling.Stem + "\r\n";str = ReplaceStr(str, "_________ ");WriteContent(str);if (chkAnswer.Checked){string sAns = "";foreach(string s in gapFilling.Answer){sAns += s + ",";}sAns = sAns.Substring(0, sAns.Length - 1);WriteContent("答案:"+sAns);}}MyDoc.Write(fs2);fs2.Close();MessageBox.Show("导出成功!请打开" +sfd.FileName+ "文件");}private void WriteTitle(string strTitle){paragraph = MyDoc.CreateParagraph();paragraph.Alignment = ParagraphAlignment.LEFT; //字体居中var run = paragraph.CreateRun();run.IsBold = true;run.SetText(strTitle);run.FontSize = 14;run.SetFontFamily("黑体", FontCharRange.None); //设置黑体//控制段落与其他元素的上下距离paragraph.SpacingBeforeLines = 20;//上方距离paragraph.SpacingAfterLines = 20;//下方距离}private void WriteContent(string strTitle){paragraph = MyDoc.CreateParagraph();paragraph.Alignment = ParagraphAlignment.LEFT; //字体居中var run = paragraph.CreateRun();run.IsBold = false;run.SetText(strTitle);run.FontSize = 10;run.SetFontFamily("宋体", FontCharRange.None); //设置黑体//控制段落与其他元素的上下距离paragraph.SpacingBeforeLines = 20;//上方距离paragraph.SpacingAfterLines = 20;//下方距离}}}
六、源程序下载:
蓝墨云试卷生成系统