我正在创建一个程序,该程序接受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工作簿,并且无法确定如何提取我创建的现有文件。
发布于 2017-03-03 14:20:30
只需将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
发布于 2017-03-03 14:42:09
正如R3uK的回答中已经指出的,您可以使用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对象上的方法和属性。在下面,您将看到不同之处:
间接参考:
直接参考:
注意,您必须将相关的
Microsoft Excel Object Library
导入到项目中。您还必须将Imports Microsoft.Office.Interop
添加到类中。
另外,如果您还没有,我强烈建议在使用选项严格对象的同时打开:
将隐式数据类型转换限制为仅扩大转换,不允许后期绑定,不允许导致对象类型的隐式类型。
https://stackoverflow.com/questions/42581195
复制相似问题