我正在尝试使用PHPExcel将数据从我的数据库导出到Excel。我使用了this question中的代码。编辑mysql到mysqli并将扩展名更改为xlsx格式。但是,当我想打开文件时,我收到以下错误(我将其翻译成英文,如果不对,我很抱歉我的英文不好)
无法打开文件rapportage 29-05-2017.xlsx,因为文件格式或文件扩展名无效。检查文件是否已损坏,以及文件扩展名是否与文件格式匹配。
这是我复制和更改的代码:
<?php
require 'database.php';
require '../Classes/PHPExcel.php';
if (isset($_POST['exporteer'])) {
$a = $_POST['organisatie_naam'];
$b = $_POST['begin_datum'];
$c = $_POST['eind_datum'];
$datum = date("d-m-Y");
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM register WHERE organisatie_naam = '$a' AND (registratie_aansluit_datum BETWEEN '$b' AND '$c'";
$result = mysqli_query($db, $query);
$objPHPExcel = new PHPExcel();
$rowCount = 1;
while ($row = mysqli_fetch_array($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCount, $row['registratie_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowCount, $row['registratie_soort']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$rowCount, $row['organisatie_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$rowCount, $row['apparaat_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$rowCount, $row['client_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$rowCount, $row['dienst_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$rowCount, $row['functie_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$rowCount, $row['kosten_soort']);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$rowCount, $row['status_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('J'.$rowCount, $row['registratie_aansluit_datum']);
$objPHPExcel->getActiveSheet()->setCellValue('K'.$rowCount, $row['registratie_afsluit_datum']);
$objPHPExcel->getActiveSheet()->setCellValue('L'.$rowCount, $row['registratie_omschr']);
$rowCount++;
pr($objPHPExcel);
}
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="rapportage ' . $datum . '.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
header("Location: ../rapportage.php");
}
?>
任何知道我是否可能遗漏某些东西的人?提前谢谢!
答案 0 :(得分:1)
require('../phpexcel/PHPExcel.php');
require('../phpexcel/PHPExcel/Writer/Excel5.php');
$filename = 'userReport'; //your file name
$objPHPExcel = new PHPExcel();
/*********************Add column headings START**********************/
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'username')
->setCellValue('B1', 'city_name');
/*********************Add data entries START**********************/
//get_result_array_from_class**You can replace your sql code with this line.
$result = $get_report_clas->get_user_report();
//set variable for count table fields.
$num_row = 1;
foreach ($result as $value) {
$user_name = $value['username'];
$c_code = $value['city_name'];
$num_row++;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$num_row, $user_name )
->setCellValue('B'.$num_row, $c_code );
}
/*********************Autoresize column width depending upon contents START**********************/
foreach(range('A','B') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true);
//Make heading font bold
/*********************Add color to heading START**********************/
$objPHPExcel->getActiveSheet()
->getStyle('A1:B1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setARGB('99ff99');
$objPHPExcel->getActiveSheet()->setTitle('userReport'); //give title to sheet
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;Filename=$filename.xls");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');