C#, Excel Workbook, SaveAs doesn't work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girlswannahavefun
    New Member
    • Aug 2007
    • 4

    C#, Excel Workbook, SaveAs doesn't work

    Hi,
    This is my first post on these forums and I am very new to c# and .Net, so please be gentle:)

    I have an ASP application that is supposed to put data into a workbook and then save it and open it in the browser later on. The program seems to hang on the SaveAs(..) line, it sort of loads and loads and loads and nothing happens. Sometimes it eventually loads the start page of the application (session time out maybe?).

    I was thinking that it might be something with permissions, but everything seems to be in place as far as i understand. This used to work 1-2 months ago, but we've recently installed MS Office 2007 on the server (we had 2003 before).

    Any kind of ideas are appreciated, since i don't even know what to test anymore. An error message would have made things easier but i don't understand why i don't get one...

    This is the code I have:
    Code:
    Excel.Application excelApp = new Excel.Application();
    excelApp.Visible = false;
     
    System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture ; 
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 
    
    excelApp.DisplayAlerts = false;
    Excel.Workbook eWorkbook = excelApp.Workbooks.Open(templateFolder+c.prodCode+".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    
    this.fillTechDetails(eWorkbook, ds, c, templateFolder);
    this.fillTermsAndConditions(eWorkbook, c);
    
    eWorkbook.SaveAs(fileFolder+c.ID+c.revision+".xls", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    Thanks in advance!
    //Anna
    Last edited by Frinavale; Oct 7 '09, 01:22 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Welcome to TSDN. Suggest opening file in word 2007 to see if there is a problem. If it starts loading unlikely to be permissions. You say you are using ASP this may be the problem, try updating the code. HTH.

    Comment

    • girlswannahavefun
      New Member
      • Aug 2007
      • 4

      #3
      Thank you for you reply!
      I have now managed to solve this problem by changing this

      Code:
      eWorkbook.SaveAs(fileFolder + c.ID + c.revision + ".xls", Excel.XlFileFormat.[B]xlWorkbookNormal[/B], Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      to this:
      Code:
      eWorkbook.SaveAs(fileFolder + c.ID + c.revision + ".xls", Excel.XlFileFormat.[B]xlXMLSpreadsheet[/B], Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      I'm not sure why it worked but it worked! :)

      Originally posted by kenobewan
      Welcome to TSDN. Suggest opening file in word 2007 to see if there is a problem. If it starts loading unlikely to be permissions. You say you are using ASP this may be the problem, try updating the code. HTH.

      Comment

      • majidkorai
        New Member
        • Jun 2009
        • 6

        #4
        THanks

        Well Thanks ......
        This helped me a lot. I was also having the same issue.

        Majid
        -------------------------------------------------------------------------------------

        Originally posted by girlswannahavef un
        Thank you for you reply!
        I have now managed to solve this problem by changing this

        Code:
        eWorkbook.SaveAs(fileFolder + c.ID + c.revision + ".xls", Excel.XlFileFormat.[B]xlWorkbookNormal[/B], Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        to this:
        Code:
        eWorkbook.SaveAs(fileFolder + c.ID + c.revision + ".xls", Excel.XlFileFormat.[B]xlXMLSpreadsheet[/B], Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        I'm not sure why it worked but it worked! :)

        Comment

        Working...