简体   繁体   English

删除用于Excel导出的WYSIWYG HTML和CSS标记

[英]Remove WYSIWYG HTML and CSS tags for Excel export

I have a form which has a WYSIWYG using http://summernote.org/#/ . 我有一个使用http://summernote.org/#/的所见即所得的表格。 I used it for comments field in my form. 我将其用于表单中的注释字段。 I am using PostgreSQL 9.3 for database and saving the data from the comments field in a text datatype. 我将PostgreSQL 9.3用于数据库并将注释字段中的数据保存为text数据类型。

So as you expected when you save that to database, if the user has made changes of the format of the text like Font Style, Size, Line Breaks etc. the WYSIWYG will also add HTML and CSS codes on it and will also be saved together in the database. 因此,正如您期望的那样,将其保存到数据库时,如果用户更改了字体,字号,换行符等文本格式,则WYSIWYG还将在其上添加HTML和CSS代码,并将它们一起保存在数据库中。 Then now I have this problem that when I render those comments in a Datatable the comments will look fine because the web interprets the HTML and CSS codes in it. 现在,我遇到了一个问题,当我在数据表中呈现这些注释时,这些注释看起来会很好,因为Web会解释其中的HTML和CSS代码。 But when I export the content to Excel, then my data is in chaos. 但是,当我将内容导出到Excel时,我的数据混乱了。

Excel won't interpret HTML and CSS tags and line breaks making my excel output broken. Excel不会解释HTML和CSS标记以​​及换行符,使我的excel输出损坏。 Lots of line breaks and everything. 很多换行符和所有内容。

Is there a way to strip those HTML and CSS? 有没有办法剥离那些HTML和CSS? I want to do it when I am rendering it in a Datatables in jQuery so that when the Datatables load, the HTML and CSS is already stripped so I can just export it easily or even copy paste. 我想在jQuery的数据表中呈现它时执行此操作,以便在加载数据表时,已经剥离了HTML和CSS,因此我可以轻松地导出它甚至复制粘贴。

$.ajax({
    url: "api/crm/detailedsummary", 
    type: 'GET',
    data: {"from" : $("#fromDateAll").val(), "to" :  $("#toDateAll").val()},
    success: function(result){
    var myObj = $.parseJSON(result);
        $.each(myObj, function(key,value) {
            table.row.add( [
                value.calldatetime, 
                value.phone_num,    
                value.agent,    
                value.gross_disposition,    
                value.net_disposition,  
                value.verified_status,  
                value.passwithchanges_status,   
                value.reject_status,        
                value.verified_by,  
                value.verified_date,    
                value.comments
            ] ).draw();
        });
    }});

The value.comments is the one with HTML and CSS codes in it. value.comments是其中包含HTML和CSS代码的代码。

.replace may work for this situation: .replace 可能适用于这种情况:

var comments = '<div id="someId">Hello <span>world!</span></div>';
console.log(comments.replace(/(<([^>]+)>)/ig,""));

The regexp checks the following: regexp检查以下内容:

  • < : left tag < 左标签
  • [^>] : anything except a right tag [^>] 除正确标签外的所有内容
  • + : one or more times + 一次或多次
  • > : right tag > 正确的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM