vb.net操作Excel表格类

Public Class Execl
    Private ExcelApp As Microsoft.Office.Interop.Excel.Application
    Private ExcelBook As Microsoft.Office.Interop.Excel.Workbook
    Private ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet

    '构造函数
    Public Sub New()
        ExcelApp = New Microsoft.Office.Interop.Excel.Application
    End Sub

    '是否显示Excel文件
    Public Sub IsVisible(ByVal visible As Boolean)
        ExcelApp.Visible = visible
    End Sub

    '按照filename给出路径打开Excel文件
    Public Sub OpenExcel(ByVal filename As String)
        ExcelBook = ExcelApp.Workbooks.Open(filename)
    End Sub

    '打开名字为sheetname的Sheet
    Public Sub OpenSheet(ByVal sheetname As String)
        ExcelSheet = ExcelBook.Sheets(sheetname)
    End Sub

    '获取已使用的行数
    Public Function UserRange()
        Return ExcelSheet.UsedRange.Cells.Rows.Count
    End Function

    '获取单元格的值
    Public Function GetRangeValue(ByVal range As String)
        Return ExcelSheet.Range(range).Value
    End Function

    '设置单元格的值
    Public Sub SetRangeValue(ByVal range As String, ByVal value As String)
        ExcelSheet.Range(range).Value = value
    End Sub

    '清除单元格内容
    Public Sub ClearRange(ByVal range As String)
        ExcelSheet.Range(range).ClearContents()
    End Sub

    '设置单元格字体,字体大小,是否粗体,水平对齐类型
    Public Sub SetRangeAttribute(ByVal range As String, ByVal font As String, ByVal size As Integer, ByVal bold As Boolean, ByVal horalign As Microsoft.Office.Interop.Excel.XlHAlign)
        ExcelSheet.Range(range).Font.Name = font
        ExcelSheet.Range(range).Font.Size = size
        ExcelSheet.Range(range).Font.Bold = bold
        ExcelSheet.Range(range).HorizontalAlignment = horalign
    End Sub

    '保存工作表
    Public Sub Save()
        ExcelBook.Save()
    End Sub

    '打印工作表
    Public Sub Print()
        ExcelBook.PrintOutEx()
    End Sub

    '关闭Excel文件
    Public Sub Close()
        ExcelSheet = Nothing
        ExcelBook.Close()
        ExcelBook = Nothing
        ExcelApp.Quit()
        ExcelApp = Nothing
    End Sub

End Class

发布了2 篇原创文章 · 获赞 1 · 访问量 3642
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览