记录集导出到Excel方法
 

Public Function ExportToExcel(RSrecord As ADODB.Recordset, Titles_Name)
'==================================================
'参数说明
'RSrecord :记录集
'titles_name 表头名称
'==================================================
On Error GoTo ERRCL
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Long
Dim Icolcount As Long

Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable

' 假设Rs_Data 是你的记录集
With RSrecord
If .RecordCount < 1 Then
MsgBox "没有可导出的记录!", vbInformation + vbOKOnly, "提示"
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With

Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True

'添加查询语句,导入EXCEL数据

Set xlQuery = xlSheet.QueryTables.Add(RSrecord, xlSheet.Range("a2"))
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 8)).Merge
xlSheet.Cells(1, 1).HorizontalAlignment = xlCenter
xlSheet.Cells(1, 1) = Titles_Name
With xlQuery
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With

xlQuery.FieldNames = True '显示字段名
xlQuery.Refresh
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "宋体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
'标题字体加粗
.Range(.Cells(2, 1), .Cells(Irowcount + 2, Icolcount)).Borders.LineStyle = xlContinuous
'设表格边框样式

' .PageSetup.PaperSize = xlPaperA4 '
' .PageSetup.PrintGridlines = True
End With
xlApp.Application.Visible = True

Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
Set Rs_Data = Nothing
Exit Function
ERRCL: MsgBox "无有效数据或 Excel 2000 未安装!", vbInformation, "错误"
End Function

记录集导出到Excel方法的更多相关文章

  1. 【MVC】 非常简单的页面导出 WORD, EXCEL方法

    [MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...

  2. 导出word excel 方法

    ---导出excel public static void DataTableToExcelXjd(DataTable dt, string Title, string TmpColsName) { ...

  3. c# 数据导出成excel 方法总结 见标红部分

    public void ServiceOrderExport(string data) { StringBuilder sb = new StringBuilder(); Type entityTyp ...

  4. vb.net-三种将datagridview数据导出为excel文件的函数

    第一种方法较慢,但是数据格式都比较好,需要引用excel的 Microsoft.Office.Interop.Excel.dll  office.dll #Region "导出excel函数 ...

  5. 第七章 Odoo 12开发之记录集 - 使用模型数据

    在上一篇文章中,我们概览了模型创建以及如何从模型中载入和导出数据.现在我们已有数据模型和相关数据,是时候学习如何编程与其进行交互 了.模型的 ORM(Object-Relational Mapping ...

  6. .NET导入导出Excel方法总结

    最近,应项目的需求,需要实现Excel的导入导出功能,对于Web架构的Excel导入导出功能,比较传统的实现方式是: 1)导入Excel:将Excel文件上传到服务器的某一文件夹下,然后在服务端完成E ...

  7. DataGridView导出到Excel的三个方法

    #region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...

  8. MySQL要导出成excel的方法

    MySQL 要导出成 excel 文件很简单,执行类似这样的命令:   select * from 某个表 into outfile  'd:/文件名.xls';   上述命令你在服务器上执行,就导在 ...

  9. Winform 中 dataGridView 导出到Excel中的方法总结

    最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个  DataGridV ...

随机推荐

  1. BSGS求解离散对数问题

    离散对数问题是求解axΞb mod(n) 同余方程 以下模板使用于gcd(a,n)=1的情况 ; int hs[mod],head[mod],Next[mod],id[mod],top; void i ...

  2. itchat 爬了爬自己的微信通讯录

    参考 一件有趣的事: 爬了爬自己的微信朋友 忘记从谁那里看到的了,俺也来试试 首先在annconda prompt里面安装了itchat包 pip install itchat 目前对python这里 ...

  3. ubuntu set up 2 - 双系统时间问题

    http://ubuntuhandbook.org/index.php/2016/05/time-differences-ubuntu-1604-windows-10/ For users who d ...

  4. PP: Sequence to sequence learning with neural networks

    From google institution; 1. Before this, DNN cannot be used to map sequences to sequences. In this p ...

  5. bootstrap-datatimepicker插件的使用

    1.引入相关的css.js文件(因为是bootstrap的,首先应该引入bootstrap.css和bootstrap.js): <link rel="stylesheet" ...

  6. 自定义Nginx返回页面

    1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...

  7. 2020牛客寒假算法基础集训营6 I.导航系统 (最小生成树)

    https://ac.nowcoder.com/acm/contest/3007/I 题中给定的图必定是一棵树 容易发现,如果将输入的N(N-1)个距离看做N(N-1)条无向边的话,那么如果数据合法, ...

  8. Linux終端一行命令发送邮件

    近期由于经常需要给别人发送邮件,每次都要打开QQ邮箱觉得非常麻烦.想到Linux终端可以自定义命令,加上python可以实现邮件发送功能,于是自己写了一个终端send + 文件地址的命令. 首先贴上p ...

  9. Three.js的开始(附代码)_2

    1 下载Three.js代码 https://github.com/mrdoob/three.js/tree/master/build 2 引用方法 在HTML中添加以下代码: <script ...

  10. Jmeter-功能概要

    1.Jmeter工具组成部分 (1)资源生成器:用于生成测试过程中服务器.负载机的资源代码.(LR中的VuGen) (2)用户运行器:通常是一个脚本运行引擎,根据脚本要求模拟指定的用户行为.(LR中的 ...