我正在尝试使用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>
任何帮助将非常感激!
谢谢
最佳答案
也许bucause您包括:librarie excel2007
require_once(“ Classes / PHPExcel / Writer / Excel2007.php”);
当您在excel2007中保存文档时,扩展名为.xlsx
$ objWriter-> save(str_replace('FILE,'/ desktop / filename.xls',FILE));
这可以是一个选择
$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel2007');
$ objWriter-> save('php:// output')