2024年终活动

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

[DevExpress WinForms控件] 绑定到Excel数据源(一)

来源:   发布时间:2017-09-15   浏览:6718次

本系列文档将说明如何使用数据源配置向导,ExcelDataSource组件和代码绑定来自Microsoft Excel工作簿(XLS,XLSX或XLSM文件)或CSV文件的数据。 首先,我们来看如何使用代码达成这一目的。(点击查看如何用配置向导实现)   一、在代码中进行绑定 1. 创建一个ExcelDataSource组件的新实例。 [C#]
DevExpress.DataAccess.Excel.ExcelDataSource myExcelSource = new DevExpress.DataAccess.Excel.ExcelDataSource();
[VB]
Dim myExcelSource As New DevExpress.DataAccess.Excel.ExcelDataSource()
  2. 使用ExcelDataSource.FileName或ExcelDataSource.Stream属性为组件提供来自本地存储或流的数据源。 [C#]
myExcelSource.FileName = @"d:\Work\Excel DataSources\Book1.xlsx";
[VB]
myExcelSource.FileName = "d:\Work\Excel DataSources\Book1.xlsx"
  3. 根据数据源类型(Excel工作簿或CSV文件),创建一个ExcelSourceOptions或CsvSourceOptions对象。使用此对象的属性,可以指定要加载的单元格范围。将创建的对象分配给ExcelDataSource.SourceOptions属性。 [C#]
ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("SalesPerson", "A1:L100");
myExcelSource.SourceOptions = new ExcelSourceOptions(worksheetSettings);
//or 
myExcelSource.SourceOptions = new CsvSourceOptions() { CellRange = "A1:L100" };
[VB]
Dim worksheetSettings As New ExcelWorksheetSettings("SalesPerson", "A1:L100")
myExcelSource.SourceOptions = New ExcelSourceOptions(worksheetSettings)
'or 
myExcelSource.SourceOptions = New CsvSourceOptions() With {.CellRange = "A1:L100"}
  4. 如果需要,请设置其他导入选项 [C#]
myExcelSource.SourceOptions.SkipEmptyRows = false;
myExcelSource.SourceOptions.UseFirstRowAsHeader = true;
[VB]
myExcelSource.SourceOptions.SkipEmptyRows = False 
myExcelSource.SourceOptions.UseFirstRowAsHeader = True 
  5. 将您的ExcelDataSource分配给数据感知控件,并调用组件的Fill方法以用记录填充它。 [C#]
myExcelSource.Fill();
gridControl1.DataSource = myExcelSource;
[VB]
myExcelSource.Fill()
gridControl1.DataSource = myExcelSource
  完整的代码如下所示。 [C#]
using DevExpress.DataAccess.Excel;
//. . . 

DevExpress.DataAccess.Excel.ExcelDataSource myExcelSource = new DevExpress.DataAccess.Excel.ExcelDataSource();
myExcelSource.FileName = @"d:\Work\Excel DataSources\Book1.xlsx";
ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("SalesPerson", "A1:L13");
myExcelSource.SourceOptions = new ExcelSourceOptions(worksheetSettings);
//or 
myExcelSource.SourceOptions = new CsvSourceOptions() { CellRange = "A1:L100" };
myExcelSource.SourceOptions.SkipEmptyRows = false;
myExcelSource.SourceOptions.UseFirstRowAsHeader = true;
gridControl1.DataSource = myExcelSource;
myExcelSource.Fill();
[VB]
Imports DevExpress.DataAccess.Excel
'. . . 

Dim myExcelSource As New DevExpress.DataAccess.Excel.ExcelDataSource()
myExcelSource.FileName = "d:\Work\Excel DataSources\Book1.xlsx" 
Dim worksheetSettings As New ExcelWorksheetSettings("SalesPerson", "A1:L13")
myExcelSource.SourceOptions = New ExcelSourceOptions(worksheetSettings)
'or 
myExcelSource.SourceOptions = New CsvSourceOptions() With {.CellRange = "A1:L100"}
myExcelSource.SourceOptions.SkipEmptyRows = False 
myExcelSource.SourceOptions.UseFirstRowAsHeader = True 
gridControl1.DataSource = myExcelSource
myExcelSource.Fill()
DevExpress线下研修班
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/901.html

相关产品: DevExpress Universal Subscription,

扫码咨询
电话咨询
023-68661681
返回
顶部