从IIS运行应用程序(asp.net)时无法创建Excel文件 [英] Unable to create the excel file when the application (asp.net) is run from IIS

查看:191
本文介绍了从IIS运行应用程序(asp.net)时无法创建Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从IIS运行应用程序时,无法在特定路径上创建excel文件.但是它是在使用Visual Studio 2008调试代码时创建的.即使我已经尝试使用绝对路径并使用server.mappath()函数,但徒劳地尝试了.服务器抛出错误:HTTP404.您正在寻找的资源(或一个)的依赖项)可能已被删除,名称更改或暂时不可用.请查看以下网址,并确保其拼写正确. 要求的网址:/Error.aspx

Unable to create the excel file at particular path when the application is run from IIS. But its created when the code is debuged using visual studio 2008.Even i have tried out using absolute path and using server.mappath() function but in vain.Server throws the error : HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Error.aspx

代码如下:

private void CreateExcel(DataTable dt)
    {
        try
        {

        FilePath =  "\\\\192.168.1.252\\GNC Reports\\TallyExport.xls";
            Excel.Application oXL = new Excel.Application();

            //Get a new workbook.
            Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Type.Missing));

            // *************** Sheet 1
            Excel._Worksheet oSheet = (Excel._Worksheet)oWB.Sheets["Sheet1"];
            oSheet.Name = "Journal";
            WriteWxcel(oSheet, dt, 3);

            //***************** Sheet 2
            oSheet = (Excel._Worksheet)oWB.Sheets["Sheet2"];
            oSheet.Name = "Payroll";
            WriteWxcel(oSheet, dt, 4);

            // ************* Sheet 3
            oSheet = (Excel._Worksheet)oWB.Sheets["Sheet3"];
            oSheet.Name = "Receipt";
            WriteWxcel(oSheet, dt, 2);

            //Save Excel File
            oWB.SaveAs(FilePath, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            oXL.Quit();

        }
        catch (Exception ex)
        {
            BussinessLayer.CMSException.Instance.HandleMe(this, ex);
        }

    }

推荐答案

MS不支持在类似服务器的方案(IIS/ASP.NET/Windows服务...)中使用Office Interop -请参见 http://support.microsoft.com/default.aspx?scid= kb; EN-US; q257757#kb2

另一点是,由于Windows Vista发生了一些与安全相关的更改,这些更改阻止了Windows服务执行任何类似桌面的"操作(例如,打印,写入网络共享...).

Another point is that since windows vista there have been several security-related changed which prevent doing any "desktop-like" things (for example printing, writing to a network share...) from a Windows Service...

互操作的替代项:

有许多选项可以在不使用Interop的情况下读取/编辑/创建Excel文件:

There are many options to read/edit/create Excel files without Interop:

MS提供了免费的OpenXML SDK V 2.0-请参见 http://msdn .microsoft.com/en-us/library/bb448854%28office.14%29.aspx (仅适用于XLSX)

MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx (XLSX only)

这可以读写MS Office文件(包括Excel).

This can read+write MS Office files (including Excel).

另一个免费选项,请参见 http://www.codeproject.com/KB/office/OpenXML.aspx(仅适用于XLSX)

Another free option see http://www.codeproject.com/KB/office/OpenXML.aspx (XLSX only)

如果您需要更多的东西来处理较旧的Excel版本(例如XLS,不仅是XLSX),渲染,创建PDF,公式等,那么还有不同的免费和商业库,例如 EPPlus (免费,仅XLSX),Aspose.Cells LibXL Flexcel 等.

IF you need more like handling older Excel versions (like XLS, not only XLSX), rendering, creating PDFs, formulas etc. then there are different free and commercial libraries like ClosedXML (free, XLSX only), EPPlus (free, XLSX only), Aspose.Cells, SpreadsheetGear, LibXL and Flexcel etc.

这篇关于从IIS运行应用程序(asp.net)时无法创建Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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