DataSet results set to Excel

zhaozj2021-02-16  124

Imports system.data

Imports system.data.oledb

Public Class Form1 Inherits System.Windows.Forms.Form Private connstr, selectcmd As String Private conn As OleDbConnection Private cmd As OleDbCommand Private myreader As OleDbDataReader Private Sub button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1. Click Dim excelApp As Object excelApp = CreateObject ( "Excel.Application") Dim excelBook As Object excelBook = excelApp.Workbooks.Add Dim excelWorksheet As Object excelWorksheet = excelBook.Worksheets (1) excelApp.Visible = False SaveFileDialog1.Title = "results save as "SaveFileDialog1.Filter =" excel * .xls | * .xls "If SaveFileDialog1.ShowDialog = DialogResult.OK Then If SaveFileDialog1.FileName <>" "Then Dim n as Integer Dim i as Integer = 2 With excelWorksheet Do While myreader .Read () for n = 0 to myReader.fieldcount - 1 .Range (CHR (65 N) & "1"). Value = myreader.getname (n) .range (chr (65 n) & "1"). ColumnWidth = 15.range (CHR (65 N) & I.to = myreader.Item (n) Next N i = 1 loop end with excelBook.saveas (SavefileDialog1.filename) end if EndiffElapp.quit () Excelapp = Nothing Msgbox ("Congratulations") Conn .close () End Sub

转载请注明原文地址:https://www.9cbs.com/read-8859.html

New Post(0)