简体   繁体   English

如何在Javascript中创建具有从右到左方向的Excel工作表

[英]How to create excel sheet with right-to-left direction in Javascript

I'm trying to export html table to excel in javascript, but when i click on export button, it exports successfully and direction of table is changed from right to left to left to right . 我正试图在javascript中将html表导出为ex​​cel,但是当我点击导出按钮时,它成功导出并且表的方向从右 向左变为从左到右 How can I change this option in javascript to be exported right to left . 如何在javascript中更改此选项以从右向左导出

This is my JS function: 这是我的JS功能:

function tablesToExcel(id) {

    var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel" lang="fa">';
    tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

    tab_text = tab_text + '<x:Name>Report Sheet</x:Name>';
    //tab_text = tab_text + '<x:Direction><x:right-to-left></x:right-to-left></x:Direction>';

    tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
    tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

    tab_text = tab_text + "<table border='1px' dir='rtl'>";
    tab_text = tab_text + $('#'+id).html();
    tab_text = tab_text + '</table></body></html>';

    var data_type = 'data:application/vnd.ms-excel';

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        if (window.navigator.msSaveBlob) {
            var blob = new Blob([tab_text], {
                type: "application/csv;charset=utf-8;"
            });
            navigator.msSaveBlob(blob, 'Report file.xls');
        }
    }
    else {
        $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
        $('#test').attr('download', 'Report file.xls');
    }

} }

according to SpreadsheetML in order to specify RTL for a sheet you should write this inside your WorkSheet: 根据SpreadsheetML为了指定工作表的RTL,你应该在工作表中写这个:

<x:worksheet><x:sheetViews><x:sheetView rightToLeft=1></x:sheetView></x:sheetViews></x:worksheet>

Haven't tried it myself though, here is a reference to "worksheet" object, you can see for yourself https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.worksheet.aspx 虽然我没有尝试过,这里是对“工作表”对象的引用,你可以自己看看https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.worksheet.aspx

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

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