无法使用c#在asp.net的客户端机器上创建excel文件?

时间:2023-01-15 11:18:31

i am using this code to crate an excel file on client's machine while pressing a button in asp.net

我正在使用此代码在客户端的机器上创建一个excel文件,同时按下asp.net中的按钮

 protected void excldwnlbtn4_Click(object sender, EventArgs e)
{
    write_on_excel("Vendor.xls");
    lblmsg4.Text = "File Downloaded";
}

and the creation of excel file's code is:

并创建excel文件的代码是:

public void write_on_excel(string filepath)
{
    Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
    try
    {

        Excel.Workbook workbook = (Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
        Excel.Worksheet worksheet;


        //// Opening excel file
        //workbook = excelApp.Workbooks.Open(filepath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



        workbook.SaveAs(filepath,
        Excel.XlFileFormat.xlExcel5, Missing.Value, Missing.Value,
        false, false, Excel.XlSaveAsAccessMode.xlNoChange,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        // Get first Worksheet
        worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);

        // Setting cell values

        ((Excel.Range)worksheet.Cells[1, "A"]).Value2 = "Vendor Code";
        ((Excel.Range)worksheet.Cells[1, "B"]).Value2 = "Vendor Password";
        ((Excel.Range)worksheet.Cells[1, "C"]).Value2 = "Vendor Name";


        workbook.Save();
        workbook.Close(0, 0, 0);
    }
    catch (Exception)
    {
        lblmsg4.Text = "File not Downloaded!!";
    }
    finally
    {
        excelApp.Quit();
    }
}

now its working fine in my IIS localhost .. i am getting an empty excel file with those column headers in my hard drive .... but when i am hosting it in a server and making it live then i am not able to create the excel file ... i am getting an error :

现在它在我的IIS localhost工作正常..我得到一个空的excel文件与我的硬盘驱动器中的那些列标题....但是当我在服务器中托管它并使其生效然后我无法创建excel文件...我收到一个错误:

Server Error in '/' Application.

The resource cannot be found.  

what should i do?? where is my fault ? pls help me on this

我该怎么办??我的错在哪里?请帮助我

2 个解决方案

#1


0  

I see several issues here.

我在这看到几个问题。

One is, you are expecting Excel to be present on the server. If you are not sure that it is there, it probably isn't, and your application should check for it. Basically if you get an Exception while trying to instantiate an Excel object, either Excel isn't there or you don't have permissions to execute it.

一个是,您希望Excel出现在服务器上。如果您不确定它是否存在,则可能不存在,您的应用程序应检查它。基本上,如果在尝试实例化Excel对象时遇到异常,则Excel不存在,或者您没有执行权限。

Second is, you are saving your output file (if you can generate it at all) locally on the server. This does not magically send the file to the client. You have to send the file yourself; best is not to create a local copy on the server, but to have your ASP.NET page output the Excel file to a stream instead; the client receives the stream. See here for more info: http://www.gemboxsoftware.com/support/articles/asp-net-excel

其次,您要在服务器上本地保存输出文件(如果可以生成它)。这并不会神奇地将文件发送到客户端。你必须自己发送文件;最好不是在服务器上创建本地副本,而是让您的ASP.NET页面将Excel文件输出到流中;客户端接收流。有关详细信息,请参阅此处:http://www.gemboxsoftware.com/support/articles/asp-net-excel

Finally, there are other ways to generate Excel files. You may use a library such as OpenXML (see http://openxmldeveloper.org/) or you may generate XML files that Excel can read using the built-in System.Xml classes. There are also commercially-available libraries for working with Excel files in .NET, such as Aspose Cells (see http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx).

最后,还有其他方法可以生成Excel文件。您可以使用OpenXML等库(请参阅http://openxmldeveloper.org/),也可以使用内置的System.Xml类生成Excel可以读取的XML文件。还有商业上可用的库,用于在.NET中使用Excel文件,例如Aspose Cells(请参阅http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default。 ASPX)。

#2


0  

That will never create a file on the machine of the browser client (unless that machine is the machine ruinning the web server).

这永远不会在浏览器客户端的机器上创建文件(除非该机器是破坏Web服务器的机器)。

If you want to allow the browser client to download the file, you need to write it directly to the response stream, for example. Have a look at the highest ranked answer on this thread for more

如果要允许浏览器客户端下载文件,则需要将其直接写入响应流,例如。请查看此主题中排名最高的答案

ASP.NET Excel file download on button click

单击按钮上的ASP.NET Excel文件下载

#1


0  

I see several issues here.

我在这看到几个问题。

One is, you are expecting Excel to be present on the server. If you are not sure that it is there, it probably isn't, and your application should check for it. Basically if you get an Exception while trying to instantiate an Excel object, either Excel isn't there or you don't have permissions to execute it.

一个是,您希望Excel出现在服务器上。如果您不确定它是否存在,则可能不存在,您的应用程序应检查它。基本上,如果在尝试实例化Excel对象时遇到异常,则Excel不存在,或者您没有执行权限。

Second is, you are saving your output file (if you can generate it at all) locally on the server. This does not magically send the file to the client. You have to send the file yourself; best is not to create a local copy on the server, but to have your ASP.NET page output the Excel file to a stream instead; the client receives the stream. See here for more info: http://www.gemboxsoftware.com/support/articles/asp-net-excel

其次,您要在服务器上本地保存输出文件(如果可以生成它)。这并不会神奇地将文件发送到客户端。你必须自己发送文件;最好不是在服务器上创建本地副本,而是让您的ASP.NET页面将Excel文件输出到流中;客户端接收流。有关详细信息,请参阅此处:http://www.gemboxsoftware.com/support/articles/asp-net-excel

Finally, there are other ways to generate Excel files. You may use a library such as OpenXML (see http://openxmldeveloper.org/) or you may generate XML files that Excel can read using the built-in System.Xml classes. There are also commercially-available libraries for working with Excel files in .NET, such as Aspose Cells (see http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx).

最后,还有其他方法可以生成Excel文件。您可以使用OpenXML等库(请参阅http://openxmldeveloper.org/),也可以使用内置的System.Xml类生成Excel可以读取的XML文件。还有商业上可用的库,用于在.NET中使用Excel文件,例如Aspose Cells(请参阅http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default。 ASPX)。

#2


0  

That will never create a file on the machine of the browser client (unless that machine is the machine ruinning the web server).

这永远不会在浏览器客户端的机器上创建文件(除非该机器是破坏Web服务器的机器)。

If you want to allow the browser client to download the file, you need to write it directly to the response stream, for example. Have a look at the highest ranked answer on this thread for more

如果要允许浏览器客户端下载文件,则需要将其直接写入响应流,例如。请查看此主题中排名最高的答案

ASP.NET Excel file download on button click

单击按钮上的ASP.NET Excel文件下载