基本信息
源码名称:自动出题并生成excel表格
源码大小:8.50M
文件格式:.rar
开发语言:C#
更新时间:2020-03-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


生成的excel如下:

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;
        }
    }
}