首页 > 代码库 > 导出Excel

导出Excel

  把excel另存为xml电子表格(*.xml),格式复制生成的xml代码,放到view中,然后根据model去生成excel的单元格和列。

  下面是生成excel和下载的代码。

        Response.Clear();
        Response.ClearContent();
        Response.Buffer = true;
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/ms-excel";
        string downLoadFileName = title;
        if (Request.UserAgent.ToLower().IndexOf("msie") > -1)
        {
            downLoadFileName = HttpUtility.UrlPathEncode(downLoadFileName);
        }
        else if (Request.UserAgent.ToLower().IndexOf("firefox") > -1)
        {
            Response.AddHeader("Content-Disposition", "attachment:filename=\"" + downLoadFileName + "\"");
        }
        else
            Response.AddHeader("Content-Disposition", "attachment:filename=" + downLoadFileName + "");