How to create excel at ASP?

How to create excel at ASP.NET?
How to make excel file at ASP web Page?

In the example belove we open a template Excel file, change it content
and save it as an other file.

You add
using Microsoft.Office.Interop.Excel;
directive and the rest is straight forward.

You must also add
Microsoft.Office.Interop.Excel as reference from Add Reference.

1 Sheets WRss = null;
2 _Worksheet WRws = null;
3
4 string baseFile = Server.MapPath(".") + "\\MyTemplate.xls";
5 string newFile = Server.MapPath(".") + "\\MyFile.xls";
6
7 Microsoft.Office.Interop.Excel.Application oExcel =
8 new Microsoft.Office.Interop.Excel.Application();
9
10 oExcel.Workbooks.Open(baseFile, Type.Missing, false,
11 Type.Missing, Type.Missing, Type.Missing,
12 Type.Missing, Type.Missing, Type.Missing,
13 Type.Missing, Type.Missing, Type.Missing,
14 Type.Missing, Type.Missing, Type.Missing);
15
16 WRss = (Sheets)oExcel.Worksheets;
17 WRws = (_Worksheet)(WRss.get_Item(1));
18
19 WRws.Cells.Font.Size = 12;
20
21 WRws.Cells[7, 3] = "Write Sometyhing";
22
23 if (System.IO.File.Exists(newFile))
24 System.IO.File.Delete(newFile);
25
26 oExcel.Workbooks[1].SaveAs(newFile, Type.Missing,
27 Type.Missing, Type.Missing, Type.Missing, false,
28 Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
29 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
30
31 oExcel.Workbooks.Close();
32 oExcel.Quit();
33
34 Response.Clear();
35 Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
36 Response.AppendHeader("Content-disposition",
37 "attachment; filename=MyFile.xls");
38 Response.TransmitFile(newFile);


NOTE: if you get the

How to download file at ASP?

How to download file at ASP.NET?
How to download file in ASP?

1 string newFile = Server.MapPath(".") + "\\MyFile.xls";
2 Response.Clear();
3 Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
4 Response.AppendHeader("Content-disposition", "attachment; filename=Hesabat.xls");
5 Response.TransmitFile(newFile);