StarkSoft题库管理系统(二)--生成word格式试卷

一、功能介绍
    1、自定义试题库管理系统目录、难易程度,题型,知识库等。
    2、试题录入。
    3、强大的试题编辑功能,并与通常应用编辑工具有共通。
    4、灵活的试卷构造功能,用户可自定义试卷标题、试题分类,试题数量、总分、试题难度系数等。
    5、人工生成试卷和自动生成试卷文档格式标准通用。能够合理使用,有效再编辑,保存,方便浏览和打印输出。
    6、题库管理,可以随时分散和集中管理题库数据。
二、菜单功能
    1、基础数据维护:试题分类设置;题型设置;难易程度;知识点库设置
    2、题库管理:试题录入
    3、试卷管理:人工生成试卷;自动生成试卷;试卷库管理

导出试卷到WORD

#region 创建word试卷(直接导出为word)
        /// <summary>
        /// 创建word试卷
        /// </summary>
        /// <param name="PathName"></param>
        public void CreateWordFile(string PathName)
        {
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                object filename = PathName;  //文件保存路径
                //1.创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //2.添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[入职考试试卷]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
                WordApp.Selection.ParagraphFormat.LineSpacing = 8f;//设置文档的行间距
                //3.移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落
                #region 标题
                WordApp.Selection.Font.Size = 20;
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
                WordApp.Selection.Font.Bold = 1;    // 黑体
                WordApp.Selection.TypeText("xxxx入职考试");
                WordApp.Selection.TypeParagraph();
                //WordApp.Selection.TypeParagraph();
                #endregion

                //设置内容部分格式
                WordApp.Selection.Font.Size = 12;
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; // 居中
                WordApp.Selection.Font.Bold = 0;    // 黑体

                string sql = @"select * from base_tx";
                DataTable dt = DataBaseAccess.GetDataTable(sql);
                for (int i = 0; i < dt.Rows.Count;i++ )
                {
                    //写标题
                    WordApp.Selection.TypeText((i+1)+""+dt.Rows[i]["name"].ToString()+"(每题3分)" + "

");                   

                    int txid = Convert.ToInt32(dt.Rows[i]["id"]);//题型ID
                    DataRow[] rows = dtxz.Select("xztxid=" + txid, "xztxid asc");
                    DataTable dttemp = dtxz.Clone();
                    dttemp.Clear();
                    foreach (DataRow dr2 in rows)
                    {
                        dttemp.Rows.Add(dr2.ItemArray);
                    }

                    for (int k = 0; k < dttemp.Rows.Count; k++)
                    {
                        string sqlstr = @"select * from base_st where id=" + dttemp.Rows[k]["xzstid"] + "";
                        DataTable dtst = DataBaseAccess.GetDataTable(sqlstr);

                        //获取题目内容
                        byte[] bWrite = (byte[])dtst.Rows[0]["contents"];//从数据库中读出数据
                        string s = System.Text.Encoding.UTF8.GetString(bWrite, 0, bWrite.Length);
                        //开始写内容
                        WordApp.Selection.TypeText(s);
                    }
                }
               
                WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //文件保存
                WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);                MessageBoxEx.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBoxEx.Show("导出失败!" + "
" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

附完整源代码下载:http://www.51aspx.com/Code/StarkSoftExam

原文地址:https://www.cnblogs.com/starksoft/p/3940361.html