我有以下代码在.xls
文件中写入数据。
如何将这两个数据字段分开并存储在.xls
文件的不同列中?
Response.BinaryWrite((byte[])dr["data"]);
Response.BinaryWrite((byte[])dr["data2"]);
请帮忙!
答案 0 :(得分:0)
假设您DataTable
名为dt
,那么
StringBuilder sb = new StringBuilder();
sb.Append("<table><tr>");
foreach (DataColumn dc in dt.Columns)
{
sb.AppendFormat("<th>{0}</th>", dc.ColumnName);
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn dc in dt.Columns)
sb.AppendFormat("<td>{0}</td>", row[dc]);
sb.Append("</tr>");
}
sb.Append("</table>");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
Response.Charset = "";
Response.Write(sb.ToString())