G
Guest
Hi There
I have been having a play around with the following code to display a
datagrid in Excel (all from Steve Orr's site):
Private Sub btnTechServAccred_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTechServAccred.Click
Dim AcredDT As DataTable = UserSession.Current.AcredTable
Dim aExcel As New Microsoft.Office.Interop.Excel.Application
Dim aBooks As Microsoft.Office.Interop.Excel.Workbooks
Dim aBook As Microsoft.Office.Interop.Excel.Workbook
Dim aSheets As Microsoft.Office.Interop.Excel.Sheets
Dim aSheet3 As Microsoft.Office.Interop.Excel.Worksheet
Dim aSheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim aCells As Microsoft.Office.Interop.Excel.Range
Dim tFile As String, tTemplate As String
tFile = "C:\Accreditations\Excel.xls"
tTemplate = "C:\Accreditations\Template.xls"
aExcel.Visible = False : aExcel.DisplayAlerts = False
aBooks = aExcel.Workbooks
aBooks.Open("C:\Accreditations\Template.xls")
aBook = aBooks.Item(1)
aSheets = aBook.Worksheets
aSheet3 = CType(aSheets.Item(3),
Microsoft.Office.Interop.Excel.Worksheet)
aSheet1 = CType(aSheets.Item(1),
Microsoft.Office.Interop.Excel.Worksheet)
aSheet3.Name = "Raw Data"
aCells = aSheet3.Cells
DumpData(AcredDT, aCells)
aSheet1.SaveAs(tFile)
aBook.Close()
aExcel.Quit()
ReleaseComObject(aCells) : ReleaseComObject(aSheet3)
ReleaseComObject(aSheets) : ReleaseComObject(aBook)
ReleaseComObject(aBooks) : ReleaseComObject(aExcel)
aExcel = Nothing : aBooks = Nothing : aBook = Nothing
aSheets = Nothing : aSheet3 = Nothing : aCells = Nothing
System.GC.Collect()
Response.Redirect(tFile)
End Sub
Private Function DumpData(ByVal _
dt As DataTable, ByVal oCells As Microsoft.Office.Interop.Excel.Range)
As String
Dim dr As DataRow, ary() As Object
Dim iRow As Integer, iCol As Integer
'Output Column Headers
For iCol = 0 To dt.Columns.Count - 1
oCells(2, iCol + 1) = dt.Columns(iCol).ToString
Next
'Output Data
For iRow = 0 To dt.Rows.Count - 1
dr = dt.Rows.Item(iRow)
ary = dr.ItemArray
For iCol = 0 To UBound(ary)
oCells(iRow + 3, iCol + 1) = ary(iCol).ToString
Response.Write(ary(iCol).ToString & vbTab)
Next
Next
End Function
This all works beautifully if I run the page from within Visual Studio 2003
- but as soon as I browse to the page using http://localhost/blah-de-blah it
fails at the point you would expect to see the message from the browser
asking if you want to open or save the file. Browser simply comes back with:
"The Page Cannot Be Displayed"
I get the impression that it is failing when it tries to overwrite the
"MyExcel" temporary document - and I have no doubt I am doing something daft
or have forgotten something obvious... any help would be appreciated.
I have set impersonation "Yes" and the IUSR account has permission to write
to the relevant excel files on the C: drive
Thanks
I have been having a play around with the following code to display a
datagrid in Excel (all from Steve Orr's site):
Private Sub btnTechServAccred_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTechServAccred.Click
Dim AcredDT As DataTable = UserSession.Current.AcredTable
Dim aExcel As New Microsoft.Office.Interop.Excel.Application
Dim aBooks As Microsoft.Office.Interop.Excel.Workbooks
Dim aBook As Microsoft.Office.Interop.Excel.Workbook
Dim aSheets As Microsoft.Office.Interop.Excel.Sheets
Dim aSheet3 As Microsoft.Office.Interop.Excel.Worksheet
Dim aSheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim aCells As Microsoft.Office.Interop.Excel.Range
Dim tFile As String, tTemplate As String
tFile = "C:\Accreditations\Excel.xls"
tTemplate = "C:\Accreditations\Template.xls"
aExcel.Visible = False : aExcel.DisplayAlerts = False
aBooks = aExcel.Workbooks
aBooks.Open("C:\Accreditations\Template.xls")
aBook = aBooks.Item(1)
aSheets = aBook.Worksheets
aSheet3 = CType(aSheets.Item(3),
Microsoft.Office.Interop.Excel.Worksheet)
aSheet1 = CType(aSheets.Item(1),
Microsoft.Office.Interop.Excel.Worksheet)
aSheet3.Name = "Raw Data"
aCells = aSheet3.Cells
DumpData(AcredDT, aCells)
aSheet1.SaveAs(tFile)
aBook.Close()
aExcel.Quit()
ReleaseComObject(aCells) : ReleaseComObject(aSheet3)
ReleaseComObject(aSheets) : ReleaseComObject(aBook)
ReleaseComObject(aBooks) : ReleaseComObject(aExcel)
aExcel = Nothing : aBooks = Nothing : aBook = Nothing
aSheets = Nothing : aSheet3 = Nothing : aCells = Nothing
System.GC.Collect()
Response.Redirect(tFile)
End Sub
Private Function DumpData(ByVal _
dt As DataTable, ByVal oCells As Microsoft.Office.Interop.Excel.Range)
As String
Dim dr As DataRow, ary() As Object
Dim iRow As Integer, iCol As Integer
'Output Column Headers
For iCol = 0 To dt.Columns.Count - 1
oCells(2, iCol + 1) = dt.Columns(iCol).ToString
Next
'Output Data
For iRow = 0 To dt.Rows.Count - 1
dr = dt.Rows.Item(iRow)
ary = dr.ItemArray
For iCol = 0 To UBound(ary)
oCells(iRow + 3, iCol + 1) = ary(iCol).ToString
Response.Write(ary(iCol).ToString & vbTab)
Next
Next
End Function
This all works beautifully if I run the page from within Visual Studio 2003
- but as soon as I browse to the page using http://localhost/blah-de-blah it
fails at the point you would expect to see the message from the browser
asking if you want to open or save the file. Browser simply comes back with:
"The Page Cannot Be Displayed"
I get the impression that it is failing when it tries to overwrite the
"MyExcel" temporary document - and I have no doubt I am doing something daft
or have forgotten something obvious... any help would be appreciated.
I have set impersonation "Yes" and the IUSR account has permission to write
to the relevant excel files on the C: drive
Thanks