2017-03-03 239 views
2

我正在創建一個採用Excel文件的程序,在其上標記信息並將其保存在文件位置。使用現有的Excel文件保存數據並以新名稱保存

我可以很容易地創建一個新的Excel工作表,把它的信息,然後將其保存到文件位置。這不是我需要的。在表單中,我希望它能夠拉取我創建的現有空白Excel文件模板,將表單中輸入的信息加蓋到它上面,重命名文件並將其保存在文件位置(類似於「另存爲」)。這樣,最初就會有一個空白的主模板文件。

我不知道如何抓住該Excel文件和而不是創建一個新的Excel文件。

下面是一些示例代碼:

If EmployeeInfo.empNameTextBox.Text = "" Or EmployeeInfo.dateBox.Text = "" Then 
    'prompt user must include name and date at least to save 
    MessageBox.Show("In order to save a file, you must include the name AND the date", "Fill in Name/Date!", 
      MessageBoxButtons.OK, MessageBoxIcon.Error) 
    'minimize the password form and open back up the EmployeeInfo form 
    EmployeeInfo.Show() 
    Me.Hide() 
Else 
    'create and save the excel file 
    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 


    'Add data to cells of the first worksheet in the new workbook 
    oSheet = oBook.Worksheets(1) 
    oSheet.Range("A1").Value = "Last Name" 
    oSheet.Range("B1").Value = "First Name" 
    oSheet.Range("A1:B1").Font.Bold = True 
    oSheet.Range("A2").Value = "Litoris" 
    oSheet.Range("B2").Value = "Mike" 

    'Save the Workbook and Quit Excel 
    oBook.SaveAs("N:\IT\Device Images\Incomplete\" + EmployeeInfo.empNameTextBox.Text + EmployeeInfo.dateBox.Text) 
    oExcel.Quit 

    'minimize this form and go back to main form 
    ImageTool.Show() 
    Me.Hide() 
End If 

爲了證實,我並不想開始一個新的Excel工作簿和無法弄清楚如何拉我創造了我現有的文件。

回答

3

只要改變oBook = oExcel.Workbooks.Add
oBook = oExcel.Workbooks.Open("C:\Path\FileName.xls")

並設置正確的路徑,以及下一行的右側紙! ;)

If EmployeeInfo.empNameTextBox.Text = "" Or EmployeeInfo.dateBox.Text = "" Then 
    'prompt user must include name and date at least to save 
    MessageBox.Show("In order to save a file, you must include the name AND the date", "Fill in Name/Date!", 
     MessageBoxButtons.OK, MessageBoxIcon.Error) 
    'minimize the password form and open back up the EmployeeInfo form 
    EmployeeInfo.Show() 
    Me.Hide() 
Else 
    'create and save the excel file 
    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.Open("C:\Path\FileName.xls") 


    'Add data to cells of the first worksheet in the new workbook 
    oSheet = oBook.Worksheets(1) 
    oSheet.Range("A1").Value = "Last Name" 
    oSheet.Range("B1").Value = "First Name" 
    oSheet.Range("A1:B1").Font.Bold = True 
    oSheet.Range("A2").Value = "Litoris" 
    oSheet.Range("B2").Value = "Mike" 

    'Save the Workbook and Quit Excel 
    oBook.SaveAs("N:\IT\Device Images\Incomplete\" + EmployeeInfo.empNameTextBox.Text + EmployeeInfo.dateBox.Text) 
    oExcel.Quit 

    'minimize this form and go back to main form 
    ImageTool.Show() 
    Me.Hide() 
End If 
2

正如已經在R3uKanswer可以使用Workbooks.Open法規定:

Dim oExcel As Object 
Dim oBook As Object 

oExcel = CreateObject("Excel.Application") 
oBook = oExcel.Workbooks.Open("filename") 

我想在這個非常輕微擴大,建議大家直接引用Excel對象:

Dim oExcel As New Excel.Application 
Dim oBook As Workbook = oExcel.Workbooks.Open("filename") 

這將有助於在Excel對象上引用方法和屬性。下面你將看到的區別:

間接參考:

enter image description here

直接引用:

enter image description here

注意,您必須導入相關的Microsoft Excel Object Library到您的項目。您還必須將Imports Microsoft.Office.Interop添加到您的班級。

作爲一個側面說明,如果你有沒有準備好,我會強烈建議開啓Option Strict同時使用Excel對象:

隱含範圍限制數據類型轉換爲只能進行擴大轉換,不允許後期綁定,並且不允許導致Object類型的隱式類型。

相關問題