gpt4 book ai didi

PHPExcel 输出不可读并且没有抛出错误

转载 作者:行者123 更新时间:2023-12-02 04:58:20 24 4
gpt4 key购买 nike

你好,我有以下代码

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");

$exchange = $_POST['exchange'];
$jobchange = $_POST['estimate'];
$wpchange = $_POST['wp'];

$username = "----";
$password = "----";
$hostname = "----";

$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("----", $dbhandle) or die("Could not select examples");

$query = "SELECT * FROM btsec WHERE WP='$wpchange' AND Exchange='$exchange' AND Estimate='$jobchange'";

$result = mysql_query($query);

$acellnum = "3";
$bcellnum = "3";
$ccellnum = "3";
$dcellnum = "3";

$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Section ID')
->setCellValue('B1', 'Length')
->setCellValue('C1', 'Status')
->setCellValue('D1', 'TM');

while ($row = mysql_fetch_array($result)) {
// Query
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A".$acellnum, $row['SectionID'])
->setCellValue("B".$bcellnum, $row['Length'])
->setCellValue("C".$ccellnum, $row['Status'])
->setCellValue("D".$dcellnum, $row['TM']);

$acellnum++;
$bcellnum++;
$ccellnum++;
$dcellnum++;
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

它正在生成 Excel 文件,但它输出的只是类似于此的文本

ÐÏࡱá;þÿ

在文本编辑器中打开没有发现明显的错误,脚本也没​​有返回任何错误。我正在尝试将其输出为 Excel 2007 兼容格式。有谁知道为什么会发生这种情况?

编辑:可能不相关,但 Excel 会抛出格式与扩展名不匹配的错误

最佳答案

// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="sectionlist.xlsx"');
$objWriter->save('php://output');

这修复了它并使其正常工作! ob_end_clean 就是解决方案。

关于PHPExcel 输出不可读并且没有抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21290477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com