DataGridView, translated into Chinese means data table display, using the DataGridView control, you can display and edit tables from different types of data sources, binding data to DataGridView controls is very simple and intuitive, in most cases, Just set the DataSource property, and when you bind to a database source that contains multiple lists or tables, you only need to set the DataMember property to the bound list or to the string of the table. Computer room toll system used to display data tables, and exported to Excel table, the first computer room charge system is a VB version, she exported Excel method as follows:
Open vb-Project-apps-Check Microsoft Excel 14.0 objexts 2.6 Library
The code is as follows:
<span style= "FONT-SIZE:18PX;" >private Sub Cmdexport_click () Dim J As Integer Dim xlapp As Excel.Application Dim xlbook As Excel.workboo K Dim xlsheet as Excel.Worksheet Set xlapp = CreateObject ("Excel.Application") ' instantiation of Object xlapp xlapp.visible = True Set xlbook = XlApp.Workbooks.Add Set xlsheet = Xlbook.worksheets (1) for i = 0 to Myflexgr Id. Rows-1 for j = 0 to Myflexgrid. Cols-1 Myflexgrid. Row = i myflexgrid. Col = J Xlsheet.cells (i + 1, j + 1) = Trim (Myflexgrid. Text) next next End Sub </span>
The second edition of the computer room fee system vb.net version, she how to export exce form NIE, and VB version of the way to export Excel table, and there is not the beauty of VB it? Here are the following:
First, we need to quote
One more detail, we need to change the Allowusertoaddrow attribute to False, followed by the difference between true and false:
The programming code is as follows:
<span style= "FONT-SIZE:18PX;" >public Function Dirivexcel (ByVal DGV as DataGridView) as Boolean Dim myexcel as New Microsoft.Office.Interop.Exce L.application ' Establish Excel connection MyExcel.Application.Workbooks.Add (true) myexcel.visible = True Dim I, J, K As Integer ' defines the variable for k = 0 to DGV. ColumnCount-1 ' Add header myexcel.cells (1, k + 1) = DGV. Columns (k). HeaderText Next k for i = 0 to DGV. RowCount-1 ' Adds the data in the control through a loop to the table for j = 0 to DGV. ColumnCount-1 ' Because the first row is a table header, adding data begins with adding myexcel.cells (i + 2, j + 1) = DGV (J, I) from the second line. Value next Next return true ' add complete, return true End function</span>
Now let's look at the results of the operation:
Allowusertoaddrow The following error occurs when this property is changed to true:
Because the value of the cell is empty, a control is performed. The System.NullReferenceException exception occurs when the ToString () operation. For this I will remove the ToString, will not error, in addition you can be the value of the advanced line is empty to judge if not empty, in the ToString () operation, the above is in the code to modify the basis to solve the problem, in addition to the control itself has a property control, Whether to add blank lines automatically. Vb. NET Edition room charge system, not finished, to be continued ...