使用现有Excel文件保存数据并以新名称保存

时间:2017-03-03 14:17:01

标签: vb.net excel-interop

我正在创建一个程序,它接受一个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工作簿,也无法弄清楚如何提取我创建的现有文件。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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添加到您的班级。

作为附注,如果你还没有,我强烈建议在使用Excel对象时转动Option Strict 开启

  

将隐式数据类型转换限制为仅扩展转换,禁止后期绑定,并禁止导致Object类型的隐式类型。