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

ASP.NET
漫谈.Net开发关于命名空间和目录划分
.net中关于企业Excel报表的生成
.NET中取得IP/用户名等信息常用方法
关于.Net开发下的分布式缓存设计
.NET中为组合框添加自动查询功能
ASP.NET如何进行性能优化问题
开发中如何有效监控.NET应用程序
ASP.NET2.0 显示写入日期和时间语法
用.NET Array类的Sort方法分类数值
Asp.net Mvc Pv4中使用AjaxHelper
Asp.net中Forms验证的角色验证授权(一)
Asp.net中Forms验证的角色验证授权(二)
Asp.Net2.0数据库基本操作方法学习
Asp.Net之枚举类型输出需要类型转换
用.NET的File控件上传文件的解决方案
.NET泛型技巧之类型参数之间的转换
在ASP.NET中将数据直接输出成Excel格式
在.NET框架下使用自定义配置设置
跟ASP.NET MVC一起使用jQuery
Visual Basic中文本框处理技巧集萃

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 85 ::
收藏到网摘: 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