1

i am using this code to crate an excel file on client's machine while pressing a button in 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:

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 :

Server Error in '/' Application.

The resource cannot be found.  

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

Arindam Das
  • 699
  • 4
  • 20
  • 39
  • Do you have Excel installed on your server? (It is a bad idea to create Excel files this way, BTW, but that's another matter...) – Roy Dictus Nov 15 '12 at 10:18
  • i have bought a domain ... so i think they should have excel – Arindam Das Nov 15 '12 at 10:19
  • Is MS Excel installed on server ? You can ask their support people whether it is installed or not. Are you allowed to save files in your specified folder ? – Software Engineer Nov 15 '12 at 10:20
  • can u give an example how to create an excel file? i only know this – Arindam Das Nov 15 '12 at 10:20
  • @ArindamDas: you bought a domain, so they should have Excel? I don't see the connection. Based on this answer, I think we can conclude that there is no Excel installed on your server, and therefore the Excel interop calls you make can only fail. – Roy Dictus Nov 15 '12 at 10:23
  • ok but is the right way to create an excel file? bcoz i got an answer who is stating that this will only create in my machine – Arindam Das Nov 15 '12 at 10:25

2 Answers2

0

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

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

Community
  • 1
  • 1
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
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.

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

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).

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • ok let me try that gameboxsoftware link i think that will work and 1 more qus sir .. can i use .csv file instead of excel ? will that have any dependency on the hosting site? – Arindam Das Nov 15 '12 at 11:11
  • You can use CSV if you want, sure, that has nothing to do with the hosting provider. What matters is *how* you generate your output. If you just use plain ASP.NET to create CSV, and if that is enough for your purposes, it's probably the easiest and thus best solution. – Roy Dictus Nov 15 '12 at 12:03