将HTML表导出到Excel,在Excel工作表中创建文件但编码HTML

时间:2014-03-22 11:03:36

标签: excel internet-explorer-8 export

我正在将HTML表导出到Excel文件。我将其用于所有其他浏览器:

window.open('data:application/vnd.ms-excel,<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>' + encodeURIComponent(tblHtml));

但对于IE8,我已完成以下操作以进行此导出工作:

  1. jQuery post将表格html标记发送为字符串
  2. 执行帖子请求的Javascript:

    if (($.browser.msie && parseInt($.browser.version) <= 8)) {
        $.ajax({
            url: '/exportdatatoexcel',
            data: {
                'tblToExport': tblHtml//encodeURIComponent(tblHtml)
                , 'tblID': id
            },
            type: 'POST',
            async: false,
            success: function (html) {
                //nothing to do
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (jqXHR.readyState != 0) {
                    alert('error occurred');
                }
            }
        });
    
        window.open("/exportdatatoexcelfilesavedialog", 'PopUp', 'width=500,height=300');
    }
    
  3. 处理发布请求和window.open请求的操作(用于打开文件保存对话框):

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult ExportDataToExcel(FormCollection form)
    {
        string tblToExport = form["tblToExport"], tblID = form["tblID"];
    
        Session["ExportTbl"] = tblToExport;
    
        ContentResult content = new ContentResult();
        return content;
    }
    [ValidateInput(false)]
    public ActionResult ExportDataToExcelFileSaveDialog()
    {
        Response.ContentType = "application/vnd.ms-excel"; 
        Response.AddHeader("Content-Disposition", "attachment;filename=download.xls");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
    
        object tblToExport = Session["ExportTbl"];
        return PartialView("_ExportToExcelForIE8", tblToExport.ToString());
    
    }
    
  4. 现在,当文件保存对话框打开时,我可以保存excel文件,但excel文件内容是:

      

    %0D%0A%3cTABLE + DIR%3dltr + ID%3dtblCompAnalysis +类%3D%22clear +表+公司的分析 - 一般%22 + CELLSPACING%3D0 + CELLPADDING%3D0 +宽度%3D%22100%25% 22%3E%3cTBODY%3E%0D%0A%3cTR +类%3D%22labh +排序%22%3E%0D%0A%3cTD%3eCompany%3C%2fTD%3E%0D%0A%3cTD%3eMarket +扇区%图3c%2fTD%3E%0D%0A%3cTD%3eArgaam +部门+%3C%2fTD%3E%0D%0A%3cTD +类%3D城市%3eCity%3C%2fTD%3E%0D%0A%3cTD +类%3dtel% 3ePhone%3C%2fTD%3E%0D%0A%3cTD +类%3dtel%3eFax%3C%2fTD%3E%0D%0A%3cTD%3eEmail%3C%2fTD%3E%0D%0A%3cTD%3eWebsite%3C% 2fTD%3E%3C%2fTR%3E%0D 0A%%3cTR%3E%0D 0A%%3cTD%3E%3C

0 个答案:

没有答案