VB.Net,修改现有的Excel而不是replace,并正确closures呼叫

我一直在研究和testing这个问题三天。 我尝试过在其他网站上看到的代码变体,以及我在这里find的变体代码。 我已经看了以下网站的灵感: 在Excel中追加数据 ; 如何在Excel中追加 ; 转移到Excel的方法 ; 在VB网上使用Excel ;以及其他一些网站。

我有几个问题想要在第一个程序中解决。 有了上述工具,我学会了validation用户input并将其应用于Excel工作簿。 但是,每次在尝试创build分辨率时进行更改后进行debugging时,Excel总是询问是否要replace现有文件。 我不。 我想添加从运行程序的每个实例中检索到的数据,并replace以前的数据。

如何在不replace现有工作簿的情况下添加新数据? 感谢您的时间,考虑和任何build议。

Imports Excel = Microsoft.Office.Interop.Excel 

公共类frmLocationECS

 'Launches the first stage of the tracking process and removes four characters of the word item to populate the labels with the correct parts at each location. Private Sub frmLocationECS_Load(sender As Object, e As System.EventArgs) Handles Me.Load For Each ctrl As Control In TableLayoutPanel1.Controls If ctrl.Name.StartsWith("Item") And TypeOf ctrl Is Item Then CType(ctrl, Item).Title(ctrl.Name.Remove(0, 4)) End If Next End Sub 'Closes the current form and moves the user to the next location. Private Sub BtnNext_Click(sender As System.Object, e As System.EventArgs) Handles BtnNext.Click 'Declare an instance of Excel and it's current path. Data enterd by user is to be saved in the existing Excel workbook. Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet 'Dim misValue As Object = System.Reflection.Missing.Value 'Dim milkStart As Date = Now xlApp.Workbooks.Open("C:\IOtestsVB\TestWrite.xlsx") 'xlApp = CreateObject("Excel.Application") xlWorkBook = xlApp.ActiveWorkbook xlWorkSheet = xlWorkBook.ActiveSheet 'xlWorkSheet.Cells(2, 1) = "Plates (ECS)" 'xlWorkSheet.Cells(2, 2) = "Inks (ECS)" 'xlWorkSheet.Cells(2, 3) = "Chambers (ECS)" 'xlWorkSheet.Cells(2, 4) = "Cores (ECS)" 'xlWorkSheet.Cells(2, 5) = "Other (ECS)" 'xlWorkSheet.Cells(1, 1) = milkStart xlWorkSheet.Cells(3, 1) = ItemPlates.Value xlWorkSheet.Cells(3, 2) = ItemInks.Value xlWorkSheet.Cells(3, 3) = ItemChambers.Value xlWorkSheet.Cells(3, 4) = ItemCores.Value xlWorkSheet.Cells(3, 5) = ItemOther.Value xlWorkBook.Save() xlApp.Workbooks.Close() 'xlWorkBook.SaveAs("C:\IOtestsVB\" & Now.ToString("HHmmss.xlsx")) xlApp.Quit() frmTest.ShowDialog() Me.Close() End Sub 

末class

你必须添加value属性来赋值给单元格。

 'Launches the first stage of the tracking process and removes four characters of the word item to populate the labels with the correct parts at each location. Private Sub frmLocationECS_Load(sender As Object, e As System.EventArgs) Handles Me.Load For Each ctrl As Control In TableLayoutPanel1.Controls If ctrl.Name.StartsWith("Item") And TypeOf ctrl Is Item Then CType(ctrl, Item).Title(ctrl.Name.Remove(0, 4)) End If Next End Sub 'Closes the current form and moves the user to the next location. Private Sub BtnNext_Click(sender As System.Object, e As System.EventArgs) Handles BtnNext.Click 'Declare an instance of Excel and it's current path. Data enterd by user is to be saved in the existing Excel workbook. Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet 'Dim misValue As Object = System.Reflection.Missing.Value 'Dim milkStart As Date = Now xlApp.Workbooks.Open("C:\IOtestsVB\TestWrite.xlsx") 'xlApp = CreateObject("Excel.Application") xlWorkBook = xlApp.ActiveWorkbook xlWorkSheet = xlWorkBook.ActiveSheet 'xlWorkSheet.Cells(2, 1).value = "Plates (ECS)" 'xlWorkSheet.Cells(2, 2).value = "Inks (ECS)" 'xlWorkSheet.Cells(2, 3).value = "Chambers (ECS)" 'xlWorkSheet.Cells(2, 4).value = "Cores (ECS)" 'xlWorkSheet.Cells(2, 5).value = "Other (ECS)" 'xlWorkSheet.Cells(1, 1).value = milkStart xlWorkSheet.Cells(3, 1).value = ItemPlates.Value xlWorkSheet.Cells(3, 2).value = ItemInks.Value xlWorkSheet.Cells(3, 3).value = ItemChambers.Value xlWorkSheet.Cells(3, 4).value = ItemCores.Value xlWorkSheet.Cells(3, 5).value = ItemOther.Value xlWorkBook.Save() xlApp.Workbooks.Close() 'xlWorkBook.SaveAs("C:\IOtestsVB\" & Now.ToString("HHmmss.xlsx")) xlApp.Quit() frmTest.ShowDialog() Me.Close() End Sub End Class 

我想你可以使用

  xlObject.AlertBeforeOverwriting = False xlObject.DisplayAlerts = False