当用php到excel格式创建文件时,我如何摆脱excel荒谬的自动格式


How do I get rid of excels ridiculous auto format when creating a file in php to excel format

我有一个程序,它使文件成为excel文件,我有它,所以它摆脱了哑格式的mc-excel,但不会摆脱#######

这种格式出现在mc-excel中——似乎一个单元格中有符号。但并非所有单元格都是这样。

清理代码为:

function cleanData(&$str)
  {
   // escape tab characters
    $str = preg_replace("/'t/", "''t", $str);
    // escape new lines
    $str = preg_replace("/'r?'n/", "''n", $str);
    // convert 't' and 'f' to boolean values
    if($str == 't') $str = 'TRUE';
    if($str == 'f') $str = 'FALSE';
    // force certain number/date formats to be imported as strings
    if(preg_match("/^0/", $str) || preg_match("/^'+?'d{8,}$/", $str) || preg_match("/^'d{4}.'d{1,2}.'d{1,2}/", $str)) {
      $str = "'$str";
    }
    // escape fields that include double quotes
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

如果你还需要什么,请告诉我!

谢谢。

当单元格内容对于默认列大小来说太大时,MS Excel会显示"########":这与文本中的符号无关。单元格中的值仍然正确,但在使用制表符分隔的值文件时无法设置列宽,因此除非切换到真正的Excel文件,否则无法对此进行控制。

如果必须使用制表符分隔的值文件;与其编写自己的转换为制表符分隔的值,为什么不使用PHP内置的fputcsv()函数呢?