// 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. JavaScriptResult用法

    写MVC项目的时候,一开始就遇到返回脚本无法执行的情况,查阅博客园中老鸟的各种文章,最后没有得出个所以然,没办法,找项目经理实地讨论. public JavaScriptResult Hello() ...

  2. Arch Linux 修改主机名称

    Hostname Set the hostname to your liking (e.g. arch): # echo myhostname > /etc/hostname Add the s ...

  3. xdg-open 打开“irc:*”链接

    用于打开chrome浏览器中的"irc://*" #cp /usr/share/applications/xchat.desktop /home/zsj/.local/share/ ...

  4. PL/SQL常用设置 可看引用位置更清晰直观 引自:http://blog.csdn.net/xiaoqforever/article/details/27695569

    引自:http://blog.csdn.net/xiaoqforever/article/details/27695569 1,登录后默认自动选中My Objects 默认情况下,PLSQL Deve ...

  5. Party Games

    Party Games You've been invited to a party. The host wants to divide the guests into 2 teams for par ...

  6. CODESOFT 2015中的条形码对象该如何创建

     CODESOFT条码设计软件提供了大量适应行业要求的符号,以及创建二维条形码的选项.用户可以通过条形码对话框选择符号.定义其属性以及输入要编码的消息.下面小编带大家具体学习下如何在CODESOFT ...

  7. Linux重置系统root密码

    linux的root享有至高无上的权力,一旦root密码忘记或者被盗,是一件非常头痛的事情,那么怎么样才能找回root的密码呢,本人把其总结(亲身试验)如下:  (一)Linux 系统密码破解     ...

  8. vb 取得桌面路径

    txtPath.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

  9. Handler 取不到session 解决办法

      Handler需要继承 Handler : IHttpHandler, IReadOnlySessionState, IRequiresSessionState

  10. linux 下 jdk tar.gz 包安装方法

    JDK安装 tar.gz为解压后就可使用的版本,这里我们将jdk-7u3-linux-i586.tar.gz解压到/usr/local/下. 1.解压 解压到当前目录:$ tar -zxvf /opt ...