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. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  2. 【转】Installing OpenCV on Debian Linux

    In this post I will describe the process of installing OpenCV(both versions 2.4.2 and 2.4.3) on Debi ...

  3. FFT矩阵

    举个例子: \[{F_4}=\left[{\begin{array}{*{20}{c}}1&1&1&1\\1&i&{-1}&{-i}\\1&{- ...

  4. 【HTML】Advanced4:Accessible Links

    1.Tab <ul> <li><a href="here.html" tabindex="1">Here</a> ...

  5. oracle 临时表学习

    临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...

  6. 【wuzhicms】apache 设置禁止访问某些文件或目录

    [apache配置禁止访问] 1. 禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$&qu ...

  7. HW2.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. Nyoj42 一笔画问题 (欧拉道路)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=42题目链接 #include <cstdio> #include <cstring ...

  9. javascript如何判断一个对象是否是窗口

    <!DOCTYPE html> <html> <head> </head> <body> <script type="tex ...

  10. Video Surveillance - POJ 1474(判断是否存在内核)

    题目大意:询问是否在家里装一个监视器就可以监控所有的角落. 分析:赤裸裸的判断多边形内核题目. 代码如下: #include<iostream> #include<string.h& ...