如何将数据字段拆分为.xls文件中的两个单独的列

时间:2013-03-16 05:34:58

标签: c# asp.net binary xls

我有以下代码在.xls文件中写入数据。 如何将这两个数据字段分开并存储在.xls文件的不同列中?

Response.BinaryWrite((byte[])dr["data"]);
Response.BinaryWrite((byte[])dr["data2"]);

请帮忙!

1 个答案:

答案 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())