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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值