2014-02-06 49 views
0

如何讓我的代碼在Excel中手動保存?因爲到目前爲止,在我的代碼中,它會將我過濾的數據從mysql數據庫傳輸到Excel文件中......我如何在不在我的程序文件夾中生成Excel文件的情況下手動保存,以便在我的數據中生成Excel報告。如何將數據保存到Excel中手動執行

我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<?php 
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php'; 
$filename = 'file.xlsx'; 
mysql_connect("localhost","root","") or die ("cant connect!"); 
mysql_select_db("test") or die ("cant find database!"); 

$objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
$objReader->setReadDataOnly(true); 

$objPHPExcel = $objReader->load($filename); 
$objWorksheet = $objPHPExcel->getActiveSheet(); 
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

$results = mysql_query("SELECT * FROM score"); 
while($row = mysql_fetch_assoc($results)){ 
} 
$result = mysql_query("SELECT * FROM score"); 
if(isset($_POST['send'])){ 

$headings = array(
    'ID', 
    'NAME', 
    'SCORE 1', 
    'SCORE 2', 
    'OTHER QUALITIES', 
    'INTERVIEW', 
    'TOTAL', 
    'AIC', 
    'BATCHCODE', 
); 
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1'); 
$row = 2; 
while($rows = mysql_fetch_row($result)){ 
    $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row); 
    $row++; 
} 
echo 'saved'; 
header('Location: Index.php'); 
} 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save($filename); 
?> 
<form id="form1" name="form1" method="post" action="" > 
<input type="submit" name="send" value="send to excel" id="send" /> 
</form> 
</body> 
</html> 

我想要做的是,如果我點擊發送到Excel(按鈕)...一個Excel保存對話框就會彈出爲了節省我的過濾數據到Excel。

+0

消除來自腳本或非PHP阻止所有呼應或印刷字符(如您' ....'標記標籤,等);發送適當的頭文件告訴瀏覽器期望一個Excel格式文件,將文件保存到'php:// output' .... ....完全如PHPExcel發行版中的Examples/01simple-download-xls.php所示 –

+0

@Mark Ba​​ker可以編輯我的代碼...我只是一個新手程序員...我試過這個例子,但它automaticaly下載文件...我想要的是,如果我點擊按鈕一個Excel保存對話框將彈出,以便我可以手動改變文件名....你知道該怎麼做嗎? – xplody

回答

2
<?php 
if (!isset($_POST['send']) { ?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <title>test</title> 
    </head> 
    <body> 
<?php } else { 
    require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php'; 
    $filename = 'file.xlsx'; 
    mysql_connect("localhost","root","") or die ("cant connect!"); 
    mysql_select_db("test") or die ("cant find database!"); 

    $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    $objReader->setReadDataOnly(true); 

    $objPHPExcel = $objReader->load($filename); 
    $objWorksheet = $objPHPExcel->getActiveSheet(); 
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

    $result = mysql_query("SELECT * FROM score"); 
    if(isset($_POST['send'])){ 

     $headings = array(
      'ID', 
      'NAME', 
      'SCORE 1', 
      'SCORE 2', 
      'OTHER QUALITIES', 
      'INTERVIEW', 
      'TOTAL', 
      'AIC', 
      'BATCHCODE', 
     ); 
     $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1'); 
     $row = 2; 
     while($rows = mysql_fetch_row($result)){ 
      $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row); 
      $row++; 
     } 
    } 

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="01simple.xlsx"'); 
    header('Cache-Control: max-age=0'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save('php://output'); 
} 
if (!isset($_POST['send']) { ?> 
    <form id="form1" name="form1" method="post" action="" > 
    <input type="submit" name="send" value="send to excel" id="send" /> 
    </form> 
    </body> 
    </html> 
<?php } 
+0

解析錯誤:語法錯誤,在第2行的C:\ xampp \ htdocs \ test \ index.php中出現意外的'{'...出錯 – xplody

+0

ive修復了它謝謝 – xplody

相關問題