【问题标题】:When i download the excel file in php, A newline(0x0A) character is inserted at the first当我在 php 中下载 excel 文件时,首先插入一个换行符(0x0A)字符
【发布时间】:2017-05-04 08:16:08
【问题描述】:
<?php

    header("Content-Type:application/vnd.ms-excel;charset=utf-8");
    header("Content-Disposition:attachment;filename=test.xls");
    header("Cache-Control:max-age=0"); 

    echo 'test';


    // 'when i open the excel after download, A newline(0x0A) character have been inserted at the first character.'

    exit; 
?>

这是使用 php 编写和下载 xsl 文件的代码。

当我打开 excel 文件时出现我的问题。

在第一个插入换行符(0x0A)。

我想删除换行符(0x0A)。

帮帮我。 T_T

enter image description here

[当我在excel程序中打开test.xls时]

enter image description here

[已转换为十六进制码]

【问题讨论】:

  • 该代码不写任何 xsl(甚至 Excel 文件),它只是告诉网络浏览器期待一个 excel xls 文件,然后什么也不做
  • 是的,你的权利。内容为空。但是,当我执行此操作时,首先会插入一个换行符(0x0A)字符。就算有内容也是一样的:(
  • 请显示更多您的代码。例如,包含的 php 文件中的换行符(在 之后)可能会成为输出的一部分。
  • 我正在更新我的问题。示例代码只是为了下载 Excel。即使我只下载了 Excel 文件,也已在第一个字符处插入了换行符(0x0A)。
  • 这样您只需要生成一个 text 文件,其中包含文本测试(并且没有任何换行符),不是 Excel 文件。 Excel 如何处理导入文本文件是另一个问题,但它肯定不应该命名为 .xls。

标签: php excel download newline


【解决方案1】:

就像其他开发人员在 cmets 中指出的那样,您应该发布整个代码,但由于我面临同样的问题,我将与您分享对我有用的方法。

在 config.php 和主代码中包含的其他文件中搜索 ?>(php 关闭标签)。

100% 的机会在我的代码中的 php 关闭标记之后有一个额外的线路制动。删除它,你就可以开始了。

例子:

主要代码:(index.php)

<?php
include_once 'config.php';
$some_file_path = '/var/www/html/image1.jpg';
$some_file_mime_type = 'image/jpeg';
$some_file_download_name = 'download.jpg';
$data_to_download = fread(fopen($some_file_path, 'r'), filesize($some_file_path));
header("Content-type: ".$some_file_mime_type);
header("Pragma: public");
header("Cache-Control: private");
header("Content-Disposition: attachment; filename=\"$some_file_download_name\"");
header("Content-Description: PHP Generated Data");
header("Content-Encoding: none");
echo $data_to_download;

配置代码:(config.php)

<?php
$some_code_config = 1;
?>
<!-- wipe out any space/chars/line-brakes you found here, including this comment -->

如果您在配置文件末尾的 php 结束标记之后有额外的字符,您的下载将如何结束:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多