永恒烛光

生如夏花之灿烂

导航

javascript 实现 html 页面 table 导出 excel

在做数据导出excel的时候我们有时候需要将Table 的样式也导出在excel中。
功能说明:用js将页面上的talbe 全部 以字符的形式传到后台代码中,后台输出。
此处使用的是asp.net  mvc 模式
html页面:
<script language="javascript" type="text/javascript">
        $(function () {
            $("#btnExcel").click(function () {
                ExportExcelByID('cDiv','报表名');
            });
        })

</script>
<input type="button" name="btnout" id="btnExcel" value="导出" />
<div id="cDiv">
    <table id="tableview" >内容</table>
</div>
js函数:
function ExportExcelByID(ReportTableID,reportname) {
    // alert('xxxx');
    if (!$("#cDiv")) {
        alert("请先确定导出内容!");
        return false;
    }


    var exportvalue = $("#cDiv").html();

    exportvalue = exportvalue.replace(/\ /g, "");
    exportvalue = escape(exportvalue);
    
    var ExportForm = document.createElement("FORM");
    document.body.appendChild(ExportForm);
    ExportForm.method = "POST";
    
    //var newElement = document.createElement("<input name='exportvalue' type='hidden' id='exportvalue'>");
   //上面的写法firefox不兼容,下面的写法可以兼容firefox
    var newElement = document.createElement("input");
    newElement.setAttribute("name", "exportvalue");
    newElement.setAttribute("type", "hidden");

    ExportForm.appendChild(newElement);
    newElement.value = exportvalue;
    ExportForm.action = "/Report/ToExcel/"+reportname;//提交到后台的一个页面ToExcel.aspx页面     ExportForm.target = "_blank";
    ExportForm.submit();
} 
ToExcel 后台代码:
public ActionResult ToExcel(string id)
        {
            
            string exportvalue =Request["exportvalue"];

            //xyq 2007.12.4 负数导出到excel后不能运算
            //        exportvalue = exportvalue.Replace("-", "—");

            //去掉一些不要显示的字符串
            //exportvalue = Remove(exportvalue, "DISPLAY: none");

            /*
            exportvalue = Remove(exportvalue, "<SPAN style=\"DISPLAY: none", "</SPAN>");
            exportvalue = Remove(exportvalue, "<TD style=\"DISPLAY: none", "</TD>");
            exportvalue = Remove(exportvalue, "<TH style=\"DISPLAY: none", "</TH>");
            */

            exportvalue = Microsoft.JScript.GlobalObject.unescape(Server.HtmlDecode(exportvalue));
            exportvalue = exportvalue.Replace("?", "");
            exportvalue = exportvalue.Replace(" ", "");
            exportvalue = exportvalue.Replace("<TABLE", "<TABLE border=\"1\" bordercolor=\"#000000\"");
            exportvalue = exportvalue.Replace("<table", "<table border=\"1\" ");
            exportvalue = exportvalue.Replace("border=0", "border=1 ");
            exportvalue = exportvalue.Replace("border='0'", "border=1 ");
            exportvalue = exportvalue.Replace("border=\"0\"", "border=1 ");
            exportvalue = exportvalue.Replace("STYLE", "stylefrom ");
            exportvalue = exportvalue.Replace("<TD><SPAN></SPAN></TD>", " ");
            exportvalue = exportvalue.Replace("<td>", " <td style=\"vnd.ms-excel.numberformat:@\">");
            exportvalue = exportvalue.Replace("<TD>", " <td style=\"vnd.ms-excel.numberformat:@\">");
            exportvalue = exportvalue.Replace("<A ", "<SPAN ");
            exportvalue = exportvalue.Replace("</A>", "</SPAN>");
            exportvalue = exportvalue.Replace("<IMG ", "<IMG1 ");

            exportvalue = exportvalue.Replace("type=image ", "type=image1 ");


            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            //this.EnableViewState = false;
            //xyq 2007.12.4
            Response.ContentEncoding = System.Text.Encoding.UTF8;

            Response.AddHeader("Content-Disposition", "attachment;filename="+id+".xls");

            StringWriter sw = new StringWriter();
            //sw.WriteLine("javascript:unescape('" +exportvalue+"');");
            sw.WriteLine(exportvalue);
            HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
            Control cr = new Control();
            cr.RenderControl(htmlWriter);

            //强制输出bom 这样避免excel打开时乱码
            Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });

            Response.Write(sw.ToString());
            Response.End();
            return View();
        }
       

posted on 2011-04-08 17:16  夏花,秋叶  阅读(3866)  评论(0编辑  收藏  举报