Exportar tabla de datos desde GridWeb
Exportación de datos de la hoja de trabajo
A una tabla de datos específica
Para exportar datos de la hoja de cálculo a un objeto DataTable específico:
- Agregue el control Aspose.Cells.GridWeb a su formulario web.
- Cree un objeto DataTable específico.
- Exporte los datos de las celdas seleccionadas al objeto DataTable especificado.
El siguiente ejemplo crea un objeto DataTable específico con cuatro columnas. Los datos de la hoja de cálculo se exportan a partir de 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 una nueva tabla de datos
A veces, no desea crear un objeto DataTable sino que simplemente necesita exportar los datos de la hoja de trabajo a un nuevo objeto DataTable.
El siguiente ejemplo intenta una forma diferente de mostrar el uso del método Exportar. Toma la referencia de la hoja de trabajo activa y exporta los datos completos de esa hoja de trabajo a un nuevo objeto DataTable. El objeto DataTable ahora se puede usar de la forma que desee. Por ejemplo, es posible vincular el objeto DataTable a 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(); |