在VB.NET中修改基于模板的excel

在VB.NET中修改基于模板的excel,vb.net,excel,Vb.net,Excel,我正在快速运行这样的代码 xlWorkSheet.Cells(行数,1)=“某些值” 问题是当我打开excel文档时,单元格值没有更新 任何帮助都将不胜感激 我使用以下代码打开该文件 Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx") Dim xlApp As Excel.Application =New Excel.Application Dim xlWorkBook As Exce

我正在快速运行这样的代码 xlWorkSheet.Cells(行数,1)=“某些值”

问题是当我打开excel文档时,单元格值没有更新

任何帮助都将不胜感激

我使用以下代码打开该文件

Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx")

Dim xlApp As Excel.Application =New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlworkbooks As Excel.Workbooks
Dim xlWorksheets As Excel.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkSheet As Excel.Worksheet = Nothing

xlWorkBook = xlApp.Workbooks.Add(misValue)

xlworkbooks = xlApp.Workbooks

xlWorkBook = xlworkbooks.Open(pathtofile )

xlWorkSheet = xlWorkBook.Sheets("Sheet1")

您需要保存、关闭和释放com对象,以确保excel不会继续作为进程运行

Sub save(filename As String) ' Saves the sheet under a specfic name
        xlWorkBook.SaveAs(filename)
    End Sub

 Sub closexl()
        xlWorkBooks.Close()
        xlApp.Quit()
    End Sub

Sub cleanup() ' Releases all of the com objects to object not have excel running as a process after this method finishes
        GC.Collect()
        GC.WaitForPendingFinalizers()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
        xlApp = Nothing
    End Sub

如何打开文档?(向我们显示代码)保存和关闭工作簿的代码如何?另外,为什么要创建一个空工作簿作为第一个操作?我很确定,您打开的
将把空工作簿替换为活动工作簿,但是更新空工作簿而不是以后打开的工作簿可能会解释您的更改没有保存的原因。我将删除创建空工作簿的语句,然后重试。