By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

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

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8.  
  9. namespace TU_Sofia_PublicationSystem
  10. {
  11.     public partial class Default : System.Web.UI.Page
  12.     {
  13.         protected void Page_Load(object sender, EventArgs e)
  14.         {
  15.  
  16.         }
  17.  
  18.         protected void ExportToExcel(object sender, EventArgs e)
  19.         {
  20.             Excel.Application excelApp = new Excel.Application();
  21.             excelApp.Workbooks.Add();
  22.             Excel._Worksheet workSheet = excelApp.ActiveSheet;
  23.             workSheet.Cells[1, "A"] = "Make";
  24.             workSheet.Cells[1, "B"] = "Colorqqqqqqqqq";
  25.             workSheet.Cells[1, "C"] = "Pet Namesdvbpofsdajnvbdajbi";
  26.             workSheet.SaveAs(@"C:\Inventory.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Excel.XlSaveAsAccessMode.xlNoChange);
  27.             excelApp.Workbooks.Close();
  28.             excelApp.Quit();
  29.             excelApp = null;
  30.         }
  31.     }
  32.  
Now this will work only if in the SaveAs method I change "C:\Inventory.xlsx" 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.
Dec 3 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.