Creating Excel File

Discussion in 'ASP.NET / ASP.NET Core' started by cluettinsur, Dec 31, 2010.

  1. I'm told DiscountASP does not support Microsoft Office Excel Namespace and I need to create an Excel File from my Web Application. Does anyone have sample VB code to do this (e.g. using OpenXML)..... thanks.
     
  2. ...I do have an example from Microsoft using LINQ/XML but have not tested it.
    I'll look for it and post a little later.

    note: I did test the LINQ to XML functions on a DASP server and they worked great.
     
  3. Thanks. Look forward to seeing the code.
     
  4. Thanks Mark. Probably a dumb question but how do I reference OpenOfficeXML (I am using VB).
     
  5. All set on referencing OpenOfficeXML. Thanks again for helping me with this. I now am experiencing a system.unauthorizedaccess Exception. Probably need to set some security access on the DiscountASP Server.
     
  6. ...Keep posting, I haven't messed with this so it's in your hands. ;-)
     
  7. Making progress but frustrating its taking so long to do this. I can now create the Excel File and save it on the Web Server. I now am trying to copy it to a local machine and start Excel to view it. Works fine when testing using localhost. When I do it on the Server I get this error. Not sure how to authroize the rights as suggested in the error message. Any help with this is greatly appreciated.

    ---------------- Error Message ---------------------

    Access to the path 'C:\temp\Reconcile.xlsx' is denied.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\temp\Reconcile.xlsx' is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

    Source Error:


    Line 1689: Dim fFile1 As New FileInfo(Server.MapPath(".\Uploads\Reconcile.xlsx"))
    Line 1690: fFile1.CopyTo("C:\temp\Reconcile.xlsx", True)
    Line 1691: Dim ps As New ProcessStartInfo
    Line 1692: ps.UseShellExecute = True
     
  8. You won't be able to get access to the C:\temp folder on the DASP web server - you only have access to the file system area accessible by your account. The code attempting to access C:\temp on the web server will need to be changed.
     
  9. Thanks Joe. I am trying to have the file copied to the local machine - not the c: drive on the Server. Is there a way I can do that?
     
  10. Ok now I understand what you're trying to do - I misunderstood because the code in post #8 in this thread isn't doing that; it's attempting to write a file to the c:\temp folder on the web server so it won't work.

    It sounds like what you need is: in response to some user interaction on your web site, you need to create an excel file and it then immediately be downloaded to the user. Is that right? If it is it's fairly easy to do - let me know if this is what you need and I'll see what I can find for you.
     
  11. Thanks to all. I figured out a workaround that allows my users to create an Excel File from a data view on my Web Server and then invoke Excel to view/print is as follows:

    1. Button that creates the Excel File using OpenOfficeXML and I save it in my directory on the server.
    2. I created a hyperlink that points to the excel file and when then click it it asks to open/save etc.

    Pretty kludgy but it works. Spent way too much time on this but if there is a better way would appreciate folks thoughts.
     
  12. After your code successfully created the spreadsheet file on the web server, you could do something like:

    Code:
    Response.ClearHeaders();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //use application/ms-excel for older xls files
    Response.AddHeader("content-disposition", "inline; filename=Reconcile.xlsx");
    Response.TransmitFile(Server.MapPath(".\Uploads\Reconcile.xlsx"));            
    Response.Flush();
    Response.End();
    
    Notes:
    • Replace Reconcile.xlsx with whatever the file is actually named
    • For cross browser success, ensure the filename contains only valid western dialect ASCII characters
    • When using a method like this, the file should be immediately downloaded to the client following its creation without the requirement to click an <a> tag
     
  13. Joe - thanks so much. It works perfectly.
     
  14. Way to go guys ;-)
     

Share This Page