• 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"]);
            }
        }
    }

  • 相关阅读:
    Javascript、CSS和IMG之网页执行探索
    从零开始学习Node.js例子九 设置HTTP头
    从零开始学习Node.js例子八 使用SQLite3和MongoDB
    如何做到 jQuery-free?
    jQuery的deferred对象详解
    使用openxml读取xml数据
    Drupal commerce 性能优化
    DataTable数据进行排序、检索、合并、分页、统计
    jquery实现替代iframe的功能
    9_Jvn框架之实现ORM持久层save操作(第九讲)
  • 原文地址:https://www.cnblogs.com/2814/p/1752649.html
Copyright © 2020-2023  润新知