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

Use an existing Excel file to save data on and save under a new name

我正在创建一个程序,它接受一个 Excel 文件,在其上标记信息并将其保存在文件位置。

我可以轻松创建一个新的 Excel sheet,将信息放在上面,然后将其保存到文件位置。那不是我需要的。在表单中,我希望它拉取我创建的现有空白 Excel 文件模板,将表单中输入的信息标记给它,重命名文件并将其保存在文件位置(类似于 "save as").这样一开始就会得到一个空白的主模板文件。

我不知道如何获取那个 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 工作簿,也不知道如何提取我创建的现有文件。

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

并设置正确的路径,以及下一行的正确sheet! ;)

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

R3uK's you can use the 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 对象的方法和属性。下面你会看到不同之处:

间接引用:

直接引用:

Note you must import the relevant Microsoft Excel Object Library into your project. You will also have to add Imports Microsoft.Office.Interop to your class.

附带说明一下,如果您还没有,我强烈建议在使用 Excel 对象时打开 Option Strict On

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.