使用C#导入导出数据到Excel
2011-05-31 11:31
1071 查看
1.实现原理
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
ab6c
2.程序设计
程序集类图
上面是直接用vs生存的,不是大符合UML类图规范,但主要的类关系已经勾勒出来了。
核心类是ExcelFile类,依赖HDRType,ExcelVersion和AppConfigKey这三个类似于枚举的类,AppConfig是配置管理类。
组件调用方式灵活:
1.直接用静态方式调用
2.用面向对象方式调用
Demo界面如下:
3.本组件主要优点
1.使用Oledb连接Excel,不依赖Com组件。
2.支持多页Sheet导入。
3.拥有自己的配置文件,可灵活配置组件参数。
4.当导入的数据超过每页最大允许数据量时,自动分页机制。
引用原文【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;" |
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(); |
} |
} |
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.程序设计
程序集类图
上面是直接用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界面如下:
3.本组件主要优点
1.使用Oledb连接Excel,不依赖Com组件。
2.支持多页Sheet导入。
3.拥有自己的配置文件,可灵活配置组件参数。
4.当导入的数据超过每页最大允许数据量时,自动分页机制。
相关文章推荐
- 使用C#导入导出数据到Excel
- 使用C#导入导出数据到Excel
- C#实现Excel模板导出和从Excel导入数据
- Oracle 使用TOAD实现导入导出Excel数据
- C#导出GridView的数据到Excel以及Excel导入SQL Server
- 使用PHP导入Excel和导出数据为Excel文件
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第三篇
- 使用PHPExcel实现对excel数据的导入和导出
- C# 使用NPOI 实现Excel的简单导入导出
- 使用C#把Excel格式文件的数据导入到sqlserver中
- ThinkPHP使用phpExcel实现Excel数据的导入导出(真正的完全步骤)
- 使用GridView将数据导入或导出Excel中
- C#导入导出(excel)数据
- 【推荐】.NET(webform,MVC)使用NPOI组件将数据导出、导入Excel。 Aspose.Cell 控件报表
- C#导入导出Excel数据的两种方法
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
- ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
- c# 导入excel处理数据,导出excel报表
- 使用C#或javascript将Table里的数据导出到Excel
- C#使用oledb方式将excel数据导入到datagridview后数据被截断为 255 个字符