Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends.
I am generating a excel file at server to be downloaded for client.
I am using backgroundworker and it works fine while testing at my local computer
Once published I can see IIS Woeker Process(32) at server running.
But nothing happens

What I have tried:

C#
namespace SCQA
{
    public partial class TestMaster : System.Web.UI.Page
    {
        BackgroundWorker bgView = new BackgroundWorker();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnBackGround_Click(object sender, EventArgs e)
        {
            bgView = new BackgroundWorker();
            bgView.WorkerSupportsCancellation = true;
            bgView.DoWork += new DoWorkEventHandler(bw_DoWork);
            bgView.WorkerReportsProgress = true;            
            bgView.RunWorkerAsync();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            Excel.Application xlApp;
            Excel.Worksheet xlWorkSheet;
            xlApp = new Excel.Application();
            xlWorkSheet = new Excel.Worksheet();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add("C:\\Model.xlsx");
            xlWorkSheet = xlWorkBook.ActiveSheet;
            xlApp.DisplayAlerts = false;
            try
            {
                var _with1 = xlWorkSheet;
                _with1.Cells[1, 1] = "SCQA OVER TIME on - " + DateTime.Now.ToString("dd/MMM/yyyy");

            }
            catch (Exception ex) { }

            string fInfo = "D:\\SCQA_DOC\\ModelOut.xlsx";
            xlWorkBook.SaveAs(fInfo, xlWorkBook.FileFormat, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlExclusive, false, false, Type.Missing, Type.Missing, Type.Missing);
            xlWorkBook.Close();
            xlApp.DisplayAlerts = true;
            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);
            //Button5_Click(null, EventArgs.Empty);        
            Response.Write("alert('Completed')");
        }
    }
}
Posted
Updated 19-Sep-17 4:24am
v2
Comments
Graeme_Grant 19-Sep-17 7:10am    
IIS may be blocking access to the app. Please look at the link about remote debugging to see what is actually happening on the server.

C#
try
{
	var _with1 = xlWorkSheet;
	_with1.Cells[1, 1] = "SCQA OVER TIME on - " + DateTime.Now.ToString("dd/MMM/yyyy");

}
catch (Exception ex) { }

Any reason why you have this in production code???

If something does not work, you need to start debugging. Visual Studio has a remote debugging feature. you can even set breakpoints and step through the code. Read more here: Remote debugging in Visual Studio | Microsoft Docs[^]

Also, have you considered that Excel may not be installed on your web server?

UPDATE

Running excel on a web server may be very problematic even if you can resolve your issue. Typically, web servers can have single, tens, hundreds, thousands, etc of simultaneous connections. The excel client is not designed with this purpose in mind. You would be better off:
1. finding a third-party paid or free library that can work with excel files;
2. simply generate CSV files that can be downloaded and imported into Excel;
3. use an Office 365 API and use it to generate your files. Office 365 is designed to handle these types of workloads.
 
Share this answer
 
v3
Comments
OriginalGriff 19-Sep-17 7:05am    
"Hi Graeme_Grant
I want to create a Excel file in the server (set defaults and set macros in that excel)before delivering to client.
I have installed same office version in that server and other Windows Applications able to run in that server."

Posted as a solution.
Graeme_Grant 19-Sep-17 7:10am    
Thanks... posted repy against his question.
You're using Excel Interop in a web application. First, in order for that to even have a chance of working, Office has to be installed on the server.

But, you can't do that. Office applications are non-reentrant, meaning if two web requests come in and both do something with Excel at the same time, Excel will crash or do things that are very unexpected and just about impossible to reproduce.

Also, some operations in Excel require an actively logged-in user at the console on the server in order to work because they put up their own dialog boxes. On a web server, that user will never be logged in to respond to dialogs. These dialogs will NOT show up on the client machines. Excel will hang, forever waiting for input that will never arrive.

You cannot use Office Interop in a web application. You MUST rewrite this code to do the operations to workbooks using a library, such as the OpenXML SDK, ClosedXML, or some other library.
 
Share this answer
 
Got it Graeme. Thanks for your suggestion.
I tried it in another I get this error.

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).



 protected void Button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (int i = 1; i <= 100; i++)
                for (int j = 1; j < 100; j++)
                    xlWorkSheet.Cells[i, j] = i + "  : " + j;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; 
            filename=translationText.xlsx");
            Response.Write("<table>");
            for (int i = 1; i <= 100; i++)
            {
                Response.Write("<tr>");
                for (int j = 1; j < 100; j++)
                {
                    Response.Write("<td>" + i + "  : " + j + "</td>");
                }
                Response.Write("</tr>");
            }
            Response.Write("</table>");
            Response.Flush();
            Response.End();

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
}
 
Share this answer
 
Comments
Dave Kreskowiak 19-Sep-17 10:20am    
Stop posting replies to other posts as SOLUTIONS to your own question. By doing so, the person you're replying to is NOT getting notified that you posted anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900