一聚教程网:一个值得你收藏的教程网站

热门教程

vb.net 数据导入excel代码

时间:2022-06-25 05:35:22 编辑:袖梨 来源:一聚教程网

vb.net 数据导入excel代码
这是一款简单的由vb.net把数据库教程的数据导入到execel实例哦,如果你正在找相关的.net把数据导入到数据表的话,这是一款完整的可用代码。

imports system.data
imports system.data.sqlclient

public class form1
  dim app as new excel.application
  dim exbook as excel.workbook
  dim exsheet as excel.worksheet
  dim exrange as excel.range

  private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
  exbook = app.workbooks.open("d:www.111com.netc.xls")
  exsheet = exbook.sheets(1)

  dim myconnection as new sqlconnection("data source=192.168.0.1;initial catalog=test;user id=sa;password=abc")
  dim mycommand as sqlcommand = new sqlcommand()
  myconnection.open()
  mycommand.connection = myconnection
  mycommand.commandtext = "select un001,un002,un003 from bcun"
  mycommand.commandtype = commandtype.text

  dim myreader as sqldatareader = mycommand.executereader

  dim i as integer = 1
  do while myreader.read()

  exsheet.cells(i, 1) = myreader.getstring(0)
  exsheet.cells(i, 2) = myreader.getstring(1)
  exsheet.cells(i, 3) = myreader.getstring(2)
  i = i + 1
  loop
  myreader.close()
  myconnection.close()

  exbook.save()
  exbook.close()
  app.workbooks.close()
  app.quit()
  mycommand.connection.close()

  end sub
end class
/*
请注意在引用中添加com组件:microsoft excel 11.0 object library(不一定是11.0的,由你用的excel版本来定)

热门栏目