• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#创建、读取和修改Excel的方法

C#创建、读取和修改Excel的方法

作者:令狐不聪 字体:[增加 减小] 来源:互联网 时间:2017-05-28

令狐不聪 通过本文主要向大家介绍了c#读取excel,c#读取excel数据,c#读取excel文件,c#读取excel单元格,c#读取excel表格等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C#创建、读取和修改Excel的方法。分享给大家供大家参考。具体如下:

windows下我们可以通过 Jet OLE DB访问Excel,就行访问数据库一样
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);
}</div>

希望本文所述对大家的C#程序设计有所帮助。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#创建、读取和修改Excel的方法
  • 不安装excel使用c#创建excel文件
  • c#高效率导出多维表头excel的实例代码

相关文章

  • 2017-05-28c#对象反序列化与对象序列化示例详解
  • 2017-05-28C#定义并实现单链表实例解析
  • 2017-05-28timespan使用方法详解
  • 2017-05-28C#实现软件监控外部程序运行状态的方法
  • 2017-05-28C#装饰者模式实例分析
  • 2017-05-28C#实现根据指定容器和控件名字获得控件的方法
  • 2017-05-28验证本机的excel版本的C#代码
  • 2017-05-28WPF实现时钟特效
  • 2017-05-28C#中foreach原理以及模拟的实现
  • 2017-05-28C# 中GUID生成格式的四种方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# 文字代码页 文字编码的代码页名称速查表
    • C#实现十五子游戏
    • C#开发的人脸左右相似度计算软件源码分析
    • DevExpress根据条件设置GridControl RepositoryItem是否可编辑
    • C#微信开发第一章
    • C# 设计模式之单例模式归纳总结
    • DevExpress实现TreeList按条件隐藏节点CheckBox的方法
    • C#版的 Escape() 和 Unescape() 函数分享
    • 深入多线程之:深入生产者、消费者队列分析
    • C#的循环语句集锦及案例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有