الاستيراد من DataTable

يمكن للمطورين استيراد البيانات من ملفجدول البيانات إلى أوراق العمل الخاصة بهم عن طريق استدعاءإيمبورداتاتابلي طريقة جمع Cells. هناك العديد من الإصدارات المحملة بشكل زائد منإيمبورداتاتابلي طريقة ولكن الحمل الزائد النموذجي يأخذ المعلمات التالية:جدول البيانات ، يمثلجدول البيانات الكائن الذي يجب استيراد محتوياته

  • هل اسم الحقل معروض، يحدد ما إذا كان يجب استيراد أسماء أعمدة DataTable إلى ورقة العمل كصف أول أم لا
  • ابدأ Cell، يمثل اسم خلية البداية (أي “A1”) من مكان استيراد محتويات DataTable
 //Instantiating a Workbook object

Workbook workbook = new Workbook();

//Adding a new worksheet to the Workbook object

int i = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = workbook.Worksheets[i];

//Instantiating a "Products" DataTable object

DataTable dataTable = new DataTable("Products");

//Adding columns to the DataTable object

dataTable.Columns.Add("Product ID", typeof(Int32));

dataTable.Columns.Add("Product Name", typeof(string));

dataTable.Columns.Add("Units In Stock", typeof(Int32));

//Creating an empty row in the DataTable object

DataRow dr = dataTable.NewRow();

//Adding data to the row

dr[0]= 1;

dr[1]= "Aniseed Syrup";

dr[2]= 15;

//Adding filled row to the DataTable object

dataTable.Rows.Add(dr);

//Creating another empty row in the DataTable object

dr = dataTable.NewRow();

//Adding data to the row

dr[0]= 2;

dr[1]= "Boston Crab Meat";

dr[2]= 123;

//Adding filled row to the DataTable object

dataTable.Rows.Add(dr);

//Importing the contents of DataTable to the worksheet starting from "A1" cell,

//where true specifies that the column names of the DataTable would be added to

//the worksheet as a header row

worksheet.Cells.ImportDataTable(dataTable, true, "A1");

workbook.Save("Import From Data Table.xls");

تنزيل نموذج التعليمات البرمجية