Esporta DataTable da GridWeb

Esportazione dei dati del foglio di calcolo

Verso una specifica DataTable

Per esportare i dati del foglio di lavoro in un oggetto DataTable specifico:

  1. Aggiungi il controllo Aspose.Cells.GridWeb al tuo modulo web.
  2. Crea un oggetto DataTable specifico.
  3. Esporta i dati delle celle selezionate nell’oggetto DataTable specificato.

L’esempio qui sotto crea un oggetto DataTable specifico con quattro colonne. I dati del foglio di lavoro vengono esportati a partire dalla prima cella con tutte le righe e colonne visibili nel foglio di lavoro, verso un oggetto DataTable già creato.

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

Verso un nuovo oggetto DataTable

A volte, non si desidera creare un oggetto DataTable ma è sufficiente esportare i dati del foglio di lavoro in un nuovo oggetto DataTable.

L’esempio qui sotto cerca un modo diverso per mostrare l’uso del metodo di esportazione. Prende il riferimento del foglio di lavoro attivo e esporta i dati completi di quel foglio di lavoro in un nuovo oggetto DataTable. L’oggetto DataTable può ora essere utilizzato in qualsiasi modo desiderato. Ad esempio, è possibile associare l’oggetto DataTable a una GridView per visualizzare i dati.

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