适用于Excel的Backgroundworker在测试环境中运行良好,但在发布后无响应 [英] Backgroundworker for excel works fine at test environment but non responsive after publishing

查看:54
本文介绍了适用于Excel的Backgroundworker在测试环境中运行良好,但在发布后无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们。

我在服务器上生成一个excel文件,供客户下载。

我正在使用backgroundworker,它的工作原理在我的本地计算机上测试时很好

一旦发布,我可以在服务器运行时看到IIS Woeker Process(32)。

但没有任何反应



我尝试了什么:



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:

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')");
        }
    }
}

推荐答案

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

}
catch (Exception ex) { }



Any你在生产代码中有这个的原因???



如果某些东西不起作用,你需要开始调试。 Visual Studio具有远程调试功能。你甚至可以设置断点并逐步完成代码。在此处阅读更多内容: Visual Studio中的远程调试Microsoft Docs [ ^ ]



此外,您是否认为您的网络服务器上可能没有安装Excel?



更新



即使您可以解决问题,在Web服务器上运行Excel也可能会出现问题。通常,Web服务器可以具有单个,数十个,数百个,数千个等同时连接。 excel客户端的设计并未考虑到这一目的。你会更好:

1.找到可以使用excel文件的第三方付费或免费库;

2.只需生成可下载并导入Excel的CSV文件;

3.使用Office 365 API并使用它来生成文件。 Office 365旨在处理这些类型的工作负载。


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.


您在Web应用程序中使用Excel Interop。首先,为了使其有机会工作,必须在服务器上安装Office。



但是,你不能这样做。 Office应用程序是不可重入的,这意味着如果两个Web请求同时进入并且同时使用Excel执行某些操作,Excel将崩溃或执行非常意外且几乎无法再现的事情。



此外,Excel中的某些操作需要在服务器上的控制台上主动登录用户才能工作,因为他们提出了自己的对话框。在Web服务器上,该用户永远不会登录以响应对话框。这些对话框不会显示在客户端计算机上。 Excel将挂起,永远等待永远不会到达的输入。



您不能在Web应用程序中使用Office Interop。您必须重写此代码以使用库(例如OpenXML SDK,ClosedXML或其他库)对工作簿执行操作。
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.


得到它Graeme。谢谢你的建议。

我在另一个中尝试过这个错误。



检索带有CLSID的组件的COM类工厂{00024500 -0000-0000-C000-000000000046}由于以下错误而失败:80070005访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED))。







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);
}


这篇关于适用于Excel的Backgroundworker在测试环境中运行良好,但在发布后无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