Export dataset to excel

Source: Internet
Author: User
Tags rtrim
My friend asked me to write the program that just solved the import problem yesterday and completed filtering, sorting, adding, deleting, searching, replacement, and so on. Now I want to export the program. Users can answer and query the information to export the data:
The Code is as follows:
You must first reference the Excel component.
Here, mydata is my dataset information. Fill in the information line by line, and finally output.

Try
Dim selemployeecontract as string = "select * from *"
Dim dsemployeecontract as dataset = new dataset
Dsemployeecontract = mydata
If dsemployeecontract is nothing then
Msgbox ("no data, cannot export! ", Msgboxstyle. okonly)
Exit sub
Else
Dim xlsapp as new excel. Application
Xlsapp. workbooks. Add ()
Xlsapp. Visible = true
Xlsapp. selection. Merge ()

Dim I as int16
For I = 1 to dsemployeecontract. Tables (0). Columns. Count
Xlsapp. cells (1, I) = dsemployeecontract. Tables (0). Columns (I-1). columnname
Next
Dim rowindex as integer = 2
Dim colindex as integer
Dim Col as datacolumn
Dim row as datarow
Dim nxh as integer = 1
For each row in dsemployeecontract. Tables (0). Rows
Colindex = 1
For each Col in dsemployeecontract. Tables (0). Columns
If colindex = 1 then
Xlsapp. cells (rowindex, colindex) = rtrim (convert. tostring (row (Col. columnname )))
Else
Xlsapp. cells (rowindex, colindex) = rtrim (convert. tostring (row (Col. columnname )))
End if
Colindex + = 1
Next
Rowindex + = 1
Nxh + = 1
Next
Xlsapp. range (xlsapp. cells (2, 1), xlsapp. cells (dsemployeecontract. tables (0 ). rows. count + 2, dsemployeecontract. tables (0 ). columns. count )). font. size = 9
Xlsapp. range (xlsapp. cells (2, 6), xlsapp. cells (dsemployeecontract. tables (0 ). rows. count + 2, 4 )). numberformat = "yyyy-mm-dd"
Xlsapp. range (xlsapp. cells (2, 7), xlsapp. cells (dsemployeecontract. tables (0 ). rows. count + 2, 5 )). numberformat = "yyyy-mm-dd"
Xlsapp. Columns. autofit ()
End if
Catch ex as exception
Msgbox (ex. tostring (), msgboxstyle. Critical)
End try

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.