Post

Asp.Net 直接写一个Excel文件流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
this.Page.Response.Clear(); 
this.Page.Response.BufferOutput = true; 
//Define Chareset  
this.Page.Response.Charset = "GB2312"; 
//For download.  
//Define type.  
this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename = ExcelFile.xls"); 
//Define encoding.  
this.Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
//Define Application type.  
this.Page.Response.ContentType = "application/ms-Excel"; 

string strExcelHeader = string.Empty; 
string strExcelItems = string.Empty; 

//Write header;  
//Please pay a ation to the character "\t"  
strExcelHeader = "Column1\tColumn2\tColumn3\tColumn4\tColumn5\tColumn6\tColumn7\tColumn8\ttColumn9\t\n"; 

strExcelItems = "1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t\n"; 
//Write Excel header.  
this.Page.Response.Write(strExcelHeader); 

//Write a Excel item.  
this.Page.Response.Write(strExcelItems); 

this.Page.Response.End();
This post is licensed under CC BY 4.0 by the author.