使用自定义标题下载Excel文件。

时间:2019-12-02 10:28:11

标签: javascript angularjs xls

I,m为客户端用户导出(下载)一个excel文件,即用户根据excel的相应标头输入数据,然后当用户将excel上载到系统时,系统接受用户数据...标头已成功下载,但之间以逗号分隔。我需要每个标头都位于enter image description here

下面的唯一单个列中

但是我需要这样enter image description here 这是我的代码...

$scope.DownLoadTemplate = function () {

    for (a in $scope.SalaryHeadList)
    {
        $scope.HeadTitle = $scope.SalaryHeadList[a].Title;
        $scope.HeaderList.push(angular.copy($scope.HeadTitle));
    }
    $scope.CurrencyTitle = $scope.CurrencyList[0].Title;
    $scope.HeaderList.push(angular.copy($scope.CurrencyTitle));

    var url = window.URL.createObjectURL(new Blob([$scope.HeaderList]));
    var link = document.createElement('a');
    var filename = "Employee.xls";
    link.href = url;
    link.setAttribute('download', filename);
    document.body.appendChild(link);
    link.click();
}

请帮助!

1 个答案:

答案 0 :(得分:0)

$scope.exportData = function () {
    debugger;
    var uri = 'data:application/vnd.ms-excel;base64,'
        , template = `
             <html xmlns:o="urn:schemas-microsoft-com:office:office" 
                   xmlns:x="urn:schemas-microsoft-com:office:excel"
                   xmlns="http://www.w3.org/TR/REC-html40">
               <head>
                 <!--[if gte mso 9]>
                    <xml>
                      <x:ExcelWorkbook>
                        <x:ExcelWorksheets>
                          <x:ExcelWorksheet>
                            <x:Name>{CouponDetails}</x:Name>
                            <x:WorksheetOptions>
                              <x:DisplayGridlines/>
                            </x:WorksheetOptions>
                          </x:ExcelWorksheet>
                        </x:ExcelWorksheets>
                      </x:ExcelWorkbook>
                    </xml>
                  <![endif]-->
                </head>
                <body><table>{table}</table></body>
              </html>
            `
        , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

    var table = document.getElementById("example1");
    var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
    var url = uri + base64(format(template, ctx));
    var a = document.createElement('a');
    a.href = url;
    a.download = 'Expoerdata.xls';
    a.click();
};

HTML表格:

<table id="example1" class="table table-bordered invoice-custom example1" 
       style="display:none;">
    <thead>
        <tr>
            <th data-field="id">Sno</th>
            <th data-field="name" data-editable="true">Name</th>

            <th data-field="phone" data-editable="true">Value</th>

            <th data-field="action">Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="Test in allItems | orderBy:TestParams_Id:reverse">
            <td style="text-align:center">
                {{Test.sno}}
            </td>
            <td>{{Test.Name}}</td>
            <td>{{Test.ExpectedValue}}</td>
        </tr>
    </tbody>
</table>