I've been struggling with this code for days, and hope someone can help. I have an asp.net page having a datagrid that displays results from a stored procedure. When the user clicks a button, an Excel file is created and the user downloads that file. Really, I would prefer if i didn't have to create a file on the server every time, since it's only needed for the user to download. I'd prefer if it was stored in memory until it was downloaded and then release it from memory. Is something like that even possible? Also, I CAN'T FIGURE OUT HOW TO KILL THE OBJECT. no matter what I do, it seems that the Excel application is still running. How can i kill the processes???? I found this article, which didn't help at all:
Any suggestions would be greatly appreciated. Here is my code:
Private Sub CreateExcel(ByVal strFile As String, ByVal strQry As String)
' get datatable
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt = GetData(strQry)
Dim EXL As New Microsoft.Office.Interop.Excel.Application()
Dim xlBooks As Microsoft.Office.Interop.Excel.Workbooks = EXL.Workbooks
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = xlBooks.Add
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet = EXL.ActiveSheet
With wSheet
Dim i As Int32 = 0
Dim c As Int32 = 1
For i = 0 To dt.Columns.Count - 1
.Cells(1, c).Value = dt.Columns(i).ColumnName.ToString
c += 1
Next
.Range("A1:Z1"
.Select()
With EXL.Selection.font
.Name = "Verdana"
.FontStyle = "Bold"
.Size = 12
End With
For i = 0 To dt.Rows.Count - 1
For c = 0 To dt.Columns.Count - 1
.Cells((i + 2), (c + 1)).Value = dt.Rows(i).Item(c).ToString
Next
Next
End With
wSheet.Range("A1:Z" & (dt.Rows.Count + 1)).Select()
EXL.Selection.Columns.AutoFit()
wSheet.Range("A1:Z1"
.Select()
With EXL.Selection
.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
End With
' Format numbers
wSheet.Range("A2:Z" & (dt.Rows.Count + 1)).Select()
With EXL.Selection.Font
.Name = "Arial"
.FontStyle = "regular"
.Size = 11
End With
wSheet.Range("A1:Z" & (dt.Rows.Count + 1)).Select()
EXL.Selection.Columns.AutoFit()
Try
wSheet.SaveAs(Server.MapPath("1" & strFile))
Catch ex As Exception
End Try
' Clean up
NAR(wSheet)
xlBook.Close(False)
NAR(xlBook)
xlBooks.Close()
NAR(xlBooks)
EXL.Quit()
NAR(EXL)
EXL = Nothing
GC.Collect()
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
Any suggestions would be greatly appreciated. Here is my code:
Private Sub CreateExcel(ByVal strFile As String, ByVal strQry As String)
' get datatable
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt = GetData(strQry)
Dim EXL As New Microsoft.Office.Interop.Excel.Application()
Dim xlBooks As Microsoft.Office.Interop.Excel.Workbooks = EXL.Workbooks
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = xlBooks.Add
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet = EXL.ActiveSheet
With wSheet
Dim i As Int32 = 0
Dim c As Int32 = 1
For i = 0 To dt.Columns.Count - 1
.Cells(1, c).Value = dt.Columns(i).ColumnName.ToString
c += 1
Next
.Range("A1:Z1"
With EXL.Selection.font
.Name = "Verdana"
.FontStyle = "Bold"
.Size = 12
End With
For i = 0 To dt.Rows.Count - 1
For c = 0 To dt.Columns.Count - 1
.Cells((i + 2), (c + 1)).Value = dt.Rows(i).Item(c).ToString
Next
Next
End With
wSheet.Range("A1:Z" & (dt.Rows.Count + 1)).Select()
EXL.Selection.Columns.AutoFit()
wSheet.Range("A1:Z1"
With EXL.Selection
.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
End With
' Format numbers
wSheet.Range("A2:Z" & (dt.Rows.Count + 1)).Select()
With EXL.Selection.Font
.Name = "Arial"
.FontStyle = "regular"
.Size = 11
End With
wSheet.Range("A1:Z" & (dt.Rows.Count + 1)).Select()
EXL.Selection.Columns.AutoFit()
Try
wSheet.SaveAs(Server.MapPath("1" & strFile))
Catch ex As Exception
End Try
' Clean up
NAR(wSheet)
xlBook.Close(False)
NAR(xlBook)
xlBooks.Close()
NAR(xlBooks)
EXL.Quit()
NAR(EXL)
EXL = Nothing
GC.Collect()
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub