机房收费系统————导出到Excel
2015-06-21 19:23
441 查看
机房收费系统中有很多之前在敲学生的时候没有接触到的功能,遇到的第一个陌生的就是把数据导出到Excel中,那么这个功能是怎么实现的呢?
首先,在VB中“工程”——>”引用"中添加引用
如果没有这个选项,单击右边的浏览,找到路径:c:\ Program Files \ Microsoft Office \ Office 15 下的 EXCEL.exe 添加就可以了。(这个方法可能会因为电脑的不同有所差异,不一样的话自己研究一下就好了,总之就是要添加引用)
接下来就是通过代码实现功能,大体上有两种实现方法
法一:数据从VB控件 MSHFlexGrid 中导出
法二:数据从SQL Server数据库的记录中导出
(第一次在csdn上写,还有点小激动呢)
首先,在VB中“工程”——>”引用"中添加引用
如果没有这个选项,单击右边的浏览,找到路径:c:\ Program Files \ Microsoft Office \ Office 15 下的 EXCEL.exe 添加就可以了。(这个方法可能会因为电脑的不同有所差异,不一样的话自己研究一下就好了,总之就是要添加引用)
接下来就是通过代码实现功能,大体上有两种实现方法
法一:数据从VB控件 MSHFlexGrid 中导出
Private Sub cmdExport_Click() Dim i As Integer Dim j As Integer On Error Resume Next If myflexgrid.TextMatrix(1, 0) = "" Then MsgBox "没有数据导出", vbInformation, "提示" Exit Sub End If Dim excelApp As Excel.Application Set excelApp = New Excel.Application Set excelApp = CreateObject("excel.application") Dim exbook As Excel.Workbook Dim exsheet As Excel.Worksheet Set exbook = excelApp.Workbooks.Add excelApp.SheetsInNewWorkbook = 1 excelApp.Visible = True Me.MousePointer = vbHourglass With excelApp.ActiveSheet For i = 1 To myflexgrid.Rows For j = 1 To myflexgrid.Cols .Cells(i, j).Value = "" & Format$(myflexgrid.TextMatrix(i - 1, j - 1)) Next j Next i End With Me.MousePointer = 0 Set exsheet = Nothing Set exbook = Nothing Set excelApp = Nothing End Sub
法二:数据从SQL Server数据库的记录中导出
Private Sub cmdExport_Click()
Dim i As Integer Dim txtSQL As String Dim MsgText As String Dim mrc As ADODB.Recordset Dim x1app1 As Excel.Application Dim x1book1 As Excel.Workbook Dim x1sheet1 As Excel.Worksheet Set x1app1 = CreateObject("excel.application") Set x1book1 = x1app1.Workbooks.Add Set x1sheet1 = x1book1.Worksheets(1) txtSQL = "select cardNo,Date,time,CancelCash,UserID,status from CancelCard_Info where date between '" & Trim(DTPicker1.Value) & "' and '" & Trim(DTPicker2.Value) & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) For i = 0 To mrc.Fields.Count - 1 x1sheet1.Cells(1, i + 1) = mrc.Fields(i).Name Next i If Not mrc.EOF Then mrc.MoveFirst x1sheet1.Range("A2").CopyFromRecordset mrc mrc.Close End If Set mrc = Nothing x1app1.Visible = True Set x1app1 = Nothing
End Sub
(第一次在csdn上写,还有点小激动呢)
相关文章推荐
- 初步C++类模板学习笔记
- CocoaPosd的使用详解
- 算法练习:矩阵清零
- 我喜欢竞争系列1
- 微信公众平台开发_OAuth2.0网页授权
- uva 11374 Airport Express(Dijkstra)
- TCP/IP协议族-----8、地址解析协议(ARP)
- Win8.1安装VirtualSVN Server发生service visualSVN Server failed to start解决办法
- 使用touch事件实现手机网页右侧菜单栏拖动显示隐藏
- hdu 5273 Dylans loves sequence
- 时间编程--gettimeofday(获取高精度时间)
- POJ 1328 Radar Installation(贪心)
- 时间编程--asctime(以字符串方式显示时间)
- 在mvc中实现图片验证码的刷新
- iOS 音乐类App必备功能:后台播放、锁屏封面、远程播放控制
- 时间编程 --localtime(获取本地时间)
- 架构师速成7-高中
- 架构师速成7-高中 分类: 架构师速成 2015-06-21 19:17 132人阅读 评论(0) 收藏
- 第15周-阅读命名空间的使用
- 时间编程--获取格林威治时间 gmtime