Import or Export DataGrid to Excel or HTML files from your .NET applications

Posted by Rajesh081725 under VB.NET category on | Points: 40 | Views : 2593
Visual Basic .NET code:
Dim ef As New ExcelFile
Dim dataTable As DataTable = DirectCast(dataGrid1.DataSource, DataTable)

' Depending on the format of the input file, you need to change this:
dataTable.Columns.Add("FirstName", GetType(String))
dataTable.Columns.Add("LastName", GetType(String))

' Load Excel file.

' Select the first worksheet from the file.
Dim ws As ExcelWorksheet = ef.Worksheets(0)

' Extract the data from the worksheet to the DataTable (DataGrid).
' Data is extracted starting at first row and first column for 10 rows or until the first empty row appears.
ws.ExtractToDataTable(dataTable, 10, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows(0), ws.Columns(0))

' Change the value of the first cell in the DataTable (DataGrid).
dataTable.Rows(0)(0) = "Hello world!"

' Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", True)

' Save the file to XLS format.

Comments or Responses

Posted by: T.Saravanan on: 11/15/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Do we need to add any references for read & write the excel file?
I think, the section "Import and Export to HTML" is missing in your code. Can you update your code?

Login to post response