2015-08-28 75 views
1

有沒有什麼特定的方法可以控制csv文件列中每個數據的更新。我希望有幾列是文本格式,數字格式很少。以特定格式導出到csv

對於如:

我要與標題爲 「股票」 更新此列和值是:

250.000,10.000,0.000,0.000,2.500,30.000,700.000,10.000 ,250.000,0.000。

但是,導出的csv文件未取十進制值。它顯示爲

250,10,0,0,2.5等

通常手動我可以去csv文件中的每一列,選擇它,單擊右鍵,格式單元格,然後改變格式爲文本或數字。不過,我不想手動執行此操作。我想在導出csv時自動完成。 代碼:

$.each(exportArray, function (index, value) { 
    csvData.push(x[index] + "," + y[index] + "," + d[index] + "," + z[index] + ","+ a[index] + "," + e[index] + "," + b[index] + "," + c[index]); 
}); 

csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(output); 

$(this) 
    .attr({ 
     'download': filename, 
     'href': csvData, 
     'target': '_blank' 
    }); 

回答

1

看到這個問題和接受的答案 - Format number to always show 2 decimal places

從本質上講,你將需要這樣做:

function toFixed3(n) { 
    return parseFloat(Math.round(n * 1000)/1000).toFixed(3); 
} 

csvData.push(toFixed3(x[index]) + "," + toFixed3(y[index]) + "," + 
    toFixed3(d[index]) + "," + toFixed3(z[index]) + ","+ 
    toFixed3(a[index]) + "," + toFixed3(e[index]) + "," + 
    toFixed3(b[index]) + "," + toFixed3(c[index])); 
0

在一個CSV文件,你不能定義什麼格式化單元格。
Excel將使用數字的默認格式。

如果你想控制單元格格式,你應該使用不同的文件格式,如SYLK

+0

謝謝大家的投入..讓我試試.. – User37