asp.net 导出 Excel

/// <summary>
    /// List 数据导出Excel
    /// </summary>
    /// <param name="list">数据集合</param>
    /// <param name="filename">文件名 不用加 .xls</param>
    /// <param name="ColumNames">标题名(数组-所有标题)</param>
    public void HtmlToExcel(数据 list, string filename, string[] ColumNames)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.xls";
        //拼接数据结构
        string xls = @"<Table border='1'><TR>";
        int i;
        int j;
        //标题
        for (i = 0; i < ColumNames.Length; i++)
        {
            xls += @"<TD>" + ColumNames[i] + @"</TD>";
        }
        xls = xls + @"</TR>";
        //内容
        for (j = 0; j < list.Count(); j++)
        {
            xls = xls + @"<TR>";
            xls = xls + @"<TD>" + list[j].字段一对应的字段 + "&nbsp;" + @"</TD>";
            xls = xls + @"<TD>" + list[j].字段二对应的字段 + "&nbsp;" + @"</TD>";
            xls = xls + @"<TD>" + list[j].字段三对应的字段 + "&nbsp;" + @"</TD>";
            xls = xls + @"<TD>" + list[j].字段四对应的字段 + "&nbsp;" + @"</TD>";
            xls = xls + @"<TD>" + list[j].字段五对应的字段+ "&nbsp;" + @"</TD>";
            xls = xls + @"<TD>" + list[j].字段六对应的字段 + "&nbsp;" + @"</TD>";
         
          
        xls = xls + @"</Table>";
        Response.Write(xls);    //stringWrite.ToString());
        Response.End();
    }
//调用

string[] ColumNames = new string[] { "字段一", "字段二", "字段三", "字段四", "字段五", "字段六" };
        HtmlToExcel(数据, "文件名称", ShowNames);

 

posted @ 2018-05-28 18:15  打不死的小强5  阅读(1126)  评论(0编辑  收藏  举报