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