c# - 如何在将数据集导出到 excel 时使标题字体加粗?

标签 c# asp.net export-to-excel htmlwriter

我在这里将数据集中的数据表导出到 excel。如何单独使数据表的标题字体看起来很粗。这是我的代码

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
foreach (DataTable table in dtInputParameters.Tables)
{
   dataExportExcel.DataSource =  table;
   dataExportExcel.DataBind();
   dataExportExcel.RenderControl(htmlWrite);
   htmlWrite.WriteLine("<br/>");
   // htmlWrite.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold");
}
StringBuilder sbResponseString = new StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheetName + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
sbResponseString.Append(stringWriter + "<table width='800' height='100' align='center' style='text-align:center'");
sbResponseString.Append("</table></body></html>");
HttpContext.Current.Response.Write(sbResponseString.ToString());
HttpContext.Current.Response.End();

有什么建议吗?

最佳答案

您需要在 DataGrid 上设置 HeaderStyle 才能使用粗体。就这样。

dataExportExcel.HeaderStyle.Font.Bold=true;

关于c# - 如何在将数据集导出到 excel 时使标题字体加粗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11207297/

相关文章:

c# - 从另一方收到不安全或不正确的安全故障。(使用 SAML 时)

php - 使用 PHP 和 jQuery 将 HTML 导出到 EXCEL

date - ColdFusion 电子表格错误,日期或时间字符串无效

c# - ASP.NET Web 应用程序导出的 excel 文件上的警告弹出窗口

c# - 在数据库中记录 Hangfire RecurringJob 的执行情况?

c# - 难倒自托管 Web Api 服务

c# - 绑定(bind)到通用属性

c# - Graphics.DrawString 如何设置行距

c# - 为什么从 C# 4.0 开始使用的 COM 库需要大量使用动态类型?

asp.net - 如何在中继器中的 ItemTemplate 和 AlternatingItemTemplate 中具有相同的标记 - ASP.NET