'
'适应各种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 IntegerIf 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
NextIf 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 IfEnd If
End If
End IfReturn k
End Function
End Class
联系客服