C#運用ADO.net動態建立excle並進行相應的資料修改顯示

iDotNetSpace發表於2010-06-07

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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-664564/,如需轉載,請註明出處,否則將追究法律責任。

相關文章