How to overwrite an excel application without prompting the users

后端 未结 4 1618
甜味超标
甜味超标 2021-01-19 14:13

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 xlsA         


        
相关标签:
4条回答
  • 2021-01-19 14:41
    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
    
    0 讨论(0)
  • 2021-01-19 14:49

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

    0 讨论(0)
  • 2021-01-19 14:55

    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.

    0 讨论(0)
  • 2021-01-19 14:57
     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
    
    0 讨论(0)
提交回复
热议问题