• C#运用ADO.net动态创建excle并进行相应的数据修改显示


    button1是动态创建excle,button2是添加数据,button3是现实数据

    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 System.Data.OleDb;
    using System.IO;

    namespace 连接EXCLE
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    if (File.Exists("f:\\example.xls"))
                        File.Delete("f:\\example.xls");
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\example.xls;Extended Properties='Excel 8.0;HDR=Yes'"))
                    {
                        conn.Open();
                        OleDbCommand cmd = new OleDbCommand("CREATE TABLE  booksheet1 ([x] int, [y] int, [z] int)", conn);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("创建excle成功");
                    }
                }
                catch (System.Exception)
                {
                    MessageBox.Show("ERROR");
                }
            }
            private void button2_Click(object sender, EventArgs e)
            {
                string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\example.xls;Extended Properties='Excel 8.0;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["x"] = 3;
                    new_row1["y"] = 4;
                    new_row1["z"] = 5;
                    ds.Tables["ww"].Rows.Add(new_row1);
                    DataRow new_row2 = ds.Tables["ww"].NewRow();
                    new_row2["x"] = 6;
                    new_row2["y"] = 7;
                    new_row2["z"] = 8;
                    ds.Tables["ww"].Rows.Add(new_row2);
                    adapter.Update(ds, "ww");
                    conn.Close();
                    MessageBox.Show("添加数据成功");
                }
                catch (System.Exception)
                {
                    MessageBox.Show("Error");
                }
            }
            private void button3_Click(object sender, EventArgs e)
            {
                string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\example.xls;Extended Properties='Excel 8.0;HDR=Yes'";
                OleDbConnection conn = new OleDbConnection(connstring);
                string commandstring = "SELECT * FROM [booksheet1]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(commandstring, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "ww");
                dataGridView1.DataSource = new DataView(ds.Tables["ww"]);
            }
        }
    }

  • 相关阅读:
    Apache Spark源码走读之4 -- DStream实时流数据处理
    Apache Spark源码走读之3 -- Task运行期之函数调用关系分析
    Apache Spark源码走读之2 -- Job的提交与运行
    Apache Spark源码走读之1 -- Spark论文阅读笔记
    Spark
    (转)互联网广告综述之点击率特征工程
    Java排序算法之冒泡排序
    java枚举
    第二课、数据的艺术---------------------狄泰软件学院
    第一课、进阶高手的大门--------------------狄泰软件学院
  • 原文地址:https://www.cnblogs.com/2814/p/1752649.html
Copyright © 2020-2023  润新知