gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:11:10 24 4
gpt4 key购买 nike

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

最佳答案

只需将 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

关于vb.net - 使用现有 Excel 文件保存数据并以新名称保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42581195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com