当前位置:编程学习 > C#/ASP.NET >>

c# 网页触发后台进程创建excel,会报一个error 磁盘空间或内存不足的信息

这是错误信息

Server Error in '/Web' Application.
--------------------------------------------------------------------------------

Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space. 

• To make more memory available, close workbooks or programs you no longer need. 

• To free disk space, delete files you no longer need from the disk you are saving to. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space. 

• To make more memory available, close workbooks or programs you no longer need. 

• To free disk space, delete files you no longer need from the disk you are saving to.
--------------------编程问答-------------------- 这是我的代码:
try
        {
            DataSet Ds = new DataSet();
            Ds.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + @"\DataSet.xml");
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel._Workbook xBk = excel.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel._Worksheet xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
            Microsoft.Office.Interop.Excel.Range excelCell = null;
            try
            {
                DataTable Dt = Ds.Tables[1];
                int RowLoc;
                for (int i = 0; i < Dt.Columns.Count; ++i)
                {
                    string tmp = Dt.Rows[0][i].ToString();
                    RowLoc = 0;
                    for (int j = 0; j < Dt.Rows.Count; ++j)
                    {
                        if (tmp == Dt.Rows[j][i].ToString())
                        {
                            if (RowLoc < j)
                            {
                                for (int CurrCol = 0; CurrCol < i; ++CurrCol)
                                {
                                    if (Dt.Rows[j - 1][CurrCol].ToString() != Dt.Rows[j][CurrCol].ToString())
                                    {
                                        tmp = Dt.Rows[j][i].ToString();
                                        RowLoc = j;
                                        xSt.Cells[j + 1, i + 1] = tmp;
                                        break;
                                    }
                                }
                                excelCell = xSt.get_Range(excel.Cells[RowLoc + 1, i + 1], excel.Cells[j+1, i + 1]);
                                excelCell.Select();
                                excelCell.Merge(false);
                                excelCell.Value2 = tmp;
                                tmp = Dt.Rows[RowLoc][i].ToString();
                                RowLoc = j;
                            }
                            else
                            {
                                tmp = Dt.Rows[j][i].ToString();
                                RowLoc = j;
                                xSt.Cells[j + 1, i + 1] = tmp;
                            }
                        }
                        else
                        {
                            tmp = Dt.Rows[j][i].ToString();                                                       
                            RowLoc = j;
                            xSt.Cells[j + 1, i + 1] = tmp; 
                        }
                    }
                }
                excel.Visible = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                excelCell = null;
                xBk = null;
                xSt = null;
                excel = null;
                GC.Collect();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        } --------------------编程问答-------------------- 请问是什么原因造成的。尝试过给aspnet  iisuser加工作目录权限等,没有用。不理解,磁盘空间和内存都是正常的,为什么会报 磁盘内存不足呢? --------------------编程问答-------------------- 不止到 --------------------编程问答-------------------- 先帮你顶一下 --------------------编程问答-------------------- 我前几天通过以下方法解决了这个问题:
环境:windows2003 + excel 2007 + IIS6.0
首先没有使用模拟
找到组件服务里面的excel组件,选属性,进入安全页里面
将“启动和激活权限”改为自定义,并添加network service (如果是IIS5 则为aspnet) 
将“访问权限”改为自定义,并添加network service (如果是IIS5 则为aspnet) 
你试试看 --------------------编程问答-------------------- 查了一下资料,office不支持server端的automation。
http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757
谢谢大家的关注 --------------------编程问答-------------------- to lxd2006() :
谢谢你的提示,我刚刚按照步骤作了一下,结果依然。

我想问问  “首先没有使用模拟”   是什么意思?不太明白。  呵呵 --------------------编程问答-------------------- 是 模拟用户 的意思 --------------------编程问答-------------------- 另外 你注意看看是不是把ASPNET和network Service 都设置上了,如果是就去掉多余的,我当时就是多设了ASPNET造成的
如果还不行,建议换个机器和其他版本的EXCEL试试 --------------------编程问答-------------------- System.AppDomain.CurrentDomain.BaseDirectory 不建议用这个,如果写应用程序用这个我想是应该的,但是你要是写ASP.NET用这个恐怕就有点问题了 --------------------编程问答-------------------- 好几天了,都没有解决这个问题。我快疯掉了。
那位达人能解决啊? --------------------编程问答-------------------- var xl = new ActiveXObject("Excel.Application");

//加上这句就不会出错了!
xl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8;
var oBook = xl.Workbooks.Add();  --------------------编程问答-------------------- 可能是那个WEB服务器所在的盘空间不够吧
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,