Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在从JSON转换为CSV时,将列标题设置为粗体_Javascript_Jquery_Json_Csv - Fatal编程技术网

Javascript 在从JSON转换为CSV时,将列标题设置为粗体

Javascript 在从JSON转换为CSV时,将列标题设置为粗体,javascript,jquery,json,csv,Javascript,Jquery,Json,Csv,我正在尝试将JSON对象导出到CSV,为此我使用了下面fiddle中的代码 问题是,我希望在生成的CSV中使用粗体列标题(即,在上述代码中,标签行包含我希望使用螺栓字母的所有列标题)。有人能告诉我如何使用JavaScript吗? 您也可以在下面的链接中查看代码 Thanx提前,伙计们。我找到了一个方法!此方法非常有效。 创建tableExport.js (function($){ $.fn.extend({ tableExport: function(options)

我正在尝试将JSON对象导出到CSV,为此我使用了下面fiddle中的代码

问题是,我希望在生成的CSV中使用粗体列标题(即,在上述代码中,标签行包含我希望使用螺栓字母的所有列标题)。有人能告诉我如何使用JavaScript吗? 您也可以在下面的链接中查看代码


Thanx提前,伙计们。

我找到了一个方法!此方法非常有效。
创建tableExport.js

(function($){
    $.fn.extend({
        tableExport: function(options) {
            var defaults = {
                    separator: ',',
                    ignoreColumn: [],
                    tableName:'yourTableName',
                    type:'csv',
                    pdfFontSize:14,
                    pdfLeftMargin:20,
                    escape:'true',
                    htmlContent:'false',
                    consoleLog:'false'
            };
            var options = $.extend(defaults, options);
            var el = this;

            if(defaults.type == 'excel') {
                //console.log($(this).html());
                var excel = "<table>";
                // Header
                $(el).find('thead').find('tr').each(function () {
                    excel += "<tr>";
                    $(this).filter(':visible').find('th').each(function (index, data) {
                        if ($(this).css('display') != 'none') {
                            if (defaults.ignoreColumn.indexOf(index) == -1) {
                                excel += "<td><b>" + parseString($(this)) + "</b></td>";
                            }
                        }
                    });
                    excel += '</tr>';
                });
                // Row Vs Column
                var rowCount = 1;
                $(el).find('tbody').find('tr').each(function () {
                    excel += "<tr>";
                    var colCount = 0;
                    $(this).filter(':visible').find('td').each(function (index, data) {
                        if ($(this).css('display') != 'none') {
                            if (defaults.ignoreColumn.indexOf(index) == -1) {
                                excel += "<td>" + parseString($(this)) + "</td>";
                            }
                        }
                        colCount++;
                    });
                    rowCount++;
                    excel += '</tr>';
                });
                excel += '</table>'
                if (defaults.consoleLog == 'true') {
                    console.log(excel);
                }
                var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:" + defaults.type + "' xmlns='http://www.w3.org/TR/REC-html40'>";
                excelFile += "<head>";
                excelFile += "<!--[if gte mso 9]>";
                excelFile += "<xml>";
                excelFile += "<x:ExcelWorkbook>";
                excelFile += "<x:ExcelWorksheets>";
                excelFile += "<x:ExcelWorksheet>";
                excelFile += "<x:Name>";
                excelFile += "{worksheet}";
                excelFile += "</x:Name>";
                excelFile += "<x:WorksheetOptions>";
                excelFile += "<x:DisplayGridlines/>";
                excelFile += "</x:WorksheetOptions>";
                excelFile += "</x:ExcelWorksheet>";
                excelFile += "</x:ExcelWorksheets>";
                excelFile += "</x:ExcelWorkbook>";
                excelFile += "</xml>";
                excelFile += "<![endif]-->";
                excelFile += "</head>";
                excelFile += "<body>";
                excelFile += excel;
                excelFile += "</body>";
                excelFile += "</html>";
                var base64data = "base64," + $.base64.encode(excelFile);
                //window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); //this was original

                // Now the little tricky part.(By shreedhar)
                // you can use either>> window.open(uri);
                // but this will not work in some browsers
                // or you will not get the correct file extension
                var uri = 'data:application/vnd.ms-excel;' + base64data;
                var link = document.createElement("a");
                link.href = uri;
                link.style = "visibility:hidden";
                link.download =  "fileName";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }

            function parseString(data){
                if(defaults.htmlContent == 'true'){
                    content_data = data.html().trim();
                }else{
                    content_data = data.text().trim();
                }
                if(defaults.escape == 'true'){
                    content_data = escape(content_data);
                }
                return content_data;
            }
            //Check oriinal template for lot of convertion like to xml,json,pdf,etc
        }
    });
    })(jQuery);
(函数($){
$.fn.extend({
tableExport:功能(选项){
var默认值={
分隔符:',',
忽略列:[],
tableName:'yourTableName',
类型:'csv',
PDF大小:14,
pdfLeftMargin:20,
逃避:'真',
htmlContent:“假”,
控制台记录:“错误”
};
var options=$.extend(默认值,选项);
var el=这个;
如果(defaults.type==“excel”){
//log($(this.html());
var excel=“”;
//标题
$(el).find('thead').find('tr').each(函数(){
excel+=”;
$(this).filter(':visible').find('th').each(函数(索引、数据){
if($(this.css('display')!='none'){
if(默认值.ignoreColumn.indexOf(index)=-1){
excel++=“”+parseString($(this))+“”;
}
}
});
excel+='';
});
//行与列
var rowCount=1;
$(el).find('tbody').find('tr').each(函数(){
excel+=”;
var colCount=0;
$(this).filter(':visible').find('td').each(函数(索引,数据){
if($(this.css('display')!='none'){
if(默认值.ignoreColumn.indexOf(index)=-1){
excel++=“”+parseString($(this))+“”;
}
}
colCount++;
});
行计数++;
excel+='';
});
excel+=''
if(defaults.consoleLog==“true”){
console.log(excel);
}
var excelFile=“”;
excelFile+=“”;
excelFile+=“”;
excelFile+=“”;
excelFile+=“”;
excelFile+=excel;
excelFile+=“”;
excelFile+=“”;
var base64data=“base64,”+$.base64.encode(excelFile);
//window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;'+base64data);//这是原始的
//现在是棘手的部分
//您可以使用>>window.open(uri);
//但这在某些浏览器中不起作用
//否则您将无法获得正确的文件扩展名
var uri='data:application/vnd.ms excel;'+base64data;
var link=document.createElement(“a”);
link.href=uri;
link.style=“可见性:隐藏”;
link.download=“fileName”;
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
}
函数解析字符串(数据){
if(defaults.htmlContent==“true”){
content_data=data.html().trim();
}否则{
content_data=data.text().trim();
}
if(defaults.escape==“true”){
内容数据=转义(内容数据);
}
返回内容和数据;
}
//检查原始模板是否有很多转换,如xml、json、pdf等
}
});
})(jQuery);
您的HTML将

<table id="customers2" class="table datatable">
    <thead>
    <tr>
        <th>Brand</th>
        <th>Base Pack</th>
        <th>Basis</th>
        <th>Weightage</th>
        <th>Error Description</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Tiger Nixon</td>
        <td>System Architect</td>
        <td>Edinburgh</td>
        <td>61</td>
        <td>2011/04/25</td>
    </tr>
    </tbody>
</table>

<button onclick="$('#customers2').tableExport({type:'excel',escape:'false'});"></button>
<script type="text/javascript" src="PATH_TO_tableExport.js"></script>
<script type="text/javascript" src="PATH_TO_base64.js"></script>

烙印
基本包
基础
重量
错误描述
老虎尼克松
系统架构师
爱丁堡
61
2011/04/25
请检查调用parseString函数的行,这里b标记将使标题加粗

您还需要base64.js文件。相信我,这是标准方式。
base64.js

jQuery.base64 = ( function( $ ) {

var _PADCHAR = "=",
  _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  _VERSION = "1.0";


function _getbyte64( s, i ) {
  // This is oddly fast, except on Chrome/V8.
  // Minimal or no improvement in performance by using a
  // object with properties mapping chars to value (eg. 'A': 0)

  var idx = _ALPHA.indexOf( s.charAt( i ) );

  if ( idx === -1 ) {
    throw "Cannot decode base64";
  }

  return idx;
}


function _decode( s ) {
  var pads = 0,
    i,
    b10,
    imax = s.length,
    x = [];

  s = String( s );

  if ( imax === 0 ) {
    return s;
  }

  if ( imax % 4 !== 0 ) {
    throw "Cannot decode base64";
  }

  if ( s.charAt( imax - 1 ) === _PADCHAR ) {
    pads = 1;

    if ( s.charAt( imax - 2 ) === _PADCHAR ) {
      pads = 2;
    }

    // either way, we want to ignore this last block
    imax -= 4;
  }

  for ( i = 0; i < imax; i += 4 ) {
    b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
    x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) );
  }

  switch ( pads ) {
    case 1:
      b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
      x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) );
      break;

    case 2:
      b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
      x.push( String.fromCharCode( b10 >> 16 ) );
      break;
  }

  return x.join( "" );
}


function _getbyte( s, i ) {
  var x = s.charCodeAt( i );

  if ( x > 255 ) {
    throw "INVALID_CHARACTER_ERR: DOM Exception 5";
  }

  return x;
}


function _encode( s ) {
  if ( arguments.length !== 1 ) {
    throw "SyntaxError: exactly one argument required";
  }

  s = String( s );

  var i,
    b10,
    x = [],
    imax = s.length - s.length % 3;

  if ( s.length === 0 ) {
    return s;
  }

  for ( i = 0; i < imax; i += 3 ) {
    b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 );
    x.push( _ALPHA.charAt( b10 >> 18 ) );
    x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
    x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
    x.push( _ALPHA.charAt( b10 & 0x3f ) );
  }

  switch ( s.length - imax ) {
    case 1:
      b10 = _getbyte( s, i ) << 16;
      x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
      break;

    case 2:
      b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 );
      x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
      break;
  }

  return x.join( "" );
}


return {
  decode: _decode,
  encode: _encode,
  VERSION: _VERSION
};

  }( jQuery ) );
