In the VB version of the room we have been exposed to the export of Excel function, again contact This function has a different understanding
First, the principle
To implement the ability to export Excel, you first refer to the namespace in order to use the methods and classes under that namespace
Imports Microsoft.Office.Interop
Then you need to create an instance of the classes you need to export in Excel, create an Excel app, workbooks, and tables
Dim xlapp, Xlbook, xlsheet as Object xlapp = CreateObject ("Excel.Application") xlbook = XlApp.Workbooks.Add xlsheet = Xlbook.worksheets ()
Once created, you can write the data you want to go to an Excel-instantiated table
Second, the Code
In the engine room, we're all going to export the data from the DataGridView.
Dim xlapp, Xlbook, xlsheet as Object xlapp = CreateObject ("Excel.Application") xlbook = XlApp.Workbooks.Add xlsheet = xlbook.worksheets () ' make sheet visible xlapp.visible = True ' export title in DataGridView Dim Cols as Integer for Cols = 1 to DataGrid.Columns.Count xlapp.cells (1, Cols) = Datagrid.columns (Cols-1). HeaderText Next ' exports data in DataGridView by line Dim i As Integer for i = 0 to datagrid.rowcount-1 Dim J A S Integer for j = 0 to Datagrid.columncount-1 ' determines whether the data in the DataGridView is exported if Me.datagrid (j, i). Value is System.DBNull.Value then xlapp.cells (i = 2, j = 1) = "" " Else xlapp.cells (i + 2, j + 1) = DataGrid ( J, i). Value.tostring End If next Next
So that we can achieve the export of Excel function, know the principle, and then encounter similar problems will be easy to solve
Iii. Summary
Such technology will need to go back to study after implementation, the impression and experience are more profound
Vb. NET Exporting Excel Principles