跳过导航链接
技术文档
·网站建设
·软件使用
·图形设计
·程序开发
·网络应用
·电脑技巧
 
公司介绍
·公司简介
·索仕SRCOS网络应用平台
·索仕网站管理系统
·影视广告制作
·联系我们
 
 

[ADO.NET] 如何 使用 OLE DB 讀寫 Excel / 建立 Excel 檔案 (二)

2010-2-6 1:51:57

 

1.本篇主要是使用Adapter與Parameter異動資料

2.Excel儲存格的欄位長這樣,英文代表Column,數字代表Row

ds.Tables[0].Rows[0][1] = textBox2.Text;

[0]→指的是Row

[1]→指的是Column

 

當然也可以醬用

ds.Tables[0].Rows[0]["Name"] = textBox2.Text;

["Name"]→代表的是欄位名稱,但不可缺少 "HDR=YES" 屬性。

 

3.其中Row會因為你下的SQL關係而影響

若是"HDR=YES",資料的第一格就是 "A2"。

[ADO.NET] 如何 使用 OLE DB 讀寫 Excel / 建立 Excel 檔案 (二) 
 

若是"HDR=NO",資料的第一格就是"A1"

[ADO.NET] 如何 使用 OLE DB 讀寫 Excel / 建立 Excel 檔案 (二)

 

4.為了要使用問號的 Where 條件: 
string qs1 = "Update [" + "EmployeeData" + "$] set Id=? , Name=? where Id=? ";

這問題困擾了我很久,直到比爾叔出面幫忙,使用了下列參數才得已成功。

//3.建立更新定義 
OleDbParameter parameter1 = cm.Parameters.Add("@id"OleDbType.VarChar, 50, "Id"); 
parameter1.SourceVersion = DataRowVersion.Current; 
OleDbParameter parameter2 = cm.Parameters.Add("@name"OleDbType.VarChar, 50, "Name"); 
parameter2.SourceVersion = DataRowVersion.Current; 
OleDbParameter parameter = cm.Parameters.Add("@oldID"OleDbType.VarChar, 50, "Id"); 
parameter.SourceVersion = DataRowVersion.Original; 
da.UpdateCommand = cm;

5.範例說明

如何插入一行資料

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = null;
    //連線字串
    cs =
            "Data Source=" + FileName + ";" +
            "Provider=" + ProviderName +
            "Extended Properties=" + ExtendedString +
            "HDR=" + Hdr +
            "IMEX=" + IMEX;
    //1.建立連線
    using (OleDbConnection cn = new OleDbConnection(cs))
    {
        cn.Open();
        string qs = "INSERT INTO [EmployeeData$] VALUES(?, ?)";
        //2.建立OleDbCommand物件
        using (OleDbCommand cm = new OleDbCommand(qs, cn))
        {
            //3.建立OleDbDataAdapter物件
            using (OleDbDataAdapter da = new OleDbDataAdapter("Select id,name From [EmployeeData$] ", cn))
            {
                //4.引用DataSet
                DataSet ds = new DataSet(); 
 
                //5.Fill到inputTable變數
                da.Fill(ds, "inputTable"); 
 
                //6.建立插入定義
                da.InsertCommand = cm;
                da.InsertCommand.Parameters.Add("@Id", OleDbType.VarChar).SourceColumn = "Id";
                da.InsertCommand.Parameters.Add("@Name", OleDbType.VarChar).SourceColumn = "Name"; 
 
                //7.在DataTable加入新的列
                DataRow dr = ds.Tables[0].NewRow();
                ds.Tables[0].Rows[0][0] = textBox1.Text;
                ds.Tables[0].Rows[0][1] = textBox2.Text; 
 
                //8.加入到DataTable裡
                ds.Tables[0].Rows.Add(dr); 
 
                //9.更新檔案
                da.UpdateCommand = cm;
                da.Update(ds, "inputTable");
            }
        }
    }
    ShowList();
    ShowGrid();
}

 

如何只更新一欄的資料

