Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 13th, 2011, 10:05 AM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel with IIS

Imar,

I have a page with a DropDownList, a GridView and 4 Buttons. The DropDowList is a list of available reports. The Gridview displays the results of a selected report. The buttons btnExcel, btnText and btnXML create an Excel file, a tab-delimited file and an XML file respectively in a temp directory. The btnEmail attaches the created file(s) and sends it/them to the logged-in user.

Everything works fine when using the built-in web server of Visual Studio, however when using IIS with Windows Vista, the Excel file does not get created. When using http://localhost, the tab-delimited and XML file work fine and so does sending the email. The only problem is the creation of the Excel file.

Network Service and IUSR has access to the temp directory. Having access to the temp directory cannot be the problem, since if it was the tab-delimited and XML file would not work. Is there some other account that needs to be used for Excel to work? Below is the code for buttons btnExcel, btnText and btnXML.

Code:
Partial Class Report
  Inherits BasePage
  Dim ds As DataSet, dt As System.Data.DataTable

  Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
    btnEmail.Visible = True
    ds = GetData(GetQueryString())
    dt = ds.Tables(0)
    If ((dt.Columns.Count <> 0) And (dt.Rows.Count <> 0)) Then
      Dim myDirectory As New IO.DirectoryInfo("C:\temp")
      Dim xlApp As New Microsoft.Office.Interop.Excel.ApplicationClass
      Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
      Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
      Dim dc As System.Data.DataColumn, dr As System.Data.DataRow
      Dim colIndex As Integer, rowIndex As Integer, strFileName As String, blnFileOpen As Boolean
      If myDirectory.Exists = False Then myDirectory.Create()
      strFileName = "C:\temp\" & RemoveSpaces(DropDownList1.Items(DropDownList1.SelectedIndex).Text) & ".xlsx"
      blnFileOpen = False
      xlBook = xlApp.Workbooks.Add()
      xlSheet = xlBook.ActiveSheet()
      colIndex = 0
      rowIndex = 0
      For Each dc In dt.Columns
        colIndex = colIndex + 1
        xlApp.Cells(1, colIndex) = dc.ColumnName
      Next
      For Each dr In dt.Rows
        rowIndex = rowIndex + 1
        colIndex = 0
        For Each dc In dt.Columns
          colIndex = colIndex + 1
          xlApp.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
        Next
      Next
      xlSheet.Columns.AutoFit()
      Try
        Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
        fileTemp.Close()
      Catch ex As Exception
        blnFileOpen = False
      End Try
      If System.IO.File.Exists(strFileName) Then System.IO.File.Delete(strFileName)
      xlBook.SaveAs(strFileName)
      xlBook.Close()
      releaseObject(xlBook)
      releaseObject(xlSheet)
    End If
  End Sub

  Protected Sub btnText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnText.Click
    btnEmail.Visible = True
    ds = GetData(GetQueryString())
    dt = ds.Tables(0)
    If ((dt.Columns.Count <> 0) And (dt.Rows.Count <> 0)) Then
      Dim myDirectory As New IO.DirectoryInfo("C:\temp")
      Dim dc As System.Data.DataColumn, dr As System.Data.DataRow, strFileName As String, colIndex As Integer
      If myDirectory.Exists = False Then myDirectory.Create()
      strFileName = "C:\temp\" & RemoveSpaces(DropDownList1.Items(DropDownList1.SelectedIndex).Text) & ".txt"
      Dim writer As StreamWriter = New StreamWriter(strFileName)
      colIndex = 0
      For Each dc In dt.Columns
        colIndex = colIndex + 1
        If colIndex < dt.Columns.Count Then
          writer.Write(dc.ColumnName & vbTab)
        Else
          writer.WriteLine(dc.ColumnName)
        End If
      Next
      For Each dr In dt.Rows
        colIndex = 0
        For Each dc In dt.Columns
          colIndex = colIndex + 1
          If colIndex < dt.Columns.Count Then
            writer.Write(dr(dc.ColumnName) & vbTab)
          Else
            writer.WriteLine(dr(dc.ColumnName))
          End If
        Next
      Next
      writer.Close()
    End If
  End Sub

  Protected Sub btnXML_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnXML.Click
    btnEmail.Visible = True
    ds = GetData(GetQueryString())
    Dim filename As String = "C:\temp\" & RemoveSpaces(DropDownList1.Items(DropDownList1.SelectedIndex).Text) & ".xml"
    Dim stream As New FileStream(filename, FileMode.Create)
    ds.WriteXml(stream, XmlWriteMode.WriteSchema)
    stream.Close()
  End Sub
Any help would be greatly appreciated.

Thanks,

Dave
 
Old April 13th, 2011, 11:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Since this is not directly related to my book, would you mind posting this in a more general ASP.NET forum? This way, others can join the discussion as well.

It looks like a COM Interop issue to me, but let's discuss the intrinsics elsewhere.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
#NAME? error running Excel 2003 VBA in Excel 2007 steveburn Excel VBA 0 October 24th, 2009 08:47 AM
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
iis.msc equivalent in IIS for Win XP Pro? RealDHelix Classic ASP Basics 14 March 18th, 2007 08:28 PM
CreateObject error on IIS 4 but not IIS 5 timtom Classic ASP XML 1 December 5th, 2003 01:26 PM
Excel in win 2000 to excel winxp (excel 2002) Max Excel VBA 3 August 28th, 2003 04:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.