⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendpaper.cs

📁 程序设计语言上机考试系统
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using Word;

namespace WindowsApplication1.KaowuManage
{
    public partial class SendPaper : Form
    {
        SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
        baseclass.BaseOperate boperate = new baseclass.BaseOperate();
        decimal choosemark = 0, pandunmark = 0,progmark=0; //选择题、判断题、程序题分值
        public SendPaper()
        {
            InitializeComponent();
        }
        #region 发放试卷
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要发放试卷?", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
            {
                SqlCommand cmd = new SqlCommand("select * from shiti ", cn);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                if (null == cmd.ExecuteScalar())
                    MessageBox.Show("试题表中没有信息,请先生成试卷", "提示");
                else
                {
                    cmd = new SqlCommand("select * from paper ", cn);
                    if (null != cmd.ExecuteScalar())
                    {
                        MessageBox.Show("试卷表中已经存在试题信息,请先清空试卷信息表", "提示");
                    }
                    else
                    {
                        boperate.Send();
                        MessageBox.Show("试卷发放成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (cn.State == ConnectionState.Open)
                    cn.Close();
                DataSet myds= boperate.getds("select idshijuan as 试卷编号,s_number as 学号 from paper", "paper");
               
                dataGridView1.DataSource = myds.Tables[0];
                if (myds.Tables[0].Rows.Count <= 0)
                {
                    dataGridView1.AllowUserToAddRows = true;
                }
                else
                {
                    btnLook.Enabled = true;
                    btnOutAll.Enabled = true;
                }
            }
        }
        #endregion

        #region 查看一套试卷
        private void btnLook_Click(object sender, EventArgs e)
        {
            string[] snumber = new string[1];
            snumber[0] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            OutTWord(snumber);
        }
        #endregion

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
            dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray;
            dataGridView1.CurrentRow.Selected = true;
        }
        #region 查看多套试卷
        private void tSMItemLook_Click(object sender, EventArgs e)
        {
            //将选中的行的学号存在数组snumber中
            string[] snumber = new string[dataGridView1.SelectedRows.Count];
            for (int i = 0, j = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected)
                {
                    snumber[j] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    j++;
                }
            }
            //导出试卷
            OutTWord(snumber);
        }
        #endregion

        #region 将试卷导出到word
        public  void OutTWord(string []snumber)
        {
            try
            {
                string savepath = ""; //存放要保存目标文件的路径名
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    savepath = folderBrowserDialog1.SelectedPath;
                }
                if (savepath != "")
                {
                    for (int j = 0; j < snumber.Length; j++)
                    {   
                        //循环输出所有选中学生的试卷
                        Object Nothing = Missing.Value;
                        string name = snumber[j] + ".doc";
                        //保存的文件名
                        object filename = savepath + "/" + name;
                        Word.Application wApp = new Word.Application();
                        //如果不是导出多分试卷则直接显示
                        if(snumber.Length ==1)
                            wApp.Visible = true;
                        Document WordDoc = wApp.Documents.Add(ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing);
                        //设置右对齐
                        wApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        string[][] id = boperate.Extract(snumber[j]);
                        WordDoc.Paragraphs.First.Range.Text = "[共有:" + (id[0].Length + id[1].Length + id[2].Length) + "道题]";
                        
                        //设置标题为居中对齐,字号为20并加粗
                        wApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        wApp.Selection.Font.Size = 20;
                        wApp.Selection.Font.Bold =1 ;
                        wApp.Selection.TypeText("C语言考试试题\n");
                        
                        //正文为居左对齐,字号11
                        wApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        wApp.Selection.Font.Size = 11;
                        wApp.Selection.TypeText("一、选择题(共有" + id[0].Length + "道小题,每小题" + choosemark + "分)\n");
                        //输出选择题
                        for (int i = 0; i < id[0].Length; i++)
                        {
                            DataSet myds =
                                boperate.getds("select * from choose where id ='" + id[0][i] + "'", "choose");
                            foreach (DataRow row in myds.Tables[0].Rows)
                            {
                                wApp.Selection.TypeText((i + 1).ToString() + "、");
                                wApp.Selection.TypeText(row["title"].ToString() + "  (  )");
                                wApp.Selection.TypeText("\n");
                                wApp.Selection.TypeText("\t" + "A、" + row["selectA"].ToString() + "\n");
                                wApp.Selection.TypeText("\t" + "B、" + row["selectB"].ToString() + "\n");
                                wApp.Selection.TypeText("\t" + "C、" + row["selectC"].ToString() + "\n");
                                wApp.Selection.TypeText("\t" + "D、" + row["selectD"].ToString() + "\n");
                            }
                        }

                        wApp.Selection.TypeText("二、判断题(共有" + id[1].Length + "道小题,每小题" + pandunmark + "分)\n");
                        for (int i = 0; i < id[1].Length; i++)
                        {
                            DataSet myds =
                                boperate.getds("select * from panduan where id ='" + id[1][i] + "'", "panduan");
                            foreach (DataRow row in myds.Tables[0].Rows)
                            {
                                wApp.Selection.TypeText((i + 1).ToString() + "、");
                                wApp.Selection.TypeText(row["title"].ToString() + "    (  )");
                                wApp.Selection.TypeText("\n");
                            }
                        }
                        wApp.Selection.TypeText("三、程序设计题(共有" + id[2].Length + "道小题,每小题" + progmark + "分)\n");
                        for (int i = 0; i < id[2].Length; i++)
                        {
                            DataSet myds =
                                boperate.getds("select * from program where id ='" + id[2][i] + "'", "program");
                            foreach (DataRow row in myds.Tables[0].Rows)
                            {
                                //填写信息
                                wApp.Selection.TypeText((i + 1).ToString() + "、");
                                wApp.Selection.TypeText(row["title"].ToString());
                                wApp.Selection.TypeText("\n");
                            }
                        }
                        //保存
                        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);
                        if (snumber.Length != 1)
                        {
                            WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭
                            wApp.Quit(ref Nothing, ref Nothing, ref Nothing); //退出
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 导出所有试卷
        private void btnOutAll_Click(object sender, EventArgs e)
        {
            DataSet myds = boperate.getds("select distinct s_number from paper", "paper");
            string [] snumber = new string[myds.Tables[0].Rows.Count];
            int i = 0;
            foreach (DataRow row in myds.Tables[0].Rows)
            {
                snumber[i] = row["s_number"].ToString();
                i++;
            }
            OutTWord(snumber);
            MessageBox.Show("试卷导出完毕。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        private void SendPaper_Load(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count ==0)
            {
                btnLook.Enabled = false;
                btnOutAll.Enabled = false;
            }
        }


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -