How to export data from the VB. NET dataset to excel

Source: Internet
Author: User

1. First, right-click "Reference" in "project" in "solution" and introduce the Exel component in the COM component, as shown in: (versions may be different, which does not affect, I installed Office 2003)

2. Program To add the following statement:
Imports Microsoft. Office. InterOP
3. Create a new process function and output it to excel:
Private sub Button2_click ( Byval Sender As System. object, Byval E As System. eventargs) Handles Button2.click
Dim Myexcel As New excel. Application
Myexcel. Visible = True
Myexcel. workbooks. Add ()
Dim Col As Integer
Dim Row As Integer
Dim Col1 As Integer
Myexcel. worksheets ("sheet1"). Activate ()
'The following is a column name for each column that is output to an Excel table.
Myexcel. cells (1, 1). value = "unit name"
Myexcel. cells (1, 2). value = "unit type"
Myexcel. cells (1, 3). value = "office address"
Myexcel. cells (1, 4). value = "mailing address"
Myexcel. cells (1, 5). value = "office phone"
Myexcel. cells (1, 6). value = "membership payment"
Myexcel. cells (1, 7). value = "payment amount"
'Next, We will output the data in the dataset to each column in Excel. My dataset is "dsresults", which can be modified as appropriate for each person.
For Row = 0 To Dsresults. Tables ("Results"). Rows. Count-1
For Col = 1 To Dsresults. Tables ("Results"). Columns. Count-1
Myexcel. cells (row + 2, col). value = dsresults. Tables ("Results"). Rows (ROW) (COL)
Next
'Each column in Excel is automatically adjusted based on the filled data
Myexcel. activesheet. Columns. autofit ()
Next
End sub
4. The final output result is:

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.