javascript - 从表到 CSV 文件的日期输出
问题描述
从表格行输出日期时遇到问题。所以基本上我有一个表,其中包含从 PHP 中选择的数据。我尝试使用 JavaScript 将表格数据输出到 Excel(通过网站找到)。除了输出“######”的日期外,Excel 中的所有数据看起来都不错。
这是我的 JavaScript
<script>
function exportData(){
/* Get the HTML data using Element by Id */
var table = document.getElementById("report");
/* Declaring array variable */
var rows =[];
//iterate through rows of table
for(var i=0,row; row = table.rows[i];i++){
//rows would be accessed using the "row" variable assigned in the for loop
//Get each cell value/column from the row
column1 = row.cells[0].innerText;
column2 = row.cells[1].innerText;
column3 = row.cells[2].innerText;
column4 = row.cells[3].innerText;
column5 = row.cells[4].innerText;
column6 = row.cells[5].innerText;
column7 = row.cells[6].innerText;
column8 = row.cells[7].innerText;
/* add a new records in the array */
rows.push(
[
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8
]
);
}
csvContent = "data:text/csv;charset=utf-8,";
/* add the column delimiter as comma(,) and each row splitted by new line character (\n) */
rows.forEach(function(rowArray){
row = rowArray.join(",");
csvContent += row + "\r\n";
});
/* create a hidden <a> DOM node and set its download attribute */
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "Report.csv");
document.body.appendChild(link);
/* download the data file named "Stock_Price_Report.csv" */
link.click();
}
解决方案
您必须扩展列的大小date
推荐阅读
- magento - Magento 2 - isSalable() 函数背后的逻辑是什么?
- spring-boot - @EnableWebSocket 和 @EnableWebSocketMessageBroker 有什么区别
- javascript - 编写一个正则表达式来匹配这些 ?, ~, ^, | 和 *
- javascript - 如何将 json 数据中的空格替换为 _ 并使用数据表为每一行添加超链接?
- java - 获取 JSON 对象的特定对象(XML 类型)
- yii2 - Yii2 自动更改用户密码
- api - 我的包中的 Swagger-YAML 错误映射条目
- ios - React Native IOS 11.3 - 11.4 错误:JavaScriptCore JSC::JSLock::DropAllLocks::DropAllLocks
- if-statement - IF 语句 - 如何减少/简化源代码量
- objective-c - NSTableView 与 NSArrayController 的竞价不适用于 Storyboard Objective C