嗨,这是我的代码,它运行正常。
我有一个数据表,我正在将其转换为.xls文件。
我用过这个Response.Write(tab + "<b>" + dc.ColumnName + "</b>");
我想把它变成BOLD
string attachment = "attachment; filename=" + filename;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dataTable.Columns)
{
Response.Write(tab + "<b>" + dc.ColumnName + "</b>");
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dataTable.Rows)
{
tab = "";
for (i = 0; i < dataTable.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
Response.Flush();
但这没有用,请帮助。
答案 0 :(得分:0)
尝试使用<span style=...>
代替<b>
:
Response.Write(tab + "<span style='font-weight:bold'>" + dc.ColumnName + "</span>");
Haven自己测试过这个;如果它仍然无效,请将其全部更改为使用<table>
,<tr>
和<th>
/ <td>
html标记 - 我从经验中知道这可以导出a&#34;伪Excel&#34;文件。然后,您可以将样式放在<th>
/ <td>
标记上。
答案 1 :(得分:0)
请你试试这个:
//Write in new column
Response.Write("<Td>");
//Get column headers and make it as bold in excel columns
Response.Write("<B>");
Response.Write(column);
Response.Write("</B>");
Response.Write("</Td>");
<强>更新强>
这里的代码和示例数据表包含一些虚拟数据,只是经过测试并且工作正常:
//export to excel datatable
var dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Country");
dataTable.Columns.Add("City");
dataTable.Rows.Add("1", "Micheal", "USA", "Washington");
dataTable.Rows.Add("2", "Smith", "UK", "London");
dataTable.Rows.Add("3", "Martin", "AUS", "Sydney");
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
Response.AddHeader("content-disposition", "attachment;filename=testExport.xls");
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "";
//Set Fonts
Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
Response.Write("<BR><BR><BR>");
//Sets the table border, cell spacing, border color, font of the text, background,
//foreground, font height
Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
// Check not to increase number of records more than 65k according to excel,03
if (dataTable.Rows.Count <= 65536)
{
// Get DataTable Column's Header
foreach (DataColumn column in dataTable.Columns)
{
//Write in new column
Response.Write("<Td>");
//Get column headers and make it as bold in excel columns
Response.Write("<B>");
Response.Write(column);
Response.Write("</B>");
Response.Write("</Td>");
}
Response.Write("</TR>");
// Get DataTable Column's Row
foreach (DataRow dtRow in dataTable.Rows)
{
//Write in new row
Response.Write("<TR>");
for (int i = 0; i <= dataTable.Columns.Count - 1; i++)
{
Response.Write("<Td>");
Response.Write(dtRow[i].ToString());
Response.Write("</Td>");
}
Response.Write("</TR>");
}
}
Response.Write("</Table>");
Response.Write("</font>");
Response.Flush();
Response.End();