private void button4_Click(object sender, EventArgs e)
{
    this.button4.Enabled = false; 
 
    string qs1 = "Update [" + "EmployeeData" + "$] set Id=? , Name=? where Id=? ";
    string qs2 = "Select id,name From [EmployeeData$]";
    //1.建立連線
    using (OleDbConnection cn = new OleDbConnection(cs))
    {
        cn.Open();
        //2.建立OleDbCommand物件
        using (OleDbCommand cm = new OleDbCommand(qs1, cn))
        {
            using (OleDbDataAdapter da = new OleDbDataAdapter(qs2, cn))
            {
                //3.建立更新定義
                OleDbParameter parameter1 = cm.Parameters.Add("@id", OleDbType.VarChar, 50, "Id");
                parameter1.SourceVersion = DataRowVersion.Current;
                OleDbParameter parameter2 = cm.Parameters.Add("@name", OleDbType.VarChar, 50, "Name");
                parameter2.SourceVersion = DataRowVersion.Current;
                OleDbParameter parameter = cm.Parameters.Add("@oldID", OleDbType.VarChar, 50, "Id");
                parameter.SourceVersion = DataRowVersion.Original;
                da.UpdateCommand = cm; 
 
                //4.引用DataSet
                DataSet ds = new DataSet();
                //Fill到inputTable變數
                da.Fill(ds, "inputTable"); 
 
                //5.更新特定位置的值
                ds.Tables[0].Rows[Int32.Parse(textBox1.Text) - 1]["Name"] = textBox2.Text;
                //ds.Tables[0].Rows[1][1] = (string)this.textBox2.Text; 
 
                //6.更新檔案
                da.UpdateCommand = cm;
                da.Update(ds, "inputTable");
                //da.Update(ds.Tables[0]);
            }
        }
    }
    ShowList();
    ShowGrid();
    this.button4.Enabled = true;
}

 

如何用 dataGridView 轉成 Excel

//比爾叔友情贊助的CODE
DataSet myDataset = new DataSet();
BindingSource sBindingSource = new BindingSource();
private void ShowGrid()
{
    //清空資料繫結
    myDataset.Reset();
    OleDbConnection cn = new OleDbConnection(cs);
    CreatDataAdapter(cn).Fill(myDataset);
    cn.Close();
    cn.Dispose();
    sBindingSource.DataSource = myDataset.Tables[0];
    dataGridView1.DataSource = sBindingSource;
}
private OleDbDataAdapter CreatDataAdapter(OleDbConnection MyConn)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand myCmd;
    string SQLstr;
    SQLstr = "select * from[" + SheetName + "$]";
    myCmd = new OleDbCommand(SQLstr, MyConn);
    adapter.SelectCommand = myCmd; 
 
    SQLstr = "Update [" + SheetName + "$] set Id=? , Name=? where Id=? ";
    OleDbCommand myCmd1;
    myCmd1 = new OleDbCommand(SQLstr, MyConn);
    OleDbParameter parameter1 = myCmd1.Parameters.Add("@id", OleDbType.VarChar, 50, "Id");
    parameter1.SourceVersion = DataRowVersion.Current;
    OleDbParameter parameter2 = myCmd1.Parameters.Add("@name", OleDbType.VarChar, 50, "Name");
    parameter2.SourceVersion = DataRowVersion.Current;
    OleDbParameter parameter = myCmd1.Parameters.Add("@oldID", OleDbType.VarChar, 50, "Id");
    parameter.SourceVersion = DataRowVersion.Original; 
 
    adapter.UpdateCommand = myCmd1;
    // adapter.ContinueUpdateOnError=true ; 
    return adapter;
} 
 
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    OleDbConnection cn = new OleDbConnection(cs);
    CreatDataAdapter(cn).Update(myDataset.Tables[0]);
    cn.Close();
    cn.Dispose();
    ShowList();
    ShowGrid();
}

 

6.範例下載:

CS_OLEDB_Excel(2).rar

VB_OLEDB_Excel(2).rar

本文附件:
CS_OLEDB_Excel(2).rar
VB_OLEDB_Excel(2).rar
 
 
 
昆明索仕科技开发有限公司 版权所有 Copyright© 2002-2010 Kunming Source Technology Exploitive Co.,LTD. All Rights Reserved.
电话:0871-5627877 业务QQ:163871 联系我们  滇ICP备05000592号
本站基于:索仕网站信息管理系统建设 版本 2.0.3845