Use excel Macro export data from database
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的更多相关文章
- 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 ...
- Export Data from mysql Workbench 6.0
原文地址:export-data-from-mysql-workbench-6-0 问题描述 I'm trying to export my database, using MySQL Workben ...
- SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer
SQL Server 2014新特性:五个关键点带你了解Excel下的Data Explorer Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企 ...
- 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 ...
- How to export data from Thermo-Calc 如何从Thermo-calc导出文本数据
记录20180510 问题:如何从thermo-calc导出文本数据供origin绘图? 解决: In Thermo-Calc graphical mode, you can just add a ' ...
- 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 ...
- [转]Introduction - Run Excel Macro using VBScript
本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...
- C# Note38: Export data into Excel
Microsoft.Office.Interop.Excel You have to have Excel installed. Add a reference to your project to ...
- SQL Server 2014 BI新特性(一)五个关键点带你了解Excel下的Data Explorer
Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企业中的自助式的商业智能变得更加的灵活,从而也降低了商业智能的门槛. 此文是在微软商业智能官方博客里 ...
随机推荐
- HDU 2817 A sequence of numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2817 __int64 pow_mod (__int64 a, __int64 n, __int64 m)快速幂取 ...
- 【HTML】Advanced6:HTML5 Forms Pt. 1: Input Types
1.Not yet work fully on all major browsers 2.<input type="search tel url email datetime date ...
- Windows Azure 的磁盘管理相关概念
在 Windows Azure 的虚拟机中,磁盘有多种使用方式.操作系统磁盘是用来为虚拟机提供操作系统的虚拟硬盘.数据磁盘是附加到虚拟机上用来存储应用程序数据的 VHD. 根据应用程序的需要,可从多种 ...
- WordPress更新服务加快收录
WordPress更新服务的设置在后台"设置"-"撰写".更新服务下的文本框中默认为http://rpc.pingomatic.com/.该地址网站Ping-o ...
- 【解决】python2.x版本的Django下admin管理页面css无效
折腾一下午,终于解决了这个问题,有必要记录一下,我就奇怪了为什么实验室电脑没问题,到宿舍就挂掉了,哼 主要是改mimetypes文件,位于D:\MySoftware\Python27\Lib下 1.添 ...
- HW4.38
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 分布式定时任务框架比较,spring batch, tbschedule jobserver
分布式定时任务框架比较,spring batch, tbschedule jobserver | 移动开发参考书 分布式定时任务框架比较,spring batch, tbschedule jobser ...
- C# WinForm多线程(三)---- Control.Invoke[转]
开发中遇到更新Winform中控件假死问题,通过看了这篇文章了解了原理,感谢!此处标记起来,以备查看! 原文地址:http://www.cnblogs.com/joechen/archive/2009 ...
- Codeforces Round #382 (Div. 2)E. Ostap and Tree
E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译)
# 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译) - 原文 <https://githu ...