我正在尝试将一些数据从mysql导出到“PHPExcel”
我想要一个“保存对话框”,而不是将文件保存在服务器上。
我有谷歌它但找不到更多“添加另一个标题”..所以我一直在添加很多标题:)< BR />
但Chrome和Firefox都只显示表格 不,保存对话框打开 为什么呢?
将文件保存在服务器上工作正常!
PHP
<?php
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: Binary");
//Load db connection
require("conf.php");
//Check if usr is logged in
if(empty($_SESSION['user'])){
header("Location: index.php");
die("Redirecting to index.php");
}
//Check IF post
if(!empty($_POST['objNr'])){
//Store post in var
$objNr = $_POST['objNr'];
/** Error reporting */
error_reporting(E_ALL);
//Load phpexcel includes
require '../Classes/PHPExcel.php';
/** PHPExcel_Writer_Excel2007 */
include '../Classes/PHPExcel/Writer/Excel2007.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Nr');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Höjd');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Typ');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Längd');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'AV');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Öppningar');
$objPHPExcel->getActiveSheet()->SetCellValue('G1', 'Vikt');
//Fetch data from DB
$query = "SELECT * FROM element WHERE objekt_nr = '$objNr' ORDER BY length(element_nr), element_nr ASC";
try{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
//Insert på first row after heading
$row = 2;
while($value = $stmt->fetch()){
//Set start Column
$column = "A";
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['element_nr']);
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['hojd']);
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['typ']);
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['langd']);
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['avdrag']."");
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['oppningar']."");
$objPHPExcel->getActiveSheet()->SetCellValue($column++.$row, $value['vikt']);
//INCREASE Row Nr
$row++;
}
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle($objNr);
// Write file to the browser
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('file.xlsx');
}
else{
echo 'Välj objektnummer..';
}
答案 0 :(得分:0)
您需要$objWriter->save('php://output');
代替$objWriter->save('file.xlsx');