PHPExcel不起作用

时间:2014-02-12 14:47:09

标签: php mysql sql phpexcel

我正在尝试使用PHPExcel将我在PHP中生成的SQL查询打印到Excel文件中。我不确定我在哪里犯了错误,但每次运行我的代码时,我都会得到一个空白页。

这是我的代码:

<html>
<body>

<?php
mysql_connect("$host", $user, $password) or die("Couldn't connect to SQL Server on     $myServer");
mysql_select_db("$database") or die("Couldn't open database $myDB");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
require_once("includes/session.php"); 
require_once("includes/connection.php"); 
require_once("includes/functions.php");

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '
');

/** Include PHPExcel */
require_once '/Classes/PHPExcel.php';


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

$filter = $_SESSION['registry'];
$query = "SELECT sales.invoiceNo, invoiceDate, dueDate, customerID, customerName, dateTrans, postDate, description, grossSale, vat, netSale, quantity, branch, despatchNote  FROM sales2012 sales WHERE sales.invoiceNo IN (SELECT invoiceNo FROM sales2012 GROUP BY invoiceNo HAVING COUNT(invoiceNo)>1)";
$result_set = mysql_query($query);
        confirm_query($result_set);

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Invoice No')
        ->setCellValue('B1', 'Invoice Date')
        ->setCellValue('C1', 'Due Date')
        ->setCellValue('D1', 'Customer ID')
        ->setCellValue('E1', 'Customer Name')
        ->setCellValue('F1', 'Transaction Date')
        ->setCellValue('G1', 'Post Date')
        ->setCellValue('H1', 'Description')
        ->setCellValue('I1', 'Gross Sale')
        ->setCellValue('J1', 'VAT')
        ->setCellValue('K1', 'Net Sale')
        ->setCellValue('L1', 'Quantity')
        ->setCellValue('M1', 'Branch')
        ->setCellValue('N1', 'Despatch Note');


        $row = 2;
       while ($data = mysql_fetch_array($result_set)) {
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A' . $row, $data['invoiceNo'])
        ->setCellValue('B'. $row, $data['invoiceDate'])
        ->setCellValue('C'. $row, $data['dueDate'])
        ->setCellValue('D'. $row, $data['customerID'])
        ->setCellValue('E'. $row, $data['customerName'])
        ->setCellValue('F'. $row, $data['dateTrans'])
        ->setCellValue('G'. $row, $data['postDate'])
        ->setCellValue('H'. $row, $data['description'])
        ->setCellValue('I'. $row, $data['grossSale'])
        ->setCellValue('J'. $row, $data['vat'])
        ->setCellValue('K'. $row, $data['netSale'])
        ->setCellValue('L'. $row, $data['quantity'])
        ->setCellValue('M'. $row, $data['branch'])
        ->setCellValue('N'. $row, $data['descriptionNote']);
        $row++;
       }

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


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('__FILE__,'/desktop/filename.xls',__FILE__));

?>
</body>
</html>

任何帮助将不胜感激! 感谢

2 个答案:

答案 0 :(得分:0)

也许因为你有include:librarie excel2007

  

require_once( “类/ PHPExcel /作家/ Excel2007.php”);

当您在excel2007中保存文档时,扩展名为.xlsx

  

$ objWriter-&GT;保存(str_replace函数( '文件下,'/桌面/ filename.xls',文件));

这可以是一个选项

  

$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel2007');

     

$ objWriter-&GT;保存( 'PHP://输出')

答案 1 :(得分:0)

$objWriter->save(
    str_replace('__FILE__,'/desktop/filename.xls',__FILE__)
);

你的str_replace()不是PHP的有效语法,我很惊讶这个错误没有被记录在某处...不匹配的引号,正如任何体面的语法高亮编辑器会告诉你的那样

愚蠢的是,你甚至不需要str_replace。 PHPExcel Writer save()方法需要一个字符串值作为文件名,它不关心如何生成该字符串,所以只需传递一个字符串

$objWriter->save('/desktop/filename.xls');