In the IDC charging system, you need to query the recharge amount. Select a date segment and click "query" to display the information in the mshflexgrid (here, this control is named mshfgrecharge_records) control (see --Figure 1). How can I export the displayed information to excel?
Figure 1
Export Method(General idea ):
1. Excel file: Microsoft Excel 14.0 ojbect Library
2. Create an application object: setxlsapp = Createobject ("Excel. application ")
3. Use application and its sub-objects: export the records in mshflexgrid to excel
4. Close the application object: Set xlsapp = nothing
Code Section:
Private sub into to_excel_click () dim xlsapp as Excel. application 'stores the created Excel Object dim xlsbook as Excel. workbook 'stores the created Excel Workbook file dim xlssheet as Excel. worksheet stores the cycle variables in the Set active worksheet dim intforrow as integer for loop statement, used to control the cycle variables in the row dim intforcol as integer for loop statement, used to control the column set xlsapp = Createobject ("Excel. application ") 'create an Excel object set xlsbook = xlsapp. workbooks. add'create an Excel Workbook file set xlssheet = xlsbook. worksheets (1) 'set the worksheet 'to write data in the table to the EXCEL object. For intforrow = 0 to forward-1 for intforcol = 0 to mshfgrecharge_records.cols-1 If intforcol = 0 then, show it as a character. Otherwise, the "0" before the student ID will not display xlssheet. cells (intforrow + 1, intforcol + 1) = "'" & mshfgrecharge_records.textmatrix (intforrow, intforcol) else xlssheet. cells (intforrow + 1, intforcol + 1) = mshfgrecharge_records.textmatrix (intforrow, intforcol) end if next intforcol next intforrow xlsapp. visible = true' show Excel Set xlsapp = nothing 'Return Control to excelend sub
Note::
In the first image, the card number starts with 0. If the data in the table is directly exported to excel, 0 in the card number is not displayed (seeFigure2). Note: The exported data is in non-simplified format by default. This is equivalent to entering a number in the Excel cell. The default value is Numeric. How to display 0? Of course, it must be converted into characters. Therefore, the code above, in the for loop statement, uses an "if" statement to select: as long as it is the first line of data, add an English single quotation mark "'" in front. In Excel, the data in the first cell column is converted to the simplified type, and the disappearing 0 is miraculously reproduced (seeFigure 3)!
Figure 2
Figure 3