Sub DownLoadMacro()  '定义过程名称
Dim i As Integer, j As Integer, sht As Worksheet 'i,j为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New ADODB.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Dim wshnetwork, info As String Set wshnetwork = CreateObject("WScript.Network")
info = wshnetwork.UserName strCn = "Provider=sqloledb;Server=IP;Database=DATABASENAME;Uid=SA;Pwd=******;"
'定义数据库链接字符串 '下面的语句将读取数据表数据,并将它保存到excel工作表中
strSQL = "SELECT dbo.V_Export.SKU,dbo.V_Export.Model,dbo.V_Export.CCC,dbo.V_Export.CertExpiryDate,dbo.V_Export.Permission,dbo.V_Export.CNW1,dbo.V_Export.CNW2, CASE When dbo.V_Export.PhaseOut ='O2' THEN 'Y' ELSE '' END AS PhaseOut,dbo.V_Export.SatetyStock,dbo.V_Export.Leadtime,dbo.V_Export.[DESC] FROM dbo.V_Export ORDER BY dbo.V_Export.SKU ASC, dbo.V_Export.SatetyStock DESC, dbo.V_Export.Leadtime DESC " '定义SQL查询命令字符串
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn
If cn = "" Then MsgBox ("Connect Faild") '提示连接数据库失败 Else rs.Open strSQL, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中
i =
Set sht = ThisWorkbook.Worksheets("CacuFromServer") '把sht指向当前工作簿的CacuFromServer工作表
sht.Range("A2:I65536").ClearContents
Do While Not rs.EOF '当数据指针未移到记录集末尾时,循环下列操作
sht.Cells(i, ) = rs("SKU") '把当前记录的字段1的值保存到CacuFromServer工作表的第i行第1列
sht.Cells(i, ) = rs("DESC")
sht.Cells(i, ) = rs("Model") '把当前字段2的值保存到CacuFromServer工作表的第i行第3列
sht.Cells(i, ) = rs("CCC") '把当前记录的字段3的值保存到CacuFromServer工作表的第i行第4列
sht.Cells(i, ) = rs("CertExpiryDate") '把当前记录的字段4的值保存到CacuFromServer工作表的第i行第5列
sht.Cells(i, ) = rs("Permission") '把当前字段5的值保存到CacuFromServer工作表的第i行第6列
sht.Cells(i, ) = rs("SatetyStock") '把当前字段7的值保存到CacuFromServer工作表的第i行第7列
sht.Cells(i, ) = rs("Leadtime") '把当前字段8的值保存到CacuFromServer工作表的第i行第8列
sht.Cells(i, ) = rs("PhaseOut") '把当前字段9的值保存到CacuFromServer工作表的第i行第9列 '
sht.Cells(i, ) = rs("CNW1") '把当前记录的字段10的值保存到CacuFromServer工作表的第i行第10列
sht.Cells(i, ) = rs("CNW2")
sht.Cells(i, ) = rs("SKU")
rs.MoveNext '把指针移向下一条记录
i = i + 'i加1,准备把下一记录相关字段的值保存到工作表的下一行
Loop '循环
rs.Close '关闭记录集,至此,程序将把某数据表的字段1到字段6保存在excel工作表CacuFromServer的第1到6列,行数等于数据表的记录数 strSQL = "insert into dbo.DIM_LogInfo values('" & info & "',getdate())" cn.Execute strSQL '执行strSQL所含的SQL命令 strSQL = "select max(exportDate) as ExportFinalDate from dbo.DIM_LogInfo"
rs.Open strSQL
Set sht = ThisWorkbook.Worksheets("Search") '把sht指向当前工作簿的CacuFromServer工作表
sht.Cells(, ) = rs("ExportFinalDate")
rs.Close cn.Close '关闭数据库链接,释放资源 MsgBox ("Download Succeed") '提示导出成功 End If End Sub

Use excel Macro export data from database的更多相关文章

  1. NetSuite SuiteScript 2.0 export data to Excel file(xls)

    In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...

  2. Export Data from mysql Workbench 6.0

    原文地址:export-data-from-mysql-workbench-6-0 问题描述 I'm trying to export my database, using MySQL Workben ...

  3. SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer

    SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企 ...

  4. xlwings: Write Excel macro using python instead of VBA

    i want to write Excel macros to deal with the data, but i am not familiar with VBA language. so i de ...

  5. How to export data from Thermo-Calc 如何从Thermo-calc导出文本数据

    记录20180510 问题:如何从thermo-calc导出文本数据供origin绘图? 解决: In Thermo-Calc graphical mode, you can just add a ' ...

  6. 1.3 Quick Start中 Step 7: Use Kafka Connect to import/export data官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 7: Use Kafka Connect to import/export ...

  7. [转]Introduction - Run Excel Macro using VBScript

    本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...

  8. C# Note38: Export data into Excel

    Microsoft.Office.Interop.Excel You have to have Excel installed. Add a reference to your project to ...

  9. SQL Server 2014 BI新特性(一)五个关键点带你了解Excel下的Data Explorer

    Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企业中的自助式的商业智能变得更加的灵活,从而也降低了商业智能的门槛. 此文是在微软商业智能官方博客里 ...

随机推荐

  1. wuzhicms 发送邮件

    发送邮件 //邮件发送 $config = get_cache('sendmail'); $siteconfigs = get_cache('siteconfigs'); $password = de ...

  2. [E120L][KitKat][4.4.2][CM11] CM11 rom+ google app安装心得

    1. 本文针对 三星手机型号: E120L  即 samsung galaxy S2 HD LTE 2. 本文安装的android ROM 的内核为4.4.2 KitKat 3. 本文安装的第三方RO ...

  3. CF402E Strictly Positive Matrix 传递闭包用强连通分量判断

    题目链接:http://codeforces.com/problemset/problem/402/E /**算法分析: 这道题考察了图论基本知识,就是传递闭包,可以构图用强联通分量来判断 */ #i ...

  4. 开始使用storm

     开始使用storm 本章将讲述如何安装.部署.启动和停止 Storm 集群. Storm 的安装比较简单,但在安装 Storm 之前需要做好充足的准备,本章将介绍安装的整个流程.在官网上可以下载到S ...

  5. excel函数

    120.623652,31.386228 120.623652 31.386228 上面数据要分成两列数据,我用了函数 =LEFT(C4,FIND(",",C4)-1),=RIGH ...

  6. opencv数据结构-MAT结构详解

    1.定义 OpenCV中的C结构体有 CvMat 和 CvMatND,但后续的应用中指出 CvMat 和 CvMatND 弃用了,在C++封装中用 Mat 代替,另外旧版还有一个 IplImage,同 ...

  7. 立体视觉-opencv中立体匹配相关代码

    三种匹配算法比较 BM算法: 该算法代码: view plaincopy to clipboardprint? CvStereoBMState *BMState = cvCreateStereoBMS ...

  8. SweetAlert 使用

    $(".delete").click(function(){ var work_name = $(this).data('name'); var item_id = $(this) ...

  9. Unity手游之路<六>游戏摇杆之Easy Touch 3教程

    之前已经介绍过Unity自带的摇杆Joystick,它用起来很简单.但是它也存在很多局限,不能全部满足普通mmo手游的一些需求,例如:要能方便地更好素材:能指定在某个区域显示,或者只有在该区域触摸时才 ...

  10. hdu 1039 Easier Done Than Said? 字符串

    Easier Done Than Said?                                                                     Time Limi ...