如何在不提示用户的情况下覆盖 Excel 应用程序

发布于 2024-09-01 08:31:55 字数 552 浏览 5 评论 0原文

任何人都可以帮助我如何在不提示 VB.Net 中的用户的情况下覆盖 excel 文件..

我已经尝试过这段代码,但它不起作用..

Dim xlsApp As New Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim dir As String = Application.StartupPath & "\Template\SampleTemplate.xls"
xlsBook = GetObject(dir)
xlsSheet = xlsBook.Sheets("Per BPA Error Report")


xlsSheet.Range("C2:T2").Merge()

xlsApp.DisplayAlerts = False
xlsSheet.SaveAs(Application.StartupPath & "\Template\SampleTemplate.xls")
xlsBook = Nothing
xlsSheet = Nothing
xlsApp.Quit()

Can anyone help me on how can I overwrite the excel file without prompting the users in VB.Net..

I have try this code but It doesn't work..

Dim xlsApp As New Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim dir As String = Application.StartupPath & "\Template\SampleTemplate.xls"
xlsBook = GetObject(dir)
xlsSheet = xlsBook.Sheets("Per BPA Error Report")


xlsSheet.Range("C2:T2").Merge()

xlsApp.DisplayAlerts = False
xlsSheet.SaveAs(Application.StartupPath & "\Template\SampleTemplate.xls")
xlsBook = Nothing
xlsSheet = Nothing
xlsApp.Quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤云独去闲 2024-09-08 08:31:55
Public Sub WriteExcelFile(ByVal ExcelFilePath As String) 
    Dim excel As Application = New Application
    Dim w As Workbook = excel.Workbooks.Open(ExcelFilePath)
    Dim sheet As Worksheet = w.Sheets(1)
    sheet.Cells(x + 1, 1) = 10
    x = x + 1
    excel.DisplayAlerts = False
    w.Save()
    w.Close()
End Sub
Public Sub WriteExcelFile(ByVal ExcelFilePath As String) 
    Dim excel As Application = New Application
    Dim w As Workbook = excel.Workbooks.Open(ExcelFilePath)
    Dim sheet As Worksheet = w.Sheets(1)
    sheet.Cells(x + 1, 1) = 10
    x = x + 1
    excel.DisplayAlerts = False
    w.Save()
    w.Close()
End Sub
绝不服输 2024-09-08 08:31:55

如果您只想覆盖当前存在的文件,那么先删除它然后保存新文件可能会更容易。因此只需使用 System.IO.File.Delete 即可。

If you just want to overwrite the file that's currently there, might just be easier to delete it first and then save the new file. So just use System.IO.File.Delete.

残花月 2024-09-08 08:31:55

为什么需要使用另存为
查看代码,您正在尝试写入同一个文件。请改用保存

Why do you need to use SaveAs?
Looking at the code, you are trying to write to the same file. Use Save instead.

薄凉少年不暖心 2024-09-08 08:31:55
 Private Sub f_ExcelWorksheet()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel
    oExcel = CreateObject("Excel.Application")

    'oBook = oExcel.Workbooks.Add 'This is when we want to create new excel sheet

    'If we want to open exisiting excel sheet
    oBook = oExcel.Workbooks.Open("C:\Users\adimadud\Desktop\Test.xlsx")

    'Add data to cells of the first worksheet in the new workbook
    oSheet = oBook.Worksheets(1)

    'This will find the lastRow in the sheet
    Dim lastRow As Integer = oSheet.UsedRange.Rows.Count

    'This is next emptyRow in the sheet
    Dim emptyRow As Integer = lastRow + 1
    'oSheet.Range("A1").Value = "Last Name"
    'oSheet.Range("B1").Value = "First Name"
    'oSheet.Range("A1:B1").Font.Bold = True
    'oSheet.Range("A2").Value = "Doe"
    'oSheet.Range("B2").Value = "John"



    MessageBox.Show(lastRow)
    oSheet.Cells(emptyRow, 1).value = "Test"
    oSheet.Cells(emptyRow, 2).value = "Test"
    'Now again find the lastRow in the excel sheet
    lastRow = oSheet.UsedRange.Rows.Count
    'This is next emptyRow in the sheet
    emptyRow = lastRow + 1

    'This will not prompt the user to overwrite the excel sheet
    oExcel.DisplayAlerts = False
    oBook.Save()
    oBook.Close()

    'Save the Workbook and Quit Excel
    'This will prompt the user to overwrite the excel sheet
    'oBook.saveas("C:\Users\adimadud\Desktop\Test.xlsx")

    oExcel.Quit()

End Sub
 Private Sub f_ExcelWorksheet()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel
    oExcel = CreateObject("Excel.Application")

    'oBook = oExcel.Workbooks.Add 'This is when we want to create new excel sheet

    'If we want to open exisiting excel sheet
    oBook = oExcel.Workbooks.Open("C:\Users\adimadud\Desktop\Test.xlsx")

    'Add data to cells of the first worksheet in the new workbook
    oSheet = oBook.Worksheets(1)

    'This will find the lastRow in the sheet
    Dim lastRow As Integer = oSheet.UsedRange.Rows.Count

    'This is next emptyRow in the sheet
    Dim emptyRow As Integer = lastRow + 1
    'oSheet.Range("A1").Value = "Last Name"
    'oSheet.Range("B1").Value = "First Name"
    'oSheet.Range("A1:B1").Font.Bold = True
    'oSheet.Range("A2").Value = "Doe"
    'oSheet.Range("B2").Value = "John"



    MessageBox.Show(lastRow)
    oSheet.Cells(emptyRow, 1).value = "Test"
    oSheet.Cells(emptyRow, 2).value = "Test"
    'Now again find the lastRow in the excel sheet
    lastRow = oSheet.UsedRange.Rows.Count
    'This is next emptyRow in the sheet
    emptyRow = lastRow + 1

    'This will not prompt the user to overwrite the excel sheet
    oExcel.DisplayAlerts = False
    oBook.Save()
    oBook.Close()

    'Save the Workbook and Quit Excel
    'This will prompt the user to overwrite the excel sheet
    'oBook.saveas("C:\Users\adimadud\Desktop\Test.xlsx")

    oExcel.Quit()

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文