使用 PHP 为 Mac 用户编写 CSV 文件

发布于 2024-08-27 19:03:08 字数 1307 浏览 9 评论 0原文

我使用通用算法来编写理论上适用于主要操作系统的 CSV 文件。然而,客户几周前开始使用Mac,他们一直告诉我CSV文件无法在Microsoft Excel 2008 for Mac 12.2.1中读取。

他们的操作系统配置为使用“分号;”作为列表分隔符,这正是我在 CSV 中编写的内容。他们还说,当他们在记事本中打开文件时,他们注意到没有换行符,所有内容都显示在一行中;这就是Excel无法正确读取文件的原因;但在我的代码中,我使用跨浏览器换行符 \r\n

这是我使用的完整代码:

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=participantes.csv");

$separator = ";";
$rs = $sql->doquery("SELECT A QUERY TO RETRIEVE DATA FROM THE DB");

$header = "";
$num_fields = mysql_num_fields($rs);

for($i=0; $i<$num_fields; $i++){
  $field = mysql_field_name($rs, $i);
  $header .= $field.$separator;
}

echo $header."\r\n";

while($row = $sql->fetch($rs)){
  $str = "";
  for($i=0; $i<$num_fields; $i++){
    $field = mysql_field_name($rs, $i);
    $value = str_replace(";", ",", $row->{$field});
    $value = str_replace("\n", ",", $value);
    $value = str_replace("\d", ",", $value);
    $value = str_replace(chr(13), ",", $value);
    $str .= $value.$separator;
  }
  echo $str."\r\n";
}

有什么我可以做的,以便 Mac 用户可以正确读取文件?

I use a generic algorithm to write CSV files that in theory works for the major OSes. However, the client started to use Mac a few weeks ago, and they keep telling me the CSV file cannot be read in Microsoft Excel 2008 for Mac 12.2.1.

They have their OS configured to use "semicolon ;" as list separator, which is exactly what I am writing in the CSV. They also say, that when they open the file in notepad, they have noticed there are no linebreaks, everything is displayed in a single line; which is why Excel cannot read the file properly; but in my code, I am using the cross-browser line break \r\n

This is the full code I use:

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=participantes.csv");

$separator = ";";
$rs = $sql->doquery("SELECT A QUERY TO RETRIEVE DATA FROM THE DB");

$header = "";
$num_fields = mysql_num_fields($rs);

for($i=0; $i<$num_fields; $i++){
  $field = mysql_field_name($rs, $i);
  $header .= $field.$separator;
}

echo $header."\r\n";

while($row = $sql->fetch($rs)){
  $str = "";
  for($i=0; $i<$num_fields; $i++){
    $field = mysql_field_name($rs, $i);
    $value = str_replace(";", ",", $row->{$field});
    $value = str_replace("\n", ",", $value);
    $value = str_replace("\d", ",", $value);
    $value = str_replace(chr(13), ",", $value);
    $str .= $value.$separator;
  }
  echo $str."\r\n";
}

Is there anything I can do so Mac users can read the file properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摇划花蜜的午后 2024-09-03 19:03:08

出于调试目的:

  1. 创建 CSV 文件并通过邮件发送给他们。他们能打开吗?
  2. 让他们从您的页面下载文件并将其发送回给您。在十六进制编辑器中比较文件,以排除它们与您发送到浏览器的文件或您保存的文件看起来不同的可能性。
  3. 让他们仔细检查他们的 Excel 设置。
  4. 让他们从头开始创建一个工作 CSV 文件(Mac 上的文本编辑器),并找出与您的方法的任何差异。

For debugging purposes:

  1. Create a CSV file and send it to them by mail. Can they open it OK?
  2. Have them download the file from your page and have it sent back to you. Compare the files in a Hex-editor to rule out the off-chance that they look differently from what you send to the browser or from what you have saved.
  3. Have them double-check their Excel-settings.
  4. Have them create a working CSV file from scratch (text editor on a mac) and spot any differences from your approach.
如果没结果 2024-09-03 19:03:08

下面是我将制表符分隔的数据转换为 CSV 的一些代码,它在我的 Mac 上运行良好。请注意,我将其设置为让我单击下载,而不是将其推送到浏览器。这不是一个很好的解决方案(我很确定代码很糟糕),但它可以满足我的需要。

$input = $_POST['input'];
//Remove commas.
$replacedinput1 = str_replace(",", "-", $input);
//remove tabs, replace with commas
$replacedinput2 = str_replace(" ", ",", $replacedinput1);
//create an array
$explodedinput = explode("
", $replacedinput2);
//open the CSV file to write to; delete other text in it
$opencsvfile = fopen("/var/www/main/tools/replace_tab.csv","w+");
//for each line in the array, write it to the file
foreach($explodedinput as $line) {
fputcsv ($opencsvfile, split(',', $line));
};
//close the file
fclose($opencsvfile);
//have the user download the file.
/*
header('Pragma: public');
header('Expires: Fri, 01 Jan 2010 00:00:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/csv');
header('Content-Disposition: filename=replace_tab.csv'); */

//Or not, since I can't get it to work.
echo "<a href='replace_tab.csv'>Download CSV File, then open in Numbers or Excel (Note, you may need to right click, save as.)</a>.";

Here's some code I did to convert a tab delimited data into CSV, and it comes in fine on my mac. Note that I have it set up to make me click to download, rather than pushing it to the browser. It's not a great solution (I'm pretty sure the code is crap) but it works for what I need it for.

$input = $_POST['input'];
//Remove commas.
$replacedinput1 = str_replace(",", "-", $input);
//remove tabs, replace with commas
$replacedinput2 = str_replace(" ", ",", $replacedinput1);
//create an array
$explodedinput = explode("
", $replacedinput2);
//open the CSV file to write to; delete other text in it
$opencsvfile = fopen("/var/www/main/tools/replace_tab.csv","w+");
//for each line in the array, write it to the file
foreach($explodedinput as $line) {
fputcsv ($opencsvfile, split(',', $line));
};
//close the file
fclose($opencsvfile);
//have the user download the file.
/*
header('Pragma: public');
header('Expires: Fri, 01 Jan 2010 00:00:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/csv');
header('Content-Disposition: filename=replace_tab.csv'); */

//Or not, since I can't get it to work.
echo "<a href='replace_tab.csv'>Download CSV File, then open in Numbers or Excel (Note, you may need to right click, save as.)</a>.";
腻橙味 2024-09-03 19:03:08

Mac 中的换行符是 LF(unicode U+000A),Windows 中的换行符是 CR + LF(Unicode U+000D 后跟 U+000A)。

也许这就是 csv 在 Mac 上无法读取的原因。

The newline character in Mac is LF (unicode U+000A) and in Windows it is CR + LF (Unicode U+000D followed by U+000A).

Maybe that's why the csv is unreadable on a Mac.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文