用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