从针对mysql表

时间:2016-08-04 19:13:39

标签: php mysql excel phpexcel

我正在测试这段代码,以便从针对mysql表的查询中生成另一个文件夹中的excel文件。它会创建一个空的excel文件,单元格中没有数据。但文件大小不是0.

<?php
    require("mysql_conn.inc");
    require_once('PHPExcel.php');
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);

    $query = "SELECT `col1`,`col2`";
    $query .= " FROM `table` limit 0,100";
    $result = mysql_query($query)or die(mysql_error());
    $rowCount = mysql_num_rows($result);             
    $result = mysql_query($query)or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['col1']);
        $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['col2']);
        $rowCount++;
    }
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->save('some_excel_file.xlsx');

    ?>

这是修复rowcount变量后的代码

<?php
require("mysql_conn.inc");
require_once('/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$query = "SELECT `col1`,`col2`";
$query .= " FROM `table` limit 0,100";
$result = mysql_query($query)or die(mysql_error());
$rowcount=1;
while($row = mysql_fetch_array($result)){
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['col1']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['col2']);
    $rowcount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file.xlsx');

?>

0 个答案:

没有答案