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

时间:2012-08-03 09:32:16

标签: php export-to-excel

当我使用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,但我不确定它是否可以解决问题。

提前致谢!

5 个答案:

答案 0 :(得分:1)

您可能正在保存带有XLS扩展名的CSV文件。 将Content-type更改为text / csv,将文件扩展名更改为.csv,默认情况下会在Excel中打开,并且应该阻止显示此警告。

答案 1 :(得分:0)

您确定要导出Excel文档吗?

如果是这样:尝试下面的标题:

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

这不会导致我的MS Office 2007安装错误

答案 2 :(得分:0)

我也收到了这个警告。

然后我使用了PHPExcel。

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

答案 3 :(得分:0)

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

的参考资料

答案 4 :(得分:0)

您可以使用:

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

相关问题