DataTable'ı GridWeb'den dışa aktarın

Çalışma Sayfası Verilerini Dışa Aktarma

Belirli bir DataTable’a

Çalışma sayfası verilerini belirli bir DataTable nesnesine vermek için:

  1. Aspose.Cells.GridWeb denetimini Web Formunuza ekleyin.
  2. Belirli bir DataTable nesnesi oluşturun.
  3. Seçilen hücrelerin verilerini belirtilen DataTable nesnesine aktarın.

Aşağıdaki örnek, dört sütunlu belirli bir DataTable nesnesi oluşturur. Çalışma sayfası verileri, çalışma sayfasında tüm satırların ve sütunların göründüğü ilk hücreden başlayarak önceden oluşturulmuş bir DataTable nesnesine aktarılır.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Creating a new DataTable object
DataTable dataTable = new DataTable();
// Adding specific columns to the DataTable object
dataTable.Columns.Add("Name", System.Type.GetType("System.String"));
dataTable.Columns.Add("Gender", System.Type.GetType("System.String"));
dataTable.Columns.Add("Age", System.Type.GetType("System.Int32"));
dataTable.Columns.Add("Class", System.Type.GetType("System.String"));
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Getting the total number of rows and columns inside the worksheet
int totalColumns = sheet.Cells.MaxColumn + 1;
int totalRows = sheet.Cells.MaxRow + 1;
// Exporting the data of the active worksheet to a specific DataTable object
dataTable = sheet.Cells.Export(0, 0, totalRows, totalColumns, true, true);
// Display exported data table in GridView
GridView1.DataSource = dataTable;
GridView1.DataBind();

Yeni bir DataTable’a

Bazen bir DataTable nesnesi oluşturmak istemezsiniz, sadece çalışma sayfası verilerini yeni bir DataTable nesnesine aktarmanız gerekir.

Aşağıdaki örnek, Export yönteminin kullanımını göstermek için farklı bir yol deniyor. Aktif çalışma sayfasının referansını alır ve bu çalışma sayfasının tüm verilerini yeni bir DataTable nesnesine verir. DataTable nesnesi artık istediğiniz şekilde kullanılabilir. Örneğin, verileri görüntülemek için DataTable nesnesini bir GridView’e bağlamak mümkündür.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet1 = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Getting the total number of rows and columns inside the worksheet
int totalColumns1 = sheet.Cells.MaxColumn + 1;
int totalRows1 = sheet.Cells.MaxRow + 1;
// Exporting the data of the active worksheet to a new DataTable object
DataTable dt = sheet.Cells.Export(0, 0, totalRows1, totalColumns1, true, true);
// Display exported data table in GridView
GridView2.DataSource = dataTable;
GridView2.DataBind();