蓝墨云功能扩展之试卷导出WORD文档

蓝墨云目前是免费且使用广泛的一个教学平台。但是普通用户目前只支持导入试题功能,却不支持把试题从蓝墨云上进行导出。
接下来,将实现导出的方法简要讲述一下:
①把蓝墨云上的试卷或测试导出
②导出的压缩包中有一个《汇总与详情》的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;//下方距离
        }
    }
}


六、源程序下载:

蓝墨云试卷生成系统


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒茗清雾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值