Last active
January 27, 2021 02:03
-
-
Save aspose-com-gists/550f80c22e22768be6c5418bbdd0c89b to your computer and use it in GitHub Desktop.
Export Excel Data to DataTable in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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