// Namespaces, Variables, and Constants
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);
}
//该代码片段来自于: http://www.sharejs.com/codes/csharp/7754

C#创建、读取和修改Excel的更多相关文章

  1. 【转】Python xlrd、xlwt、xlutils读取、修改Excel文件

    Python xlrd.xlwt.xlutils读取.修改Excel文件 一.xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文 ...

  2. 利用Python读取和修改Excel文件(包括xls文件和xlsx文件)——基于xlrd、xlwt和openpyxl模块

    https://blog.csdn.net/sinat_28576553/article/details/81275650#4.4%C2%A0%E4%BF%9D%E5%AD%98%E5%B7%A5%E ...

  3. CRUD操作 create创建 read读取 update修改 delete删除

    1.注释语法:--,#2.后缀是.sql的文件是数据库查询文件3.保存查询4.在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作:create 创建(添加)read 读取update 修改 ...

  4. lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

    在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...

  5. Cookie操作类、 包括创建、读取、修改、获取、销毁cookie

    Cookie操作类. 包括创建.读取.修改.获取.销毁cookie import java.util.Hashtable; import java.util.Iterator; import java ...

  6. 天下文章一大抄 之 修改excel 创建时间

    Sub ChangeDate()ThisWorkbook.BuiltinDocumentProperties("Creation Date") = #2 28 2016 13:25 ...

  7. Python xlrd、xlwt、xlutils修改Excel文件

    一.xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件.首先,打开workbook:    import xlrdwb = x ...

  8. 如何使用JavaScript实现纯前端读取和导出excel文件

    js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls.xlsx.ods(一种OpenOffice专 ...

  9. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

随机推荐

  1. iOS 进入后台的处理

    当App进入后台时(按下Home键), App会被系统暂停, 所有的程序逻辑都会停止, App还是驻留内存中, 除非被用户强制退出, 或者被系统kill掉(为了保证正在前台运行的App有足够的内存, ...

  2. relatedTarget、fromElement、toElement之间的关系

    在发生mouseover和mouseout事件时会把鼠标指针从一个元素的边界之内移到另一个元素边界之内.对 mouseover事件而言,事件的target是获得光标的元素,而relatedTarget ...

  3. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

  4. Redis附加功能之Redis事务

    一.事务 Redis 的事务功能允许用户将多个命令包裹起来,然后一次性地.按顺序地执行被包裹的所有命令.在事务执行的过程中,服务器不会中断事务而改去执行其他命令请求,只有在事务包裹的所有命令都被执行完 ...

  5. C and C++ 如何嵌套使用

    1. 要知道extern "C"的含义: (1) extern是C/C++中表明函数和全局变量作用范围的关键字, 该关键字表明其申明的函数和变量可以在本模块或者其他模块中使用. ( ...

  6. 深度解析国内O2O模式

    今日在网上发现这篇文章很棒,详细的分析了当前BAT矩阵下的o2o 的模式.所以转载过来与大家一起分享. 文章来自于:http://www.siilu.com/20151214/158917.shtml ...

  7. Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (2)

    Swiftsuspenders2简介 Swiftsuspenders2是一个基于元数据(metadata)的IOC(控制反转,inversion of control)的AS3的解决方案.(对于元数据 ...

  8. 在Service Fabric上部署Java应用,体验一把微服务的自动切换

    虽然Service Fabric的Java支持版本还没有正式发布,但是Service Fabric本身的服务管理.部署.升级等功能是非常好用的,那么Java的开发者可以如何利用上Service Fab ...

  9. U盘无法拷贝超过4G的大文件

    现在U盘的容量越来越大了,8G闪存满天飞,几乎已成“标配”,市面上再见难觅64M.128M等U盘的踪迹,可是细心的你也许已经发现,即使是8G或更大体积的U盘,仍然不能拷贝存储体积超过4G的大文件,这是 ...

  10. 洛谷P2735 电网 Electric Fences

    P2735 电网 Electric Fences 11通过 28提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 在本题中,格点是 ...