我想创建一个带有粗体标题文本的csv文件。以下代码创建了一个标题行。但我希望这个标题以粗体显示。
'Add Response header
Response.Clear()
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.csv", "check_"))
Response.Charset = ""
Response.ContentType = "application/vnd.csv"
Try
Dim sb As New StringBuilder()
'Add Header dr.GetName(count)
For count As Integer = 0 To GridView1.Columns.Count - 1
If GridView1.Columns(count) IsNot Nothing Then
sb.Append(GridView1.Columns(count).HeaderText)
End If
If count < GridView1.Columns.count - 1 Then
sb.Append(",")
End If
Next
Response.Write(sb.ToString() + vbLf)
Response.Flush()
Catch ex As Exception
Response.Write(ex.Message)
End Try
Response.[End]()
答案 0 :(得分:4)
CSV是用逗号分隔的纯文本(,)没有附加样式。
答案 1 :(得分:1)
正如其他人已经提到的,CSV只是没有格式化的文本。
由于我假设您希望在Excel中使用粗体标题,并且您要将ASP.NET GridView
导出为CSV文件,因此还有其他两个选项:
GridView
HTML(HTML-Table)渲染到文件中,excel会正确解释第一种方法的样本:
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
如果您运行上述代码,则会产生HttpException
,如下所示:
“GridView”类型的控件“GridView1”必须放在表单中 用runat = server标记。“
要避免此错误,请添加以下代码:
public override void VerifyRenderingInServerForm(Control control)
{
// yes, it's correct that this is empty
}