Exportar DataTable desde GridWeb

Exportar datos de hoja de cálculo

A un datatable específico.

Para exportar los datos de la hoja de cálculo a un objeto DataTable específico:

  1. Agrega el control Aspose.Cells.GridWeb a tu Formulario Web.
  2. Crea un objeto DataTable específico.
  3. Exporta los datos de las celdas seleccionadas al objeto DataTable especificado.

El ejemplo a continuación crea un objeto DataTable específico con cuatro columnas. Los datos de la hoja de cálculo se exportan comenzando desde la primera celda con todas las filas y columnas visibles en la hoja de cálculo, a un objeto DataTable ya creado.

// 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();

A un nuevo objeto DataTable

A veces, no deseas crear un objeto DataTable, sino simplemente necesitas exportar los datos de la hoja de cálculo a un nuevo objeto DataTable.

El ejemplo a continuación muestra una manera diferente de mostrar el uso del método de exportación. Toma la referencia de la hoja de cálculo activa y exporta los datos completos de esa hoja de cálculo a un nuevo objeto DataTable. El objeto DataTable ahora puede ser utilizado de la manera que desees. Por ejemplo, es posible ligar el objeto DataTable a un GridView para ver los datos.

// 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();