在ASP.NET中为Excel工作表提供背景颜色 [英] Give background color to excel sheet in asp.net

查看:261
本文介绍了在ASP.NET中为Excel工作表提供背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不使用任何第三方控件的情况下,在asp.net中执行到excel的导出.如何为导出的Excel工作表赋予背景颜色?

I am performing export to excel in asp.net without using any third party controls. How can i give background color to my exported excel sheet?

根据某些单元格范围,背景颜色可能(不确定)不同.从单元格0-5说起(excel中的单元格A-E)是红色,6-12是绿色,依此类推.

The background color may (not sure) be different according to some cells range. Say from Cell 0- 5 (Cell A-E in excel) is red color, 6-12 is green and so on and so forth.

我怎么能达到同样的目的?

How can i achieve the same?

public static void DataSetToExcel(System.Data.DataSet dtExport, System.Web.HttpResponse response, string strFileName)
{
    //Clean up the response Object
    response.Clear();
    response.Charset = "";

    //Set the respomse MIME type to excel
    response.ContentType = "application/vnd.ms-excel";

    //Opens the attachment in new window
    response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName.ToString() + ".xls;");
    response.ContentEncoding = Encoding.Unicode;
    response.BinaryWrite(Encoding.Unicode.GetPreamble());

    //Create a string writer
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    //Create an htmltextwriter which uses the stringwriter
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);

    //Instantiate the datagrid

    System.Web.UI.WebControls.GridView dgrExport = new System.Web.UI.WebControls.GridView();

    //Set input datagrid to dataset table
    dgrExport.DataSource = dtExport.Tables[0];

    //bind the data with datagrid
    dgrExport.DataBind();

    //Make header text bold
    dgrExport.HeaderStyle.Font.Bold = true;

    //bind the modified datagrid
    dgrExport.DataBind();

    //Tell the datagrid to render itself to our htmltextwriter
    dgrExport.RenderControl(htmlWrite);

    //Output the HTML
    response.Write(stringWrite.ToString());


    response.End();
}

推荐答案

///使标题文本为粗体

//Make header text bold

 dgrExport.HeaderStyle.Font.Bold = true;
 dgrExport.HeaderStyle.BackColor = Color.Black;                            
 dgrExport.HeaderStyle.ForeColor = Color.White;

这篇关于在ASP.NET中为Excel工作表提供背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