您的位置:首页 > 编程语言 > C#

使用C#导入导出数据到Excel

2012-03-15 21:22 811 查看
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

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

Exporting Data from Data Table to Excel File.

?
Import the Data from Excel

?
2.程序设计

程序集类图



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

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

组件调用方式灵活:

1.直接用静态方式调用

?
2.用面向对象方式调用

?
Demo界面如下:



3.本组件主要优点

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

2.支持多页Sheet导入。

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

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

程序源码:CODE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: