Window中office的dll使用 复制sheet和模板使用(Microsoft.Office.Interop.Excel)

        'Excel
        Dim app As Microsoft.Office.Interop.Excel.Application = Nothing
        Dim workbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
        Dim sheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
        Dim tlsheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing

        '路径
        Dim ssDirectoryPath As String = ""
        ssDirectoryPath = BB.MJ.openBrawserDialog(filepath)
        If ssDirectoryPath Is Nothing _
        OrElse ssDirectoryPath.Length = 0 Then
            Return False
        End If

        'ExcelApplication
        app = New Microsoft.Office.Interop.Excel.Application()
        app.Visible = False
        app.DisplayAlerts = False

        '模板使用
        workbook = app.Workbooks._Open(v_datadir.ToString() & "\" & c_tplpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        '模板シート
        tlsheet = CType(workbook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
        sheet = tlsheet

        If Not ssDirectoryPath.Last.ToString.Equals("\") Then
            ssDirectoryPath = ssDirectoryPath & "\"
        End If

        Dim filename As String = ssDirectoryPath & "file"

        '文件存在
        If System.IO.File.Exists(filename) = True Then


            If MsgBox("文件已经存在是否覆盖", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
                Return False
            End If
        End If

        '复制sheet
        tlsheet.Copy(Type.Missing, sheet)

        'Excel内容部分自定义
        'sheet.Cells(rIdx, cIdx)="文本内容"

        '删除模板sheet
        tlsheet.Delete()

        '另存为模式
        workbook.Saved = True
        workbook.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

        '关闭Excel
        workbook.Close(False, DBNull.Value, DBNull.Value)
        workbook = Nothing

        '回收Excel内存
        app.Quit()
        app = Nothing


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值