none
Creating Excel Files on Web Server - Interop or OpenXML RRS feed

  • Question

  • I am using .nt 4 and C# on Windows 7.

    I have a template Excel worksheet with two images and I need to fill in appropriate data and create an individual Excel file and then save it as pdf.

    Using Interop, I am able to create both xlsx and pdf files on the server correctly in debug mode.  But when running as client, I get an error message as below

    COM object that has been separated from its underlying RCW cannot be used.

    The files actually do get created. But the execution stops after that. This was not working before until I added the following code.

    System.Runtime.InteropServices.Marshal.ReleaseComObject(xcel);
    
                    System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
                    foreach (System.Diagnostics.Process p in proc)
                    {
                        if (p.MainWindowTitle.Length == 0)
                            p.Kill();
                    }

    The only reason I need to create an Excel file is so I can save it as pdf easily and the template is an Excel file. The template also contains images. 

    I do not mind using Open XML, but not sure how to do that.  Would it be better if I grab the instance handle and kill that? The above code may not kill the right instance - assuming I can get rid of the error.

    Any help would be greatly appreciated and I am sorry if I posted in a wrong forum. 

    Thanks much in advance.

    • Moved by DotNet Wang Wednesday, March 2, 2016 6:39 AM
    Tuesday, March 1, 2016 3:25 PM

Answers

  • I decided to use OpenXML to accomplish this.

    Some of the helpful links are as below:

    http://stackoverflow.com/questions/12096760/modifying-a-cell-with-openxml-in-vb-net

    The above is VB, but easy to convert that to C#.

    https://msdn.microsoft.com/en-us/library/office/cc861607.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by tinadusa Wednesday, March 2, 2016 11:10 AM
    Wednesday, March 2, 2016 11:10 AM

All replies

  • Hi tinadusa,

    Based on your description, your case more related to Excel, I will move your case to Excel for developers forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 2, 2016 6:38 AM
  • Thanks Kevin.
    Wednesday, March 2, 2016 11:08 AM
  • I decided to use OpenXML to accomplish this.

    Some of the helpful links are as below:

    http://stackoverflow.com/questions/12096760/modifying-a-cell-with-openxml-in-vb-net

    The above is VB, but easy to convert that to C#.

    https://msdn.microsoft.com/en-us/library/office/cc861607.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by tinadusa Wednesday, March 2, 2016 11:10 AM
    Wednesday, March 2, 2016 11:10 AM