This method applies to all types of files. You can modify it as appropriate.CodeThat's all.
Protected void button#click (Object sender, eventargs E)
{
Object filename = guid. newguid ();
// Generate a new file name identified by a globally unique identifier (guid)
String newpath = string. Format (@ "{0} \ Excel \ pai1).xls", server. mappath ("."), filename );
// Called template file
Fileinfo mode = new fileinfo (server. mappath ("3.xls "));
Excel. Application APP = new excel. Application ();
If (APP = NULL)
{
Return;
}
// App. application. displayalerts = false;
// App. Visible = false;
If (mode. exists)
{
Object missing = system. reflection. Missing. value;
Excel. Workbook tworkbook;
// Call the Template
Tworkbook = app. workbooks. open (mode. fullname, missing, and missing );
Excel. _ worksheet tworksheet;
Tworksheet = (Excel. _ worksheet) tworkbook. Sheets [1];
Excel. Range RNG = tworksheet. get_range ("A1", missing. value );
RNG. value2 = "hello ";
RNG. Interior. colorindex = 6; // sets the background color of the range.
// Tworksheet. cells [1, 2] = "test ";
// Tworksheet. cells [2, 2] = "test ";
// App. Visible = true;
Tworksheet. saveas (newpath, missing, missing );
Tworkbook. Save ();
Tworksheet = NULL;
Tworkbook = NULL;
App. Quit ();
App. Quit ();
APP = NULL;
If (newpath! = "")
{
String Path = newpath;
System. Io. fileinfo file = new system. Io. fileinfo (PATH );
If (file. exists)
{
Response. Clear ();
Response. addheader ("content-disposition", "attachment; filename =" + file. Name );
Response. addheader ("Content-Length", file. length. tostring ());
Response. contenttype = "application/octet-stream ";
Response. Filter. Close ();
Response. writefile (newpath );
Response. End ();
}
Else
{
Response. Write ("this file does not exist .");
}
}
// Forcibly recycle all generations
GC. Collect ();
}
}