VB实现Excel中插入文件

Module Module1
    Sub Main(ByVal Args() As String)
        If (Args.Length = 7) Then
            Dim excelApp As Microsoft.Office.Interop.Excel.Application
            Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
            Dim excelsheet As Microsoft.Office.Interop.Excel.Worksheet '定义工作表类

            Dim excelPath As String  'excel文件路径
            Dim insertPath As String  '要插入文件的路径
            Dim sheetIndex As Integer 'sheet页的下标,从1开始
            Dim col As Integer '要将文件插入到哪一列
            Dim row As Integer '要将文件插入到哪一列行
            Dim colCount As Integer '所占的列数
            Dim rowCount As Integer '所占的行数

            excelPath = Args(0)
            insertPath = Args(1)
            sheetIndex = Args(2)
            col = Args(3)
            row = Args(4)
            colCount = Args(5)
            rowCount = Args(6)

            ' System.Console.WriteLine("excelPath......." + excelPath)
            ' System.Console.WriteLine(sheetIndex)
            ' System.Console.WriteLine(col)
            ' System.Console.WriteLine(row)
            ' System.Console.WriteLine(insertPath)
            Try
                excelApp = GetObject("Excel.Application")
                excelApp.DisplayAlerts = False
            Catch ex As Exception
                System.Console.WriteLine("GetObject......." + ex.Message)
            End Try

            If (excelApp Is Nothing) Then
                Try
                    excelApp = CreateObject("Excel.Application")
                    excelApp.DisplayAlerts = False
                Catch ex As Exception
                    System.Console.WriteLine("CreateObject......." + ex.Message)
                End Try
            End If

            If (excelApp Is Nothing) Then
                Return
            Else
                Try
                    excelBook = excelApp.Workbooks.Open(Args(0))
                Catch ex As Exception
                    System.Console.WriteLine("excelBook......." + ex.Message)
                End Try
                excelApp.Visible = False

                Try
                    excelsheet = New Microsoft.Office.Interop.Excel.Worksheet()
                    excelsheet = excelBook.Worksheets(sheetIndex)
                    excelsheet.Select()
                Catch ex As Exception
                    System.Console.WriteLine("excelsheet......" + ex.Message)
                End Try

                Dim range As Microsoft.Office.Interop.Excel.Range
                Try
                    range = excelApp.Cells(row, col)
                    range.Select() '设置对哪一个单元格插入文件
                Catch ex As Exception
                    System.Console.WriteLine("range......" + ex.Message)
                End Try

                Try
                    Dim height = (range.Height) * rowCount
                    Dim width = (range.Width) * colCount
                    Dim acdHeight = 1
                    Dim acdWidth = 1

                    Dim oleObj As Microsoft.Office.Interop.Excel.OLEObject
                    oleObj = excelsheet.OLEObjects.Add(FileName:=insertPath)
                    oleObj.Application.Visible = False
                    acdHeight = oleObj.Height
                    acdWidth = oleObj.Width

                    System.Console.WriteLine("acdHeight::acdWidth")
                    System.Console.WriteLine(acdHeight)
                    System.Console.WriteLine(acdWidth)
                    System.Console.WriteLine(acdHeight / acdWidth)

                    ' System.Console.WriteLine("height::width")
                    '  System.Console.WriteLine(height)
                    ' System.Console.WriteLine(width)

                    '满足关系:设宽为:x,高为y  则1.x/y=acdWidth/acdHeight 2.0<x< width 3.0<y<height
                    Dim flag As Boolean
                    Dim tempHeight = 1
                    Dim tempWidth = 1

                    flag = True
                    While flag = True
                        tempWidth = width '设置宽度
                        tempHeight = width * (acdHeight / acdWidth) '设置高度
                        If (tempHeight > height) Then
                            width = width - 0.5
                        Else
                            oleObj.Height = tempHeight
                            oleObj.Width = tempWidth
                            flag = False
                        End If
                    End While

                    System.Console.WriteLine("oleObj.height::oleObj.width")
                    System.Console.WriteLine(oleObj.Height)
                    System.Console.WriteLine(oleObj.Width)
                    System.Console.WriteLine(oleObj.Height / oleObj.Width)
                Catch ex As Exception
                    System.Console.WriteLine("OLEObjects......." + ex.Message)
                End Try

                Try
                    If Not excelApp.ActiveWorkbook.Saved Then
                        excelApp.SaveWorkspace()
                    End If
                    excelApp.Workbooks.Close()
                Catch ex As Exception
                    System.Console.WriteLine("Close......" + ex.Message)
                Finally
                End Try
            End If
        Else
            MsgBox("请检查传入参数个数是否正确")
        End If
    End Sub

End Module

 

你可能感兴趣的:(Excel,Microsoft,Office,vb)