C#創(chuàng)建、讀取和修改Excel的方法
本文實(shí)例講述了C#創(chuàng)建、讀取和修改Excel的方法。分享給大家供大家參考。具體如下:
windows下我們可以通過(guò) Jet OLE DB訪問(wèn)Excel,就行訪問(wèn)數(shù)據(jù)庫(kù)一樣
using System;
using System.Configuration;
using System.Data;
private OleDbDataAdapter da;
private DataTable dt;
private void Excel_Load(object sender, System.EventArgs e)
{
// Create the DataAdapter.
da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", ConfigurationSettings.AppSettings["ExcelConnectString1"]);
// Create the insert command.
String insertSql = "INSERT INTO [Sheet1$] (CategoryID, CategoryName, Description) VALUES (?, ?, ?)";
da.InsertCommand = new OleDbCommand(insertSql, da.SelectCommand.Connection);
da.InsertCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0, "CategoryID");
da.InsertCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15, "CategoryName");
da.InsertCommand.Parameters.Add("@Description", OleDbType.VarChar, 100, "Description");
// Create the update command.
String updateSql = "UPDATE [Sheet1$] SET CategoryName=?, Description=? " WHERE CategoryID=?";
da.UpdateCommand = new OleDbCommand(updateSql, da.SelectCommand.Connection);
da.UpdateCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15, "CategoryName");
da.UpdateCommand.Parameters.Add("@Description", OleDbType.VarChar, 100, "Description");
da.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0, "CategoryID");
// Fill the table from the Excel spreadsheet.
dt = new DataTable( );
da.Fill(dt);
// Define the primary key.
dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
// Records can only be inserted using this technique.
dt.DefaultView.AllowDelete = false;
dt.DefaultView.AllowEdit = true;
dt.DefaultView.AllowNew = true;
// Bind the default view of the table to the grid.
dataGrid.DataSource = dt.DefaultView;
}
private void updateButton_Click(object sender, System.EventArgs e)
{
da.Update(dt);
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- ASP.NET(C#)讀取Excel的文件內(nèi)容
- C#讀取Excel的三種方式以及比較分析
- c#利用Excel直接讀取數(shù)據(jù)到DataGridView
- C#使用Aspose.Cells控件讀取Excel
- C#利用Openxml讀取Excel數(shù)據(jù)實(shí)例
- C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中
- ADO.NET 讀取EXCEL的實(shí)現(xiàn)代碼((c#))
- C#讀取Excel并轉(zhuǎn)化成XML的方法
- C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫(kù)的方法
- Unity讀取Excel文件轉(zhuǎn)換XML格式文件
相關(guān)文章
C#使用stackalloc分配堆棧內(nèi)存和非托管類(lèi)型詳解
這篇文章主要為大家介紹了C#使用stackalloc分配堆棧內(nèi)存和非托管類(lèi)型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-12-12
C# 并發(fā)控制框架之單線程環(huán)境下實(shí)現(xiàn)每秒百萬(wàn)級(jí)調(diào)度
本文介紹了一款專(zhuān)為工業(yè)自動(dòng)化及機(jī)器視覺(jué)開(kāi)發(fā)的C#并發(fā)流程控制框架,通過(guò)模仿Go語(yǔ)言并發(fā)模式設(shè)計(jì),支持高頻調(diào)度及復(fù)雜任務(wù)處理,已在多個(gè)項(xiàng)目中驗(yàn)證其穩(wěn)定性和可靠性2024-10-10
C#實(shí)現(xiàn)系統(tǒng)托盤(pán)通知的方法
這篇文章主要介紹了C#實(shí)現(xiàn)系統(tǒng)托盤(pán)通知的方法,涉及C#系統(tǒng)api調(diào)用的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)讀取txt文件生成Word文檔
大家好,本篇文章主要講的是C#實(shí)現(xiàn)讀取txt文件生成Word文檔,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C#程序集的主版本號(hào)和次版本號(hào)的實(shí)現(xiàn)
C# 程序集的版本號(hào)和次版本號(hào)是程序集的一部分,用于標(biāo)識(shí)程序集的不同版,本本文主要介紹了C#程序集的主版本號(hào)和次版本號(hào)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
json格式數(shù)據(jù)分析工具PageElement類(lèi)分享(仿Session寫(xiě)法)
json格式數(shù)據(jù)分析工具PageElement類(lèi)分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進(jìn)行交互2013-12-12

