Import data to Excel from Datagride in VB

Source: Internet
Author: User
Tags integer range
datagrid|excel| Import Data
The program needs, I looked for a long time on the internet to find, just to write the program, and added a note, I hope to help you
Reference before you write the program
Microsoft Excel 11.0 Object Library
I see the article is 10.0, I this is office2003 is 11.0, as long as the Excel has this quote ~
Private Sub Cmdsave_click ()
MsgBox "File Save as: D:\ Power network distribution line management Information system \ Information Query results \ Accident Information query results. xls"
Dim I as Integer
Dim J as Integer
Dim ex as Object
Dim Exwbook as Object
Dim Exsheet as Object

Set ex = CreateObject ("Excel.Application")
Set Exwbook = Nothing
Set Exsheet = Nothing
Set Exwbook = ex. Workbooks (). Add
Set Exsheet = Exwbook. Worksheets ("Sheet1")

Table headers in Excel, which is based on my need to add headers
Ex. Range ("C3"). Value = "Date"
Ex. Range ("D3"). Value = "Time"
Ex. Range ("E3"). Value = "Site"
Ex. Range ("F3"). Value = "Report Person"
Ex. Range ("G3"). Value = "line Double number"
Ex. Range ("H3"). Value = "Protect action type"
Ex. Range ("i3"). Value = "Cause of accident"
Ex. Range ("J3"). Value = "Process Owner"
Ex. Range ("K3"). Value = "Processing Method"
Ex. Range ("L3"). Value = "process result"
Ex. Range ("M3"). Value = "End Time"
Ex. Range ("N3"). Value = "Remarks"
I use loops to add all the data to the number of records
For i = 1 to Adodc1.Recordset.RecordCount
j = 3 + I
K is the number of data columns
For k = 0 to 11
Transform cell positions by using variables K and J
Q = Chr (+ k) & J
Put the DATAGRID1 data into the cell then cell
Ex. Range (q). Value = Datagrid1.columns (k)
Next K
The pointer moves down
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
End If
Next I

Save input to accident information query results. xls
Exwbook. SaveAs "d:\ power grid distribution line management Information system \ Information Query results \ Accident Information query results. xls"
Exit Excel
Ex. Quit
End Sub
I think this program is very simple and very easy to use, do not open the accident information query results. xls or you'll get an error.
The system automatically prompts for the replacement of the file when it is stored, based on its own hair

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.