如何将多个工作表添加到Excel工作簿并保存(无限加载)

时间:2019-05-21 07:11:47

标签: excel vb.net

我想在一个现有工作簿中添加多个空白表,语言是VB.net,并且代码在本地服务器上启动

完整代码:

Dim objApp As Excel.Application
Dim objBook As Excel._Workbook
Dim objBooks As Excel.Workbooks

objApp = New Excel.Application()
objApp.Visible = True
objBooks = objApp.Workbooks
objBook = objApp.Workbooks.Open(MyPath)
For i As Integer = 1 To 10
    objBook.Sheets.Add()
Next

objBook.Save()
objBook.Close()
objApp.Quit()

执行代码时,将保存xls文件,但未添加任何工作表。  我的本地主机无限加载  感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码可以正常工作.....

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Dim objApp As Excel.Application
    Dim objBook As Excel._Workbook
    Dim objBooks As Excel.Workbooks
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        objApp = New Excel.Application()
        objApp.Visible = True
        objBooks = objApp.Workbooks
        objBook = objApp.Workbooks.Open("\\PC1\1.xlsx")
        For i As Integer = 1 To 10
            objBook.Sheets.Add()
        Next
        objBook.Save()
        objBook.Close()
        objApp.Quit()
    End Sub
End Class

只需生成可执行文件,然后在模式下运行它 以管理员身份运行 通过右键单击程序,然后单击以管理员身份运行