在嘗試將html字符串轉換爲csv格式時出現問題,以便我可以創建新的csv blob對象並從IE & Edge中的網頁中打開該對象。使用javascript將html字符串轉換爲csv格式
我發現了一個函數來將表格對象轉換爲csv格式,但不幸的是我需要傳遞這個字符串,而不是對象。
下面是表對象CSV方法:
function (table) {
var slice = Array.prototype.slice;
return slice.call(table.rows).map(function (row) {
return slice.call(row.cells).map(function (cell) {
return '"t"'.replace("t", cell.textContent);
}).join(",");
}).join("\r\n");
}
的問題是需要在顯示之前格式化我希望在CSV文件中顯示。因此,如果我只是將該表格對象顯示在頁面上,那麼csv將具有比所需更多的數據。 當單擊導出按鈕時,我有一個方法將表格格式化,並返回表格的html字符串以顯示爲csv。 但我不知道如何將該字符串轉換爲csv格式,無法在任何地方找到此示例。
>修改1
<table id='excel_tbl'><tr><td>Name</td><td>Time allowed</td><td>Best score</td><td>Attempts</td></tr><tr><td>
<a class="ng-binding" id="assess_inst_fdd4ddec-9985-4def-9148-b5cd56ee77e6_875650db-4934-40dc-9e30-81c57c3472de" href="#" ng-click="itShowStudent(oPerson.PersonKey, oPerson.RefName)">Michelle27, Michelle27</a>
</td><td>
<div class="ng-hide" aria-hidden="true" ng-show="oPerson.MaxMins >= 0.0">
<it-time class="it-time ng-isolate-scope" seconds="-60" precise="true"><div class="time-info-text"><!-- ngIf: seconds < 60 && bPrecise --><div class="ng-scope" ng-if="seconds < 60 && bPrecise"><span class="large ng-binding">0</span><!-- ngIf: itGetSeconds(seconds) == 1 --><!-- ngIf: itGetSeconds(seconds) != 1 --><span class="small ng-binding ng-scope" ng-if="itGetSeconds(seconds) != 1">secs</span><!-- end ngIf: itGetSeconds(seconds) != 1 --></div><!-- end ngIf: seconds < 60 && bPrecise --><!-- ngIf: seconds == 0 && !bPrecise && bUseNone --><!-- ngIf: seconds == 0 && !bPrecise && !bUseNone --><!-- ngIf: seconds > 0 && seconds < 60 && !bPrecise --><!-- ngIf: seconds >= 60 --></div></it-time>
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.BestScore >= 0.0">
33%
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.Attempts > 0">
1
</div>
</td></tr><tr><td>
<a class="ng-binding" id="assess_inst_f588c82b-7eef-422c-8125-e8d8118ed549_875650db-4934-40dc-9e30-81c57c3472de" href="#" ng-click="itShowStudent(oPerson.PersonKey, oPerson.RefName)">[email protected], [email protected]</a>
</td><td>
<div class="ng-hide" aria-hidden="true" ng-show="oPerson.MaxMins >= 0.0">
<it-time class="it-time ng-isolate-scope" seconds="-60" precise="true"><div class="time-info-text"><!-- ngIf: seconds < 60 && bPrecise --><div class="ng-scope" ng-if="seconds < 60 && bPrecise"><span class="large ng-binding">0</span><!-- ngIf: itGetSeconds(seconds) == 1 --><!-- ngIf: itGetSeconds(seconds) != 1 --><span class="small ng-binding ng-scope" ng-if="itGetSeconds(seconds) != 1">secs</span><!-- end ngIf: itGetSeconds(seconds) != 1 --></div><!-- end ngIf: seconds < 60 && bPrecise --><!-- ngIf: seconds == 0 && !bPrecise && bUseNone --><!-- ngIf: seconds == 0 && !bPrecise && !bUseNone --><!-- ngIf: seconds > 0 && seconds < 60 && !bPrecise --><!-- ngIf: seconds >= 60 --></div></it-time>
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.BestScore >= 0.0">
100%
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.Attempts > 0">
18
</div>
</td></tr><tr><td>
<a class="ng-binding" id="assess_inst_a820d6e4-1e68-4265-9580-f053cf09cb11_875650db-4934-40dc-9e30-81c57c3472de" href="#" ng-click="itShowStudent(oPerson.PersonKey, oPerson.RefName)">Michelle35, Michelle35</a>
</td><td>
<div class="ng-hide" aria-hidden="true" ng-show="oPerson.MaxMins >= 0.0">
<it-time class="it-time ng-isolate-scope" seconds="-60" precise="true"><div class="time-info-text"><!-- ngIf: seconds < 60 && bPrecise --><div class="ng-scope" ng-if="seconds < 60 && bPrecise"><span class="large ng-binding">0</span><!-- ngIf: itGetSeconds(seconds) == 1 --><!-- ngIf: itGetSeconds(seconds) != 1 --><span class="small ng-binding ng-scope" ng-if="itGetSeconds(seconds) != 1">secs</span><!-- end ngIf: itGetSeconds(seconds) != 1 --></div><!-- end ngIf: seconds < 60 && bPrecise --><!-- ngIf: seconds == 0 && !bPrecise && bUseNone --><!-- ngIf: seconds == 0 && !bPrecise && !bUseNone --><!-- ngIf: seconds > 0 && seconds < 60 && !bPrecise --><!-- ngIf: seconds >= 60 --></div></it-time>
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.BestScore >= 0.0">
100%
</div>
</td><td>
<div class="ng-binding" aria-hidden="false" ng-show="oPerson.Attempts > 0">
4
</div>
</td></tr></table>
上述字符串使用鉻時傳遞給
document.open("txt/html", "replace");
iframe.document.write(sHTML);
了在鉻/ Firefox中呈現爲csv
HTML字符串/ Firefox和呈現如下:
我不能讓document.open與Edge一起工作,所以我使用Blob方法。
var csv = $scope.tbl2csv(oTable[0]);
// Create a CSV Blob
var blob = new Blob([ csv ], { type: "text/csv"});
if (navigator.msSaveOrOpenBlob) {
// Works for Internet Explorer and Microsoft Edge
navigator.msSaveOrOpenBlob(blob, "Assessment.csv");
}
這適用於邊緣,如所需要的表中的,以CSV開口的出口在Excel中,問題是,表對象轉換成CSV格式不一樣的HTML字符串由鉻呈現/ Firefox瀏覽器。 所以我的要求是找到一種方法將該字符串轉換爲csv格式。
對待HTML的唯一正確方法是將其發送到HTML解析器。你知道該怎麼做。 –
不,這不是一個CSV生成器,即使它大部分時間生成有效的CSV。 –
@JanDvorak爲迴應。不幸的是,我不知道該怎麼做讓我的html字符串(這是我想要顯示爲csv的表格)變成csv格式。任何建議不勝感激。 –