关于bootstrap-table导出问题,table-export使用

1、导出文件为.xls文件,使用office2007及以上版本打开出现格式不兼容

解决方法:

  $('#table_id').tableExport({
            type: 'xlsx',
            exportDataType: "all",
            //表格工作区名称
            worksheetName: 'sheet1',
            ignoreColumn: [2],//忽略某一列的索引
            fileName: '下载文件名称' + Date.now(),//下载文件名称
            onCellHtmlData: function (cell, row, col, data) {
                return data;
            },
        });

2、设置为.xlsx格式之后,文件里的数据出现两位有效数字,统计人数出现小数,虽然为.00后缀,但看着也难受

查了好长时间,只查到一个直接改源码的解决方案,但是我下个文件导出万一想要保留两位小数该怎么做,还是去改源码吗?不行,于是自己去看了一下源码

const defaults 默认配置的mso,转化.xlsx格式的配置
mso: {                            // word或者excel
    fileFormat: 'xlshtml',        // 'xlshtml' = Excel 2000 html format
                                  // 'xmlss' = XML Spreadsheet 2003 file format (XMLSS)
                                  // 'xlsx' = Excel 2007 Office Open XML format
    onMsoNumberFormat: null,        // Excel 2000 html format only. See readme.md for more information about msonumberformat
    pageFormat: 'a4',               // 页面大小
    pageOrientation: 'portrait',    // portrait, landscape (xlshtml format only)
    rtl: false,                     // true = Set worksheet option 'DisplayRightToLeft'
    styles: [],                     // E.g. ['border-bottom', 'border-top', 'border-left', 'border-right']
    worksheetName: '',
    xlsx: {                         //xlsx设置
        formatId: {                   // XLSX format (id) used to format excel cells. See readme.md: data-tableexport-xlsxformatid
        date: 14,                   // The default format id, or a format string (e.g. 'm/d/yy'), or a function(cell, row, col)
        numbers: 2,                 // The default Format id, or a format string (e.g. '\"T\"\ #0.00'), or a function(cell, row, col)
        currency: 164               // This id is used by "data-tableexport-xlsxformatid" to allow you to export a cell in currency format (see below)
          },
        format: {
            currency: '$#,##0.00;[Red]-$#,##0.00' // The format string to be used for the export for the currency format 
                                                  // Euro format: '#,##0.00 €;[Red](#,##0.00) €'
          },
          onHyperlink: null             // function($cell, row, col, href, content, hyperlink): Return what to export for hyperlinks
   }
}
    
      

解决方案:

   $('#table_id').tableExport({
            type: 'excel',
            mso:{//格式转换,可以转换为excel与word
                fileFormat:'xlsx', //转换为excel的.xlsx格式文档,适用于Excel2007及以上版本
                xlsx:{//配置.xlsx文件的单元格内容格式
                    formatId:{
                        numbers:0 //保留小数点后的位数
                    }
                }
            } ,
            exportDataType: "all",
            //表格工作区名称
            worksheetName: 'sheet1',
            ignoreColumn: [2],//忽略某一列的索引
            fileName: '文件名称' + Date.now(),//下载文件名称
            onCellHtmlData: function (cell, row, col, data) {
                console.info(data);
                return data;
            },
        });

ps:还是早上思路清晰!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Bootstrap Table导出Excel,你可以使用一些插件或库来实现这个功能。以下是使用两个常用的库来导出Excel的方法: 1. 使用TableExport插件: - 首先,在你的HTML页面中引入TableExport插件和相应的依赖文件。 ```html <script src="https://cdn.jsdelivr.net/npm/tableexport@5.0.0/dist/js/tableexport.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script> ``` - 然后,在你的Bootstrap Table上添加一个导出按钮,并给它一个唯一的ID。 ```html <button id="export-btn" class="btn btn-primary">导出Excel</button> ``` - 最后,在JavaScript代码中使用TableExport来初始化并绑定导出按钮的点击事件。 ```javascript $(document).ready(function() { $('#export-btn').click(function() { $('#your-table-id').tableExport({ type: 'excel', escape: 'false' }); }); }); ``` 这里的`#your-table-id`是你的Bootstrap Table的ID,你需要将其替换为你实际使用的ID。 2. 使用SheetJS库: - 首先,在你的HTML页面中引入SheetJS库。 ```html <script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script> ``` - 然后,在JavaScript代码中创建一个导出函数,并绑定到一个按钮的点击事件上。 ```javascript function exportToExcel() { var wb = XLSX.utils.table_to_book(document.getElementById('your-table-id')); XLSX.writeFile(wb, 'exported_data.xlsx'); } $(document).ready(function() { $('#export-btn').click(function() { exportToExcel(); }); }); ``` 这里的`#your-table-id`是你的Bootstrap Table的ID,你需要将其替换为你实际使用的ID。 通过以上两种方法之一,你可以将Bootstrap Table导出为Excel文件。记得将`#your-table-id`替换为你实际使用的ID,并根据需要修改导出文件的名称和格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值