Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. How can I change this option in javascript to be exported right to left.

This is my JS function:

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');
    }

}

like image 240
Rastagar Avatar asked Oct 29 '22 21:10

Rastagar


1 Answers

according to SpreadsheetML in order to specify RTL for a sheet you should write this inside your WorkSheet:

<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

like image 166
MatanCo Avatar answered Nov 12 '22 14:11

MatanCo