当我在php中下载excel文件时,会在第一个位置插入一个换行符(0x0A)

当我在php中下载excel文件时,会在第一个位置插入一个换行符(0x0A),php,excel,download,newline,Php,Excel,Download,Newline,下面是使用php编写和下载xsl文件的代码 打开excel文件时出现问题 换行符(0x0A)将插入第一个字符 我要删除换行符(0x0A)字符 帮帮我。图特 [当我在excel程序中打开test.xls时] [它已转换为十六进制代码。]就像其他开发人员在评论中指出的那样,您应该发布整个代码,但由于我面临同样的问题,我将与您分享我的工作经验 在config.php中搜索?>(php关闭标记)以及主代码中包含的其他文件 100%的几率在php关闭标记后有一个额外的行刹车,就像我在代码中看到的那样。

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

打开excel文件时出现问题

换行符(0x0A)将插入第一个字符

我要删除换行符(0x0A)字符

帮帮我。图特

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


[它已转换为十六进制代码。]

就像其他开发人员在评论中指出的那样,您应该发布整个代码,但由于我面临同样的问题,我将与您分享我的工作经验

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

100%的几率在php关闭标记后有一个额外的行刹车,就像我在代码中看到的那样。把它拿走,你就可以走了

例如:

主代码:(index.php)


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


该代码不编写任何xsl(甚至Excel文件),它只是告诉web浏览器需要一个Excel xls文件,然后什么都不做是的,你说的对。内容是空的。但是,当我执行此操作时,会在第一个位置插入一个换行符(0x0A)。即使有内容,也是一样的:(请显示更多代码。例如,包含的php文件(之前)中的换行符可能会成为输出的一部分。我正在更新我的问题。示例代码只是为了下载Excel。尽管我只下载了Excel文件,但换行符(0x0A)在第一个字符处插入了字符。这样,您只需生成一个包含文本测试的文本文件(没有换行符),而不是Excel文件。Excel如何处理导入文本文件是另一个问题,但它肯定不应命名为.xls。
<?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
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;
<?php
$some_code_config = 1;
?>
<!-- wipe out any space/chars/line-brakes you found here, including this comment -->