استيراد البيانات من DataTable إلى الشبكة
Contents
[
Hide
]
منذ إصدار .NET Framework ، قدم Microsoft طريقة ممتازة لتخزين البيانات في الوضع غير المتصل في شكل كائن DataTable. فهم احتياجات المطورين ، Aspose.Cells.GridDesktop يدعم أيضًا استيراد البيانات من جدول البيانات. يناقش هذا الموضوع كيفية القيام بذلك.
مثال
لاستيراد محتويات جدول البيانات باستخدام Aspose.Cells.GridDesktop control
- أضف Aspose.Cells.GridDesktop control إلى نموذج.
- قم بإنشاء كائن DataTable يحتوي على البيانات المراد استيرادها.
- احصل على مرجع ورقة العمل المطلوبة.
- قم باستيراد محتويات جدول البيانات إلى ورقة العمل.
- قم بتعيين رؤوس الأعمدة في ورقة العمل وفقًا لأسماء الأعمدة في جدول البيانات.
- اضبط عرض الأعمدة ، إذا رغبت في ذلك /
- اعرض ورقة العمل.
في المثال الموضح أدناه ، قمنا بإنشاء كائن DataTable وملأناه ببعض البيانات التي تم جلبها من جدول قاعدة البيانات المسمى Products. أخيرًا ، قمنا باستيراد البيانات من كائن DataTable إلى ورقة العمل المطلوبة باستخدام Aspose.Cells.GridDesktop.
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 | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
OleDbDataAdapter adapter; | |
DataTable dt = new DataTable(); | |
// Creating connection string to connect with database | |
string conStr = @"Provider=microsoft.jet.oledb.4.0;Data Source=" + dataDir + "dbDatabase.mdb"; | |
// Creating Select query to fetch data from database | |
string query = "SELECT * FROM Products ORDER BY ProductID"; | |
adapter = new OleDbDataAdapter(query, conStr); | |
// Filling DataTable using an already created OleDbDataAdapter object | |
adapter.Fill(dt); | |
// Accessing the reference of a worksheet | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Importing data from DataTable to the worksheet. 0,0 specifies to start importing data from the cell with first row (0 index) and first column (0 index) | |
sheet.ImportDataTable(dt, false, 0, 0); | |
// Iterating through the number of columns contained in the DataTable | |
for (int i = 0; i < dt.Columns.Count; i++) | |
{ | |
// Setting the column headers of the worksheet according to column names of the DataTable | |
sheet.Columns[i].Header = dt.Columns[i].Caption; | |
} | |
// Setting the widths of the columns of the worksheet | |
sheet.Columns[0].Width = 240; | |
sheet.Columns[1].Width = 160; | |
sheet.Columns[2].Width = 160; | |
sheet.Columns[3].Width = 100; | |
// Displaying the contents of the worksheet by making it active | |
gridDesktop1.ActiveSheetIndex = 0; |