使用C#导入导出数据到Excel(OleDB方式)

本文介绍如何使用C#通过OleDb连接Excel文件进行读写操作,包括创建表格、插入记录及从Excel读取数据到DataTable的方法。该方案无需依赖Office自动化组件,支持多页Sheet导入,并具备灵活的配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.实现原理

引用原文【Import / export data in MS Excel using C#】

Sometimes we may require to generate Excel file from our reports,read from excel files to import data etc. This can be achieved using Office Interop (Office Automation) assemblies, but Office Automation in Web servers,got some issues;(More details: http://support.microsoft.com/kb/257757). The alternative is using OleDb provider. You may need to add one more attribute to connection string to connect to the Excel file. And connection string will be

 
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;"

Please note the “Extended Properties” attribute. This attribute helps us to query the excel file.

Exporting Data from Data Table to Excel File.

 
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;"
// Establish a connection to the data source.
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
//creating a new Sheet with name sample and three columns with Heading firstname, lastname and email
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "CREATE TABLE [Sample$](FirstName Char(255), LastName char(255), Email char(255))";
command.ExecuteNonQuery();
}
//Adding records to the Sample Worksheet
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "INSERT INTO TABLE [Sample$](FirstName,LastName,Email) VALUES('Anuraj','P','anuraj.p@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO TABLE [Sample$](FirstName,LastName,Email) VALUES('sreekumar','vn','sreekumar.vn@example.com')";
command.ExecuteNonQuery();
}
}

Import the Data from Excel

 
DataTable dt;
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;"
// Establish a connection to the data source.
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
//reading data from excel to Data Table
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "SELECT * FROM [Sample]";
using(OleDbDataAdapter adapter =new OleDbDataAdapter())
{
adapter.SelectCommand = command;
adapter.Fill(dt);
}
}
}

 

 

 2.程序设计

程序集类图

使用C导入导出数据到Excel(OleDB方式) - xyq - xyq

上面是直接用vs生存的,不是大符合UML类图规范,但主要的类关系已经勾勒出来了。

核心类是ExcelFile类,依赖HDRType,ExcelVersion和AppConfigKey这三个类似于枚举的类,AppConfig是配置管理类。

  

组件调用方式灵活:

1.直接用静态方式调用

 
//调用ExcelFile组件执行导入操作。
DataTable[] dtExcelDatas = ExcelFile.GetData(fileSavePath, excelVersion, HDRType.Yes, true);
//调用ExcelFile组件执行导出操作。 
ExcelFile.SetData(exportData[0], tempFilePath + fileName, excelVersion, HDRType.Yes);

 

  

2.用面向对象方式调用

 
ExcelFile ef = new ExcelFile();
ef.Version = ExcelVersion.Excel8;
ef.HDR = HDRType.Yes;
ef.DataSource = dt;
ef.FilePath = "d:\\test.xls";
ef.Import();

 

Demo界面如下:

使用C导入导出数据到Excel(OleDB方式) - xyq - xyq

 3.本组件主要优点

1.使用Oledb连接Excel,不依赖Com组件。

2.支持多页Sheet导入。

3.拥有自己的配置文件,可灵活配置组件参数。

4.当导入的数据超过每页最大允许数据量时,自动分页机制。

转自:http://www.cnblogs.com/tuyile006/archive/2010/08/25/1795289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值