//导出excel修改
    public void Export(GridView GV, string FileType, string ReportTitle, string ReportMemo, string FileName, string Condition, string herMesg, Page page)
    {
        //格式
        StringBuilder strContent = new StringBuilder();
        strContent.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office'");
        strContent.Append("xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>");
        strContent.Append("<head><meta http-equiv=Content-Type content='text/html; charset=gb2312'>");
        strContent.Append("<meta name=ProgId content=Word.Document><meta name=Generator content='Microsoft Word 11'>");
        strContent.Append("<meta name=Originator content='Microsoft Word 11'>");
        strContent.Append("<!--[if gte mso 9]><xml><w:WordDocument><w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>");
        strContent.Append("<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>");
        strContent.Append("<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>");
        strContent.Append("<w:View>Print</w:View>");
        strContent.Append("</w:WordDocument></xml><![endif]-->");

        strContent.Append("<html><body>");
        strContent.Append("<p align='center' width='100%' border='0' bordercolor='#000000' bordercolorlight='#000000' ");
        strContent.Append("cellspacing='0' cellpadding='0' style='font-size:14px;font-family:宋体; border-collapse:collapse'>");
        //strContent.Append("<div align=center colspan=5 border='0' width='100%' style='font-size:18px;font-family:宋体;'>" + ReportTitle + "</div>");
        //strContent.Append("<div align=center colspan=5 border='0' width='100%' style='font-size:14px;font-family:宋体;'>" + "(统计条件:" + Condition + ")" + "</div>");
        //strContent.Append("</p>");
        //if (herMesg.Length > 0)
        //{
        //    strContent.Append("<div align=center colspan=5 border='0' width='100%' style='font-size:14px;font-family:宋体;'>" + herMesg + "</div>");
        //    strContent.Append("</p>");
        //}
        strContent.Append("<table align='center' width='100%' border='1' bordercolor='#000000' bordercolorlight='#000000' ");
        strContent.Append("cellspacing='0' cellpadding='0' style='font-size:12px;font-family:宋体; border-collapse:collapse'>");

        //列标题
        strContent.Append("<tr>");
        for (int i = 0; i < GV.HeaderRow.Cells.Count; i++)
        {
            strContent.Append(string.Format("<td align=center style=height:30px>{0}</td>", GV.HeaderRow.Cells[i].Text));
        }
        strContent.Append("</tr>");

        //每行内容
        foreach (GridViewRow gvr in GV.Rows)
        {
            strContent.Append("<tr>");
            for (int i = 0; i < gvr.Cells.Count; i++)
            {
                strContent.Append(string.Format("<td align=center style=height:30px>{0}</td>", gvr.Cells[i].Text.Replace(" ", "")));
            }
            strContent.Append("</tr>");
        }

        ////页脚
        //strContent.Append("<tr>");
        //for (int i = 0; i < GV.Columns.Count; i++)
        //{
        //    strContent.Append(string.Format("<td align=center style=height:30px>{0}</td>", GV.Columns[i].FooterText.Replace(" ", "")));
        //}
        //strContent.Append("</tr>");
        strContent.Append("</table>");
        //if (ReportMemo.Length > 0)
        //报表备注(说明)
        //    strContent.Append(string.Format("<p></p><div colspan={0} border='0' width='100%' style='font-size:14px;font-family:宋体;'>" + "备注:{1} " + "</div>", GV.Columns.Count, ReportMemo));
        strContent.Append("</body></html>");

        page.Response.Charset = "GB2312";
        page.Response.ContentEncoding = System.Text.Encoding.UTF7;
        page.Response.AppendHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        page.Response.ContentType = FileType;
        page.EnableViewState = false;
        page.Response.Write(strContent);
        page.Response.End();
    }








本文转载:CSDN博客