打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【转载】VB.NET EXCEL操作类

'
'适应各种EXCEL版本,彻底解决兼容问题

'

'
Imports System.Drawing
Public Class TKSEXCEL
    Private app As Object
    Private book As Object
    Private sheet As Object

    '表格名称
    Public Property xlSheetName() As String
        Get
            Return sheet.name
        End Get
        Set(ByVal value As String)
            sheet.name = value
        End Set
    End Property
    '新建程序
    Public Function Create() As Boolean
        app = CreateObject("Excel.Application")
        If app Is Nothing Then
            Return False
        Else
            app.Visible=True
            Return True
        End If
    End Function
    '打开文件
    Public Sub Open(ByVal xlFileName As String)
        book = app.Workbooks.Open(xlFileName)
        sheet = book.ActiveSheet
    End Sub
    '写单元格
    Public Sub Write(ByVal _Range As String, ByVal value As Object)
        If _Range <> "" Then
            sheet.Range(_Range).Value = value
        End If
    End Sub
    '插入图片
    Public Sub InsertPicture(ByVal _Range As String, ByVal imageFile As String)
        If _Range <> "" Then
            Dim ExcelRange As Object = sheet.Range(_Range)
            Dim Ins_image As Image
            Ins_image = Image.FromFile(imageFile)

            Dim HeightScale As Double = Math.Round((ExcelRange.Height - 6) / Ins_image.Height, 2)
            Dim WidthScale As Double = Math.Round((ExcelRange.Width - 6) / Ins_image.Width, 2)
            Dim ScaleHW As Double = Math.Min(HeightScale, WidthScale)
            sheet.Shapes.AddPicture(imageFile, 0, 1, ExcelRange.Left + 2, ExcelRange.Top + 2, Ins_image.Width * ScaleHW, Ins_image.Height * ScaleHW)
            Ins_image.Dispose()
        End If
    End Sub
    '取指定单元格值
    Public Function GetValue(ByVal _table As Integer, ByVal _Range As String) As String
        Dim sheet As Object = book.sheets.item(_table)
        Return sheet.Range(_Range).value
    End Function
    Public Function Save() As Boolean
        book.Save()
    End Function

    '取数组,本人专用
    Public Function GetArryValue(ByRef _table As Integer, ByVal _Row As String, ByVal _Cell As String, ByRef ArryString() As String) As Integer
        Dim sheet As Object = book.sheets.item(_table)

        Dim rowvalue As String = sheet.Range(_Row).value
        Dim cellvalue As String = sheet.Range(_Cell).value
        Dim k As Integer = 0
        If rowvalue <> "" And cellvalue <> "" Then
            Dim rowstr() As String = Split(rowvalue, "/")
            Dim cellstring() As String = Split(cellvalue, "/")
            Dim a, b, c, d As Integer

            If rowstr.Length > 1 And cellstring.Length > 0 Then
                a = Convert.ToInt32(rowstr(0))
                b = Convert.ToInt32(rowstr(1))
                If a > 0 And b > a Then
                    ReDim Preserve ArryString(b - a)
                    For i As Integer = 0 To b - a
                        ArryString(i) = cellstring(0) & Convert.ToString(a + i)
                        k += 1
                    Next

                    If rowstr.Length > 3 And cellstring.Length > 1 Then
                        c = Convert.ToInt32(rowstr(2))
                        d = Convert.ToInt32(rowstr(3))
                        If c > 0 And d > c Then
                            ReDim Preserve ArryString(b - a + d - c + 1)
                            For j As Integer = 0 To d - c
                                ArryString(b - a + j + 1) = cellstring(1) & Convert.ToString(c + j)
                                k += 1
                            Next
                        End If
                    End If

                End If

            End If
        End If

        Return k
    End Function
End Class


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VB实用代码,收藏!!
VBA中名称的使用
2017
VBA对MySql数据库进行读取和写入操作_MySQL_开发学院
VB爱好者乐园(VBGood) - 经验之谈 - VB编程的必备技巧
三类加密算法VB.NET的实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服