js怎麼導入csv,js怎麼導入Excel表

本文目錄一覽:

JS 下載/導出 csv、excel、txt 、img等文件的方法總結

1. 調用後端介面導出文件

示例下載介面url

1.1 window.open(url)

會打開一個新窗口,開始下載後會自動關閉新窗口。Safair 下載後沒有關閉新窗口。

Chrome、IE、Safair支持,貌似火狐不支持

1.2 window.location=url

在當前窗口下載

Chrome、Safair支持

1.3 iframe

在HTML中,iframe 的屬性用src,但在JS中,只有部份瀏覽器支持修改src(讀是沒問題),真正通用的是要修改對應框架的href值。

1.4 a href=”url” download=”filename”點擊鏈接下載/a

HTML5中給a標籤增加了一個download屬性,只要有這個屬性,點擊這個鏈接時瀏覽器就不在打開鏈接指向的文件,而是改為下載,目前只有chrome、firefox、opera、Edge支持。常用此方法點擊下載圖片。

IE既不支持a標籤的download屬性也不允許js調用a 標籤的click方法。

2. 前端直接導出文件到本地

2.1 將數據轉成DataURI用a標籤下載

a href=”DataURI” download=”filename”點擊鏈接下載/a

Data URI Scheme

Data URI Scheme是指可以在Web 頁面中包含圖片但無需任何額外的HTTP 請求的一類URI。 Data URI Scheme一般用於將經過base64編碼的數據嵌入網頁中,從而減少請求資源的鏈接數。IE8 之前的版本都不支持 data URI scheme。

DataURI的格式:

生成DataURI的方式

1. encodeURIComponent

使用這種方式,當數據過多時,URI長度容易超出瀏覽器限制。 encodeURIComponent常用來轉碼介面參數,為了避免伺服器收到不可預知的請求,對任何用戶輸入的作為URI部分的內容都需要用encodeURIComponent進行轉義。

2. URL.createObjectURL

URL.createObjectURL的參數是File對象或者Blob對象

IE10以下不支持URL.createObjectURL

2.2 windows.navigator.msSaveBlob IE10~Edge 專用

msSaveBlob 是IE10~Edge 私有方法。

2.3 execCommand

有的資料有提到IE9可以使用execCommand方法來保存數據到本地文件,但是我自己沒有驗證過,不知道是否可行。而且MDN文檔中execCommand沒有查到SaveAs命令。這塊只是做個小記錄。

js數據直接導出/下載數據到本地到方法總結

本文轉載自:

前端js實現數據寫csv文件,並下載

var data = [“1,2,3\n”,”4,5,6″] // 每一行數據為一個字元串,字元串用「,」隔開,並且在每一行加上換行符

var blob = new Blob(data, {type:”text/csv,charset=UTF-8″})

var csvUrl = URL.createObjectURL(blob)

var aEle = document.createElement(“a”)

aEle.download = “data.csv” //文件名隨意

aEle.href = csvUrl

aEle.click()

如何用JS或Ajax讀取csv文件

JS不能直接讀取CSV格式的文件,如果硬要讀,只能按照text的格式來讀,然後根據逗號或者什麼進行分組,

javascript 讀取csv文件

js讀取CSV格式數據,參考如下:

script type=”text/javascript”     

// This will parse a delimited string into an array of  

// arrays. The default delimiter is the comma, but this  

// can be overriden in the second argument.  

function CSVToArray( strData, strDelimiter ){  

// Check to see if the delimiter is defined. If not,  

// then default to comma.  

strDelimiter = (strDelimiter || “,”);  

   

// Create a regular expression to parse the CSV values.  

var objPattern = new RegExp(  

(  

// Delimiters.  

“(\\” + strDelimiter + “|\\r?\\n|\\r|^)” +  

   

// Quoted fields.  

“(?:\”([^\”]*(?:\”\”[^\”]*)*)\”|” +  

   

// Standard fields.  

“([^\”\\” + strDelimiter + “\\r\\n]*))”  

),  

“gi”  

);  

   

   

// Create an array to hold our data. Give the array  

// a default empty first row.  

var arrData = [[]];  

   

// Create an array to hold our individual pattern  

// matching groups.  

var arrMatches = null;  

   

   

// Keep looping over the regular expression matches  

// until we can no longer find a match.  

while (arrMatches = objPattern.exec( strData )){  

   

// Get the delimiter that was found.  

var strMatchedDelimiter = arrMatches[ 1 ];  

   

// Check to see if the given delimiter has a length  

// (is not the start of string) and if it matches  

// field delimiter. If id does not, then we know  

// that this delimiter is a row delimiter.  

if (  

strMatchedDelimiter.length   

(strMatchedDelimiter != strDelimiter)  

){  

   

// Since we have reached a new row of data,  

// add an empty row to our data array.  

arrData.push( [] );  

   

}  

   

   

// Now that we have our delimiter out of the way,  

// let’s check to see which kind of value we  

// captured (quoted or unquoted).  

if (arrMatches[ 2 ]){  

   

// We found a quoted value. When we capture  

// this value, unescape any double quotes.  

var strMatchedValue = arrMatches[ 2 ].replace(  

new RegExp( “\”\””, “g” ),  

“\””  

);  

   

} else {  

   

// We found a non-quoted value.  

var strMatchedValue = arrMatches[ 3 ];  

   

}  

   

   

// Now that we have our value string, let’s add  

// it to the data array.  

arrData[ arrData.length – 1 ].push( strMatchedValue );  

}  

   

// Return the parsed data.  

return( arrData );  

}  

   

/script

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/277597.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-19 13:21
下一篇 2024-12-19 13:21

相關推薦

發表回復

登錄後才能評論