jQuery.base64=(函数($){
var_PADCHAR=“=”,
_ALPHA=“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyz012456789+/”,
_VERSION=“1.0”;
函数_getbyte64(s,i){
//这是奇怪的快,除了在Chrome/V8上。
//通过使用
//属性将字符映射到值的对象(例如“A”:0)
var idx=_ALPHA.indexOf(s.charAt(i));
如果(idx==-1){
抛出“无法解码base64”;
}
返回idx;
}
功能_解码{
var pads=0,
我
b10,
imax=s.长度,
x=[];
s=字符串(s);
如果(imax==0){
返回s;
}
如果(imax%4!==0){
抛出“无法解码base64”;
}
if(s.charAt(imax-1)==\u PADCHAR){
pads=1;
if(s.charAt(imax-2)==\u PADCHAR){
pads=2;
}
//无论哪种方式,我们都希望忽略最后一个块
imax-=4;
}
对于(i=0;i8)和0xff,b10和0xff));
}
开关(pads){
案例1:
b10=(_getbyte64(s,i)>8)和0xff));
打破
案例2:
b10=(_getbyte64(s,i)16));
打破
}
返回x.join(“”);
}
函数_getbyte(s,i){
var x=s.charCodeAt(i);
jQuery.base64 = ( function( $ ) {

var _PADCHAR = "=",
  _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  _VERSION = "1.0";


function _getbyte64( s, i ) {
  // This is oddly fast, except on Chrome/V8.
  // Minimal or no improvement in performance by using a
  // object with properties mapping chars to value (eg. 'A': 0)

  var idx = _ALPHA.indexOf( s.charAt( i ) );

  if ( idx === -1 ) {
    throw "Cannot decode base64";
  }

  return idx;
}


function _decode( s ) {
  var pads = 0,
    i,
    b10,
    imax = s.length,
    x = [];

  s = String( s );

  if ( imax === 0 ) {
    return s;
  }

  if ( imax % 4 !== 0 ) {
    throw "Cannot decode base64";
  }

  if ( s.charAt( imax - 1 ) === _PADCHAR ) {
    pads = 1;

    if ( s.charAt( imax - 2 ) === _PADCHAR ) {
      pads = 2;
    }

    // either way, we want to ignore this last block
    imax -= 4;
  }

  for ( i = 0; i < imax; i += 4 ) {
    b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
    x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) );
  }

  switch ( pads ) {
    case 1:
      b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
      x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) );
      break;

    case 2:
      b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
      x.push( String.fromCharCode( b10 >> 16 ) );
      break;
  }

  return x.join( "" );
}


function _getbyte( s, i ) {
  var x = s.charCodeAt( i );

  if ( x > 255 ) {
    throw "INVALID_CHARACTER_ERR: DOM Exception 5";
  }

  return x;
}


function _encode( s ) {
  if ( arguments.length !== 1 ) {
    throw "SyntaxError: exactly one argument required";
  }

  s = String( s );

  var i,
    b10,
    x = [],
    imax = s.length - s.length % 3;

  if ( s.length === 0 ) {
    return s;
  }

  for ( i = 0; i < imax; i += 3 ) {
    b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 );
    x.push( _ALPHA.charAt( b10 >> 18 ) );
    x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
    x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
    x.push( _ALPHA.charAt( b10 & 0x3f ) );
  }

  switch ( s.length - imax ) {
    case 1:
      b10 = _getbyte( s, i ) << 16;
      x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
      break;

    case 2:
      b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 );
      x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
      break;
  }

  return x.join( "" );
}


return {
  decode: _decode,
  encode: _encode,
  VERSION: _VERSION
};

  }( jQuery ) );