如何在使用PHP导出excel时删除警告


How to remove warning when exporting excel using PHP

当我使用PHP导出Excel时,有人能帮助我如何删除此警告吗。

"您试图打开的文件"MyFile.xls"的格式与文件扩展名指定的格式不同。打开该文件之前,请验证该文件是否已损坏,并且来源可靠。是否立即打开该文件?"

我完全理解这个警告,说Excel内容有不同的格式。我只想知道如何/如何删除此警告

<?php
    header("Content-type:   application/x-msexcel; charset=utf-8");
    header("Content-Disposition: attachment; filename=MyFile.xls");
?>

现在我只有上面的代码,这意味着还没有显示,我正在尝试在填充任何内容之前先解决这个问题。当前安装的版本是MS Office 2007。我想试试PHPExcel,但我不确定它是否能解决这个问题。

提前感谢!

PHPExcel中的上述答案将给您带来良好的结果。但是,如果你想用corePHP实现它,那么下面是要实现的代码。

<?php
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
}
function xlsWriteLabel($Row, $Col, $Value) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
} 
// prepare headers information
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename='"export_".date("Y-m-d").".xls'"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
// start exporting
xlsBOF();
// first row 
xlsWriteLabel(0, 0, "id");
xlsWriteLabel(0, 1, "name");
xlsWriteLabel(0, 2, "email");
// second row 
xlsWriteNumber(1, 0, 230);
xlsWriteLabel(1, 1, "John");
xlsWriteLabel(1, 2, "john@yahoo.com");
// third row 
xlsWriteNumber(2, 0, 350);
xlsWriteLabel(2, 1, "Mark");
xlsWriteLabel(2, 2, "mark@yahoo.com");
// end exporting
xlsEOF();
exit;
?>

引用自http://krasimirtsonev.com/blog/article/php-export-mysql-data-to-xls-file

您可能正在保存扩展名为XLS的CSV文件。将"内容类型"更改为text.csv,将文件扩展名更改为.csv,默认情况下,它会在Excel中打开,并应防止显示此警告。

您确定要导出excel文档吗?

如果是:请尝试以下标题:

header("Pragma: public");
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="MyFile.xls"');

这不会给我的MS Office 2007安装带来错误

我也收到了这个警告。

然后我使用了PHPExcel。

可能会帮助你。http://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=主页

您可以使用:

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

或者使用以下代码:

//包括PHPExcel_IOFactory

include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example1.xls';
//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();
//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);
    //  Insert row data array into your database of choice here
}

参考:

http://www.grad.clemson.edu/assets/php/phpexcel/documentation/api/__filesource/fsource_phpexcel__phpexceliofactory.php.html