我正在 asp.net 中执行导出到 Excel 的操作,而不使用任何第三方控件。如何为导出的 Excel 工作表指定背景颜色?
根据某些单元格范围,背景颜色可能(不确定)有所不同。比如说单元格 0-5(Excel 中的单元格 A-E)是红色,6-12 是绿色,依此类推。
我怎样才能达到同样的效果?
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();
}
在 Excel 的 HTML 定义中似乎有办法,尽管我没有尝试过,请参阅http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx并查看官方文档(有关 Excel 的 HTML 的帮助文件):http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx
创建 Excel 文件的更好替代方法是使用 Microsoft 的 OpenXML(免费库),请参阅http://msdn.microsoft.com/en-us/office/bb265236 http://msdn.microsoft.com/en-us/office/bb265236 and http://openxmldeveloper.org/ http://openxmldeveloper.org/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)