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

我打算像这样运行代码xlWorkSheet.Cells(rownumber,1)=“Some Value”

问题是当我打开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") 

您需要保存,closures并释放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