c#Winform窗体 自动生成EXCEL并可以插入数据
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
namespace EPAS.f06PreData//自己命名
{
    public partial class frmSetThreshold : Form 
    {
        public frmSetThreshold()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//按钮一进行动态创建excel
        {
            try
            {
                if (File.Exists("E:\\example.xls"))
                    File.Delete("E:\\example.xls");
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.xls;Extended Properties='Excel 8.0;HDR=Yes'"))
                {
                    conn.Open();
                    OleDbCommand cmd = new OleDbCommand("CREATE TABLE  booksheet1 ([测项ID] varchar, [名字] varchar,[最大阈值] varchar,[最小阈值] varchar,[备注] varchar )", conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("创建excle成功");
                }
            }
            catch (System.Exception )
            {
               MessageBox.Show("阈值.xls已存在!");
            }
        }
        private void button2_Click(object sender, EventArgs e)//按钮二点击插入数据
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
            {
                MessageBox.Show("输入值不能为空!");
            }
            else
            {
                string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.xls;Extended Properties='Excel 8.0;HDR=Yes'";//HDR=Yes表示第一行作为标题使用
                OleDbConnection conn = new OleDbConnection(connstring);
                string commandstring = "SELECT * FROM [booksheet1]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(commandstring, conn);
                OleDbCommandBuilder cd = new OleDbCommandBuilder(adapter);
                DataSet ds = new DataSet();
                try
                {
                    conn.Open();
                    adapter.Fill(ds, "ww");
                    DataRow new_row1 = ds.Tables["ww"].NewRow();
                    new_row1["测项ID"] = textBox1.Text;
                    new_row1["名字"] = textBox2.Text;
                    new_row1["最大阈值"] = textBox3.Text;
                    new_row1["最小阈值"] = textBox4.Text;
                    new_row1["备注"] = textBox5.Text;
ds.Tables["ww"].Rows.Add(new_row1);
                    adapter.Update(ds, "ww");
                    conn.Close();
                    MessageBox.Show("成功导入EXCEL!");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
//这趟封闭开发2017-2-16至2017-2-24 我写代码的能力一般,此次c#对winform窗体的控制多引用他人代码并加以修改。
引用窗体的图下次补上 没有来得及附图...
                    
                
                
            
        
浙公网安备 33010602011771号