当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 如何在VB中操作EXCEL(一段代码,两个可以使用的过程)

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 如何在VB中操作EXCEL(一段代码,两个可以使用的过程)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 65 ::
收藏到网摘: n/a


工程引用说明:本代码的使用是基于Microsoft Excel 2003使用的,未在其它版本的Office上测试过,因此在VB中应当引用Microsoft Excel 11.0
代码其它内容说明:本代码中使用了VsFlexGrid做为源数据;并且可以命名EXCEL 工作单(SHEET)的名称,其中第一段代码是将内容保存到一个新的EXCEL 工作簿中,而第二个则是将内容保存到一个已存在的工作簿中。

为了显示进度,我使用了一个显示进度的窗体,frmPBar,可以去掉相关的该段代码。

Public Sub GridToExcel(srcGrid As VSFlexGrid, shName As String)
'将Grid中的数据导出到Excel表格中
Dim i As Integer
Dim j As Integer

Dim appXL As Variant
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim rng, rng1, rng2 As Excel.Range

On Error GoTo errhandler

Set appXL = CreateObject("Excel.Application")
Set wb = appXL.Workbooks.Add()

wb.Activate

Set sh = wb.Worksheets.Add()
sh.Name = shName

frmPBar.Caption = "正在导出数据,请稍候......"
frmPBar.Show

For i = 0 To src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Rows - 1
For j = 1 To src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Cols - 1

sh.Cells(i + 1, j) = src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Cell(flexcpText, i, j)
DoEvents
Next j
Next i

Unload frmPBar

appXL.Visible = True
Exit Sub
errhandler:
MsgBox Err.Description

End Sub

Public Sub GridToExistExcel(srcGrid As VSFlexGrid, fileName As String, shName As String)
'将Grid中的数据导出到一个指定文件的Excel表格中
Dim i As Integer
Dim j As Integer

Dim appXL As Variant
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim rng, rng1, rng2 As Excel.Range

On Error GoTo errhandler

Set appXL = CreateObject("Excel.Application")
'Set wb = appXL.Workbooks.Add()
Set wb = appXL.Workbooks.Open(fileName)
wb.Activate

Set sh = wb.Worksheets.Add()
sh.Name = shName

frmPBar.Caption = "正在导出数据,请稍候......"
frmPBar.Show
For i = 0 To src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Rows - 1
For j = 1 To src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Cols - 1
sh.Cells(i + 1, j) = src/DownloadFiles\a\2005-02-15\/DownloadFiles\a\2005-02-15\Grid.Cell(flexcpText, i, j)
DoEvents
Next j
Next i
Unload frmPBar

appXL.Visible = True
Exit Sub
errhandler:
MsgBox Err.Description

End Sub