You must display the data in an excel table and set the data format. Even so, it takes a lot of time to implement your own functions. I believe many people like me need to implement this function. Let's make a summary. Correct the error.
1. Add reference -- com --- microsoft excel 11.0 object library
2. Define when you need to access excel
Dim exapp as excel. application 'defines excel applications
Dim exbook as excel. workbook 'defines a workbook
Dim exsheet as excel. worksheet 'defines a worksheet
Dim exrange as excel. range' defines the Work Area
3. With the above definition, the excel operations are basically captured.
Exapp. visible = true' show excel program
Exbook = exapp. workbooks. add () 'add a new workbook or exbook = exapp. workbooks. open ("thin name")/open an existing workbook
Exsheet = exbook. sheets (n) 'gets the control handle of the nth worksheet, which will be processed later
Exsheet. cells (row, col) = value 'is used to assign values to a specified cell. This operation is frequently used.
The exsheet. range (cells, cells). font property is also commonly used. The setting format is:
Exsheet. range (cells, cells). font. colorindex = 3' set the color
Exsheet. range (cells, cells). Borders. LineStyle = 1 'set the border line
Exsheet. range (cells, cells). EntireColumn. AutoFit () 'automatic column width/can be changed to automatic Row Height.
Exsheet. range (cells, cells). merge' merge cells. This is also used for complicated tables.
Exsheet. range (cells, cells). HorizontalAlignment = 3' horizontal center, of course, you can also change to vertical
ExSheet. PageSetup. Orientation = 2' horizontal page
With these skills, you can basically achieve some requirements.
4. The last step is to save the file.
Exsheet. saveas ("file name ")
Or
Exbook. save ()
5. Clear Variables
exsheet=nothing
exbook.close
exbook=nothing
exapp.quit
exapp=nothing