2011-11-28 45 views
0

我將網格表添加到Excel中並向Excel中添加標題。如何寫一些文本到C#中的excel文件#

 string subject = lbl_Subj.Text; 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.ClearHeaders(); 
    Response.AddHeader("Cache-Control", "no-store, no-cache");   
    Response.AddHeader("content-disposition", "attachment;filename=" + subject + "-Status"); 
    Response.Charset = ""; 
    this.EnableViewState = false;  
    Response.ContentType = "application/vnd.ms-excel"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    Grid_UserTable.RenderControl(htmlWrite);   
    rptList.RenderControl(htmlWrite);  
    Response.Write(stringWrite.ToString());  
    Response.End(); 

我怎麼能添加一些文本到Excel中,我想添加一些字符串如

string add="this is the text I want to add to the excel"; 

回答

2

嘗試添加:htmlWrite.WriteLine(add);

string subject = lbl_Subj.Text; 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.ClearHeaders(); 
    Response.AddHeader("Cache-Control", "no-store, no-cache");   
    Response.AddHeader("content-disposition", "attachment;filename=" + subject + "-Status"); 
    Response.Charset = ""; 
    this.EnableViewState = false;  
    Response.ContentType = "application/vnd.ms-excel"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 

    htmlWrite.WriteLine(add); 

    Grid_UserTable.RenderControl(htmlWrite);   
    rptList.RenderControl(htmlWrite);  
    Response.Write(stringWrite.ToString());  
    Response.End(); 
0
hw.RenderBeginTag("strong"); 
hw.Write("this is the text I want to add to the excel" + DateTime.Now); 
hw.RenderEndTag(); 

讓我知道如果您需要任何幫助..

相關問題