如何在c#中向excel文件中写入一些文本



我正在将网格表添加到excel中,并将页眉添加到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";

尝试添加: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();

只要您在office 2007/2010上,就可以使用office Open XML-http://msdn.microsoft.com/en-us/office/ee358824

谨致问候,Nitin Rastogi

您不能使用string/html编写器,因为excel是二进制的。

您必须使用Excel PIA来创建Excel工作表。

http://msdn.microsoft.com/en-us/library/ff597926.aspx

hw.RenderBeginTag("strong");
hw.Write("this is the text I want to add to the excel" + DateTime.Now);
hw.RenderEndTag();

如果你需要帮助,请告诉我。。

相关内容

  • 没有找到相关文章

最新更新