在服务器端生成Excel文件然后从服务器下载的本地的代码
這個方法適用所有類型文件,你可以適當修改代碼就行了???????
?protected void Button1_Click(object sender, EventArgs e)
 ??????? {
 ????????? object???? filename = Guid.NewGuid()? ;
 ??????????? //生成一個新的文件名用全球唯一標識符 (GUID)來標識
 ??????????? string newpath = String.Format(@"{0}\Excel\{1}.xls", Server.MapPath("."), filename );
 ??????????? //調用的模板文件
 ??????????? 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;
????????????? 
 ??????????????? //調用模板
 ??????????????? tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, 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; //設置Range的背景色 
??????????????? //tworksheet.Cells[1, 2] = "測試";
 ??????????????? //tworksheet.Cells[2, 2] = "測試";
 ??????????????? //app.Visible = true;
 ??????????????? tworksheet.SaveAs(newpath, missing, missing, missing, missing, missing, missing, missing, 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.");
 ??????????????? }
 ??????????? }? 
 ??????????????? //強制對所有代進行垃圾回收
 ??????????????? GC.Collect();
 ??????????? }
??????? }
轉載于:https://www.cnblogs.com/alcome/archive/2011/05/03/2035385.html
總結
以上是生活随笔為你收集整理的在服务器端生成Excel文件然后从服务器下载的本地的代码的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 取数函数设置向导返回值说明
- 下一篇: ASP.NET 3.5核心编程学习笔记(
