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

时间:2011-10-14 15:57:10

标签: 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")

1 个答案:

答案 0 :(得分:0)

您需要保存,关闭和释放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