日期:2014-05-17  浏览次数:20490 次

ASP.NET操作 EXCEL后进程无法结束
我的网站根目录下面有一个模板文件,程序里面将数据计算出来以后就写入那个模板文件里面去,然后另存到服务器上面的
D:\\Reports\\Week_Product_Yield_Reports.xls
最后将D盘的这个Excel文件下载到用户本地,我在我本地测试没有问题,可以结束Excel进程,但是服务器上面就一直结束不掉。网上的方法都试过了,全都无效,有人说通过dcomcnfg来配置权限,但是我服务器上面 dcomcnfg 里面的Microsoft.Excel.Application的属性根本都编辑不了,全部是灰色的,知道的友友说说看,这里上传不了图片。


C# code

 #region 将数据写入到服务器上面的Excel模板
    /// <summary>
    /// 将数据写入到服务器上面的Excel模板
    /// </summary>
    public void WriteToExcel()
    {
        string filepath = Server.MapPath(@"~/Samples/week_product_yield.xls");

        Excel.Application xApp = new Excel.ApplicationClass();
        Excel.Workbook xBook = xApp.Workbooks._Open(filepath,
          Missing.Value, Missing.Value, Missing.Value, Missing.Value
          , Missing.Value, Missing.Value, Missing.Value, Missing.Value
          , Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];

        ////////////////////////////////////////////////////




        if (this.txtStartTime.Text.Trim() != "" && this.txtEndTime.Text.Trim() != "")
        {
            _TheStartTime = this.txtStartTime.Text.Trim().Substring(0, 10);
            _TheEndTime = this.txtEndTime.Text.Trim().Substring(0, 10);

            DateTime stStart = Convert.ToDateTime(_TheStartTime);
            DateTime stEnd = Convert.ToDateTime(_TheEndTime);
            if (DateTime.Compare(stStart, stEnd) > 0)
            {
                this.lblMessage.Text = "警告:结束时间必须大于开始时间!";
                this.lblMessage.ForeColor = Color.Red;
                return;
            }

            if (dt.Rows.Count > 0)
            {
                dt.Clear();
            }

            DateTime TheStartTime = Convert.ToDateTime(_TheStartTime);

            DateTime TheEndTime = Convert.ToDateTime(_TheEndTime);
            if (this.ddlModelList.Text == "80-A368-00W00")
            {
                dt = rm.GetWeekProductYidldReportData(_TheStartTime, _TheEndTime);
            }
            else if (this.ddlModelList.Text == "80-A368-10W00")
            {
                dt = rm.GetWeekProductYidldReportData2(_TheStartTime, _TheEndTime);
            }
            else
            {
                dt = rm.GetWeekProductYidldReportData3(TheStartTime, TheEndTime);
            }
          
        }


        //////////////////////////////////////////////////

               Excel.Range rng4 = xSheet.get_Range("B9", Missing.Value);
            rng4.Value2 = dt.Rows[0]["FTOTQTY"].ToString();

            Excel.Range rng5 = xSheet.get_Range("B10", Missing.Value);
            rng5.Value2 = dt.Rows[0]["FFQTY"].ToString();

            Excel.Range rng6 = xSheet.get_Range("B11", Missing.Value);
            if (Convert.ToInt32(rng5.Value2) == 0 && Convert.ToInt32(rng4.Value2) != 0)
            {
                rng6.Value2 = "0.00%";
            }
            else if (Convert.ToInt32(rng5.Value2) == 0 && Convert.ToInt32(rng4.Value2) == 0)
            {
                rng6.Value2 = "0.00%";
            }
            else
            {
                rng6.Value2 = (Convert.ToDouble(rng5.Value2) / Convert.ToInt32(rng4.Value2)) * 100 + "%";
            }


         


            Excel.Range rng10 = xSheet.get_Range("D9", Missing.Value);
            rng10.Value2 = dt.Rows[2]["FTOTQTY"].ToString();

            Excel.Range rng11 = xSheet.get_Range("D10", Missing.Value);
            rng11.Value2 = dt.Rows[2]["FFQTY"].ToString();

            Excel.Range rng12 = xSheet.get_Range("D11", Missing.Value);

            if (Convert.ToInt32(rng11.Value2) == 0 && Convert.ToInt32(r