How to make a website that is able to export an excel file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Stefanov
    New Member
    • Dec 2010
    • 1

    How to make a website that is able to export an excel file?

    Hello, I have the following problem:
    I created a website project and I want in that website to be make a button, which will export some data in an excel file. This is the code that I have:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace TU_Sofia_PublicationSystem
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void ExportToExcel(object sender, EventArgs e)
            {
                Excel.Application excelApp = new Excel.Application();
                excelApp.Workbooks.Add();
                Excel._Worksheet workSheet = excelApp.ActiveSheet;
                workSheet.Cells[1, "A"] = "Make";
                workSheet.Cells[1, "B"] = "Colorqqqqqqqqq";
                workSheet.Cells[1, "C"] = "Pet Namesdvbpofsdajnvbdajbi";
                workSheet.SaveAs(@"C:\Inventory.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Excel.XlSaveAsAccessMode.xlNoChange);
                excelApp.Workbooks.Close();
                excelApp.Quit();
                excelApp = null;
            }
        }
    Now this will work only if in the SaveAs method I change "C:\Inventory.x lsx" with "Inventory.xlsx " and the file will be saved in My Documents. If I try to specify a path the following error is shown:

    Microsoft Excel cannot access the file 'C:\094B4000'. There are several possible reasons:
    • The file name or path does not exist.
    • The file is being used by another program.
    • The workbook you are trying to save has the same name as a currently open workbook.

    I suppose that this is tottaly not the way that I have to do it; however I was unable to find some valuable information using Google and a lots of books that I have(from Apress).

    Any help like code, or like in which book can I find information is highly appreciated.
Working...