【问题标题】:How to overwrite an excel application without prompting the users如何在不提示用户的情况下覆盖 Excel 应用程序
【发布时间】:2011-02-19 09:04:16
【问题描述】:

谁能帮助我如何在不提示 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()

【问题讨论】:

    标签: vb.net excel overwrite


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      如果您只想覆盖当前存在的文件,可能会更容易先将其删除,然后保存新文件。所以只需使用System.IO.File.Delete

      【讨论】:

      • 感谢何先生的建议
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
         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
        

        【讨论】:

          猜你喜欢
          • 2021-11-20
          • 2010-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多