Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 27, 2021 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/550f80c22e22768be6c5418bbdd0c89b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/550f80c22e22768be6c5418bbdd0c89b to your computer and use it in GitHub Desktop.
Export Excel Data to DataTable in C#
// Create a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("Excel.xlsx", FileMode.Open);
// Instantiate a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Export the contents of 2 rows and 2 columns starting from 1st cell to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, 2, 2, true);
// Bind the DataTable with DataGrid
dataGridView1.DataSource = dataTable;
// Close the file stream to free all resources
fstream.Close();
// Create a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("Excel.xlsx", FileMode.Open);
// Instantiate a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Export the contents of 2 rows and 2 columns starting from 1st cell to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0,2, 2, true);
// Bind the DataTable with DataGrid
dataGridView1.DataSource = dataTable;
// Close the file stream to free all resources
fstream.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment