You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JavaScript导出HTML表格为CSV时日期列显示######如何解决

CSV导出后日期列显示######的解决方案

这个现象是Excel的默认展示逻辑导致的,你导出的CSV文件本身存储的日期数据没有错误,可通过两类方法解决:

一、打开文件后手动修复

  • 用鼠标拖动日期列的列边缘拉宽列宽,90%以上的场景下井号会自动消失,展示为正常日期。Excel默认单元格宽度不足以放下完整日期内容时,就会用######填充展示,不会修改实际存储的内容
  • 如果拉宽列宽后仍显示异常,右键选中日期列,选择「设置单元格格式」,在分类列表中选择「日期」,匹配你导出的日期格式确认即可

二、修改导出代码,避免用户手动调整

你可以在导出日期字段时,给内容前拼接一个制表符\t,强制Excel将该字段识别为文本格式,不会自动调整格式也不会触发列宽不足的井号提示。
假设你代码里的第7列(对应column7)是日期字段,修改后的代码如下:

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 = '\t' + 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();
}

如果你的日期字段不在第7列,对应修改对应列的赋值逻辑即可。

内容的提问来源于stack exchange,提问作者Aiman Haziq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 16:36:04