| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1380 人关注过本帖
标题:用VB.NET写的将一个Excel文件自动生成一个HTML页面的完整代码,兄弟们参考一 ...
只看楼主 加入收藏
wdlvictor
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-4-28
收藏
 问题点数:0 回复次数:2 
用VB.NET写的将一个Excel文件自动生成一个HTML页面的完整代码,兄弟们参考一下。
Private Sub Test()

        Dim objExcelApp As New Excel.Application
        Dim xlbook As Excel.Workbook
        Dim ws1 As Excel.Worksheet
        Dim ws4 As Excel.Worksheet
        Dim rng As Excel.Range

        Dim OpenFile As New OpenFileDialog
        OpenFile.Filter = "Excel文件 |(*.xls)"
        OpenFile.ShowDialog()
        Dim FileName As String
        FileName = OpenFile.FileName

        Try

            xlbook = objExcelApp.Workbooks.Open(FileName)

            xlBook.Unprotect("123456") '取消工作簿的保护
            ws1 = xlBook.Sheets.Item(1)
            ws1.Unprotect("123456") '取消工作表的保护
            ws1.Copy(After:=xlBook.Sheets(3)) '将ws1复制到sheets(3)的后面

            ws4 = xlBook.Sheets(4)
            ws4.Name = "Public" '为Sheets(4)命名

            rng = DirectCast(ws4.Columns("$H"), Excel.Range)
            rng.Delete() 'DirectCast函数和CTapy函数的用法一样,第一个参数(表达式)转换成第二个参数(类型)
            rng = DirectCast(ws4.Columns("$G"), Excel.Range)
            rng.Delete()
            rng = DirectCast(ws4.Columns("$E"), Excel.Range)
            rng.Delete()

            Dim strWk As String '定义文件上报路径
            Dim objFileInfo As
            objFileInfo = New (FileName) '获取文件路径
            strWk = objFileInfo.ToString.Substring(0, FileName.IndexOf(".")) '取得路径及名件名(但不包括后缀)
            'GetHtmlCode("市场名称")
            objExcelApp.DisplayAlerts = False '取消起用宏的询问
            '确定将所选项(类型、文件名、区域、Excel生成的网页)保存到网页中
            xlbook.PublishObjects.Add( _
                SourceType:=Excel.XlSourceType.xlSourceRange, _
                FileName:=strWk + ".htm", _
                Sheet:="Public", _
                Source:="A10:I100", _
                HTMLType:=Excel.XlHtmlType.xlHtmlStatic).Publish()
            MsgBox("成功上报HTML文件!", MsgBoxStyle.OKOnly, "提示")
            xlbook.Close() '关闭工作簿
            objExcelApp.Quit()
        Catch ex As Exception
            MsgBox("上报HTML文件失败!", MsgBoxStyle.OKOnly, "提示")
        Finally '清除占用资源
            If Not objExcelApp Is Nothing Then objExcelApp = Nothing
            If Not xlbook Is Nothing Then xlbook = Nothing
            If Not ws1 Is Nothing Then ws1 = Nothing
            If Not ws4 Is Nothing Then ws4 = Nothing
            If Not rng Is Nothing Then rng = Nothing
            GC.Collect() '强制进行拉圾回收
        End Try

    End Sub
搜索更多相关主题的帖子: Excel NET 兄弟 HTML 文件 
2005-04-29 09:22
szyyx
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
得分:0 

Goods

2006-07-02 18:44
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 
谢谢,好东西就是要共享

等待相逢
2006-10-25 11:33
快速回复:用VB.NET写的将一个Excel文件自动生成一个HTML页面的完整代码,兄弟们 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016824 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved