将 DataView 导入 GridWeb
Contents
[
Hide
]
随着 Microsoft .NET Framework 的发布,引入了一种新的数据存储方式。现在 DataSet、DataTable 和 DataView 对象以离线模式存储数据。这些对象作为数据存储库非常方便。使用 Aspose.Cells.GridWeb,可以将数据从 DataTable 或 DataView 对象导入到工作表中。 Aspose.Cells.GridWeb只支持从DataView导入数据。对象,但也可以间接使用 DataTable 对象。让我们详细讨论这个特性。
从 DataView 导入数据
使用 GridWeb 控件中的 GridWorsheetCollection 的 ImportDataView 方法从 DataView 对象导入数据。将要从中导入数据的 DataView 对象传递给 ImportDataView 方法。可以在导入期间指定列标题和数据样式。
从 DataView 对象导入数据时,会创建一个新工作表来保存导入的数据。工作表的名称与 DataTable 的名称相同。
输出:从 DataView 导入到新工作表中的数据
调整列的宽度以显示它们包含的所有数据。从 DataView 导入数据时,不会自动调整列宽。用户需要自行调整。要以编程方式调整列宽,请参阅调整行和列的大小.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Connect database | |
System.Data.OleDb.OleDbConnection oleDbConnection1 = new OleDbConnection(); | |
System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(); | |
System.Data.OleDb.OleDbCommand oleDbSelectCommand1 = new OleDbCommand(); | |
string path = (this.Master as Site).GetDataDir(); | |
oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "\\Worksheets\\Database\\Northwind.mdb"; | |
oleDbSelectCommand1.Connection = oleDbConnection1; | |
oleDbDataAdapter1.SelectCommand = oleDbSelectCommand1; | |
DataTable dataTable1 = new DataTable(); | |
dataTable1.Reset(); | |
// Queries database. | |
try | |
{ | |
oleDbSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description FROM Categories"; | |
oleDbDataAdapter1.Fill(dataTable1); | |
} | |
catch | |
{ | |
} | |
finally | |
{ | |
oleDbConnection1.Close(); | |
} | |
// Imports data from dataview object. | |
dataTable1.TableName = "Categories"; | |
GridWeb1.WorkSheets.Clear(); | |
GridWeb1.WorkSheets.ImportDataView(dataTable1.DefaultView, null, null); | |
// Imports data from dataview object with sheet name and position specified. | |
GridWeb1.WorkSheets.ImportDataView(dataTable1.DefaultView, null, null, "SpecifiedName&Position", 2, 1); |
ImportDataView 方法的重载版本允许开发人员指定保存导入数据的工作表的名称以及要从 DataView 对象导入的特定行数和列数。