在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 自动出题并生成excel表格

自动出题并生成excel表格

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:8.50M
  • 下载次数:66
  • 浏览次数:448
  • 发布时间:2020-03-16
  • 实例类别:C#语言基础
  • 发 布 人:鹰本孤独
  • 文件格式:.rar
  • 所需积分:2

实例介绍

【实例简介】

【实例截图】

from clipboard


生成的excel如下:

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
//using Spire.Xls;

namespace Excel_data
{
    
    public partial class Form1 : Form
    {
        public static string name;
        public static string comB1;
        public static string comB2;
        public CCExcel excel;
        public Form1()
        {
            
            InitializeComponent();
           
        }

        private void Form1Closing_Click(object sender, EventArgs e)
        {
            GC.Collect();
        }

        private void butRun_Click(object sender, EventArgs e)
        {
            this.Save2Excel();
            
        }

        private void Save2Excel()
        {
            //string file = "F:\\11\\ck.xlsx";
            //string path = "F:\\11\\excel\\";
            string time = DateTime.Now.ToString("yyyyMMdd");
            string file = System.Windows.Forms.Application.StartupPath   "\\ck.xlsx";
            string path = System.Windows.Forms.Application.StartupPath   "\\excel\\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string saveName1 = path   "_"   DateTime.Now.ToString("yyyyMMddHHmmss")   ".xlsx";
            excel = new CCExcel(file,saveName1);
            excel.Exceldata();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Form1.name = textBox1.Text.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Text = 11.ToString();
            comboBox2.Text = 100.ToString();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form1.comB1 = comboBox1.Text.ToString();
    }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form1.comB2 = comboBox2.Text.ToString();
        }
    }

    public class CCExcel
    {
        public Excel.Application appExcel;
        public Excel.Workbooks wbs;
        public Excel.Workbook wb;
        public Excel.Worksheets wss;
        public Excel.Worksheet ws;
        private string fileName;
        private string saveName;

        public CCExcel(string fileName,string saveName)
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
            this.fileName = fileName;
            this.saveName = saveName;
        }

        public void Exceldata()
        {
            Create();
            //Open(fileName);
            Data2Excel();
            Save(saveName);
            //Save(wb,saveName);
            //appExcel.ActiveWorkbook.SaveCopyAs(fileName);  //判断当前激活的表,并保存这个表。否则,保存时会弹出“是否保存Sheet1.xlsx”的对话框
            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            wbs.Close();
            appExcel.Quit();
            wb = null;
            wbs = null;
            appExcel = null;

            GC.Collect();
        }

        private void Create()//创建一个Excel对象
        {
            appExcel = new Excel.Application();
            wbs = appExcel.Workbooks;
            wb = wbs.Add(true);
            //ws = (Excel.Worksheet)wb.ActiveSheet;//这是一个只读sheets集合
            //Excel.Worksheet worksheet = wb.ActiveSheet as Excel.Worksheet;//这也是一个只读sheets集合
            //Excel.Worksheet ws = (Worksheet)wb.Worksheets[1];//创建工作页sheet单页
            ws = wb.Worksheets[1] as Worksheet;  //第一个sheet页
            ws.Name = "ck";  //这里修改sheet名称
         }

        public void Open(string fileName)
        {
            appExcel = new Excel.Application();
            wbs = appExcel.Workbooks;
            wb = wbs.Add(fileName);
            wb = wbs.Open(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
                Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
                Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
        }

        private void Data2Excel()
        {
            //ws.Cells[4, 1] = 11;
            //ws.Cells[4, 2] = 22;
            //ws.Cells[4, 3] = 33;
            //ws.Cells[4, 4] = 44;
            //ws.Cells[4, 5] = 55;
            ((Excel.Range)ws.Rows["1:34", System.Type.Missing]).RowHeight = 21; //行高
            
            ((Excel.Range)ws.Columns["A:C", System.Type.Missing]).ColumnWidth = 27.38;  //列宽
          
            ws.Cells[1, 1] = Form1.name;
            ws.Cells[1, 2] = "错       题";
            ws.Cells[1, 3] = "用时       分       秒";
            #region  //生成第34行第一列的算式
            Random rNumber3 = new Random();//实例化一个随机数对象  int.Parse("字符串")
            rNumber3.Next(10, int.Parse(Form1.comB1));//产生一个1到100之间的任意一个数
            int o = rNumber3.Next(10, int.Parse(Form1.comB1));

            Random rNumber4 = new Random();//实例化一个随机数对象
            rNumber4.Next(5, int.Parse(Form1.comB2));//产生一个1到100之间的任意一个数
            int p = rNumber4.Next(5, int.Parse(Form1.comB2));

            string[] s2 = { " ", "-"};//字符列表
            Random rand5 = new Random();//实例化rand
            string a1 = s2[rand5.Next(0, s2.Length)];//label.text=数组s1[随机数]

            if (a1 == "-" && (o - p) < 0)
            {
                int t1;
                t1 = o;
                o = p;
                p = t1;

            }

            if (a1 == " " && (o   p) >100)
            {
               if(o>p||o==p)
                {
                    o = (o - (o   p) / 2);
                }
               else
                {
                    p = (p - (o   p) / 2);
                }

            }
            //if(a1=="x")
            //{
            //    if(o>10&&o%9!=0)
            //    {
            //        o = (o % 9);
            //    }
            //    else
            //    {
            //        o = (o   p) % 9;
            //    }
            //    if(p>10&&p%9!=0)
            //    {
            //        p = (p % 9);
            //    }
            //    else
            //    {
            //        p = (p   o 12) % 9;
            //    }
            //}
            //#region //乘法
            //if (a1 == "x")
            //{
            //    if (o > 10 && (o % 9) != 0 && (o % 9) != 1)
            //    {
            //        o = (o % 9);
            //    }
            //    else if ((o % 9) == 0 && (o % 9) != 1)
            //    {
            //        o = ((o   p   7) % 9);
            //    }
            //    else
            //    {
            //        o = (o   p ) % 9 2;
            //    }
            //    if (p > 10 && (p % 9) != 0 && (p % 9) != 1)
            //    {
            //        p = (p % 9);
            //    }
            //    else if ((p % 9 == 0) && (p % 9) != 1)
            //    {
            //        p = (p   o   4) % 9;
            //    }
            //    else
            //    {
            //        p = (p   o 13 ) % 9 1;
            //    }
            //}
            //#endregion

           // ws.Cells[34, 1] = o   a1   p   "=";
            #endregion
            int i,j,m;
            for(i=2;i<34;i  )
            {
               
                

                for (j=1;j<4;j  )
                {
                    for(m=0;m<30;m  )
                    {

                  
                    Random rNumber = new Random();//实例化一个随机数对象
                    rNumber.Next(10, int.Parse(Form1.comB1));//产生一个1到100之间的任意一个数
                    int k = rNumber.Next(10, int.Parse(Form1.comB1));

                    Random rNumber1 = new Random();//实例化一个随机数对象
                    rNumber1.Next(5, int.Parse(Form1.comB2));//产生一个1到10之间的任意一个数
                   int h = rNumber.Next(5, int.Parse(Form1.comB2));

                    string[] s1 = { " ", "-"};//字符列表
                    Random rand = new Random();//实例化rand
                    string a = s1[rand.Next(0, s1.Length)];//label.text=数组s1[随机数]
                    if(a=="-"&&(k-h)<0)
                    {
                            int t;
                            t = k;
                            k = h;
                            h = t;

                        }
                        if (a == " " && (k   h) > 100)
                        {
                            if (k > h || k == h)
                            {
                                k = (k - (k   h) / 2);
                            }
                            else
                            {
                                h = (h - (k  h) / 2);
                            }

                        }
                        //#region  //乘法
                        //if (a == "x")
                        //{
                        //    if (k > 10&& (k % 9)!=0&& (k % 9) != 1)
                        //    {
                        //        k = (k % 9);
                        //    }
                        //    else if ((k % 9)== 0 && (k % 9) != 1)
                        //    {
                        //        k = ((k   h 7) % 9);
                        //    }
                        //    else
                        //    {
                        //        k = (k   h 41) % 9;
                        //    }
                        //    if (h > 10&&(h%9)!=0&&(h%9)!=1)
                        //    {
                        //        h = (h % 9);
                        //    }
                        //    else if((h%9==0)&&(h%9)!=1)
                        //    {
                        //        h = (h   k 4) % 9;
                        //    }
                        //    else
                        //    {
                        //        h = (h   k 13) % 9 1;
                        //    }
                        //}
                        //#endregion

                       ws.Cells[i, j] = k a h "=";
                       
                       // ws.Rows.Height(1, 20);//行高
                       // ws.Columns.Height(1, 20);//列宽
                       // ((Excel.Range)ws.Rows[i ":" i, System.Type.Missing]).RowHeight = 21; //行高
                    }
                }
               
            }
            #region //给Excel添加边框
            int Rowcount = ws.UsedRange.CurrentRegion.Rows.Count;
            Range range = ws.get_Range(ws.Cells[1, 1], ws.Cells[Rowcount, 3]);//11指的是列数
            range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            #endregion
        }

        private void Save(string saveName)//文档另存
        //private bool Save(Microsoft.Office.Interop.Excel.Workbook wb,object saveName)
        {
            try
            {
                wb.SaveAs(saveName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,
                Type.Missing,Type.Missing);
            }
            catch (Exception ex)
            {
                //return false;
                throw ex;
            }
            //return true;
        }
    }
}

实例下载地址

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警