0

我正在使用以下代码通过 php 从 sql 生成 excel。这是工作代码的模式版本..

工作代码(无错误)

<?php
//documentation on the spreadsheet package is at:
//http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php
chdir('phpxls');
require_once 'Writer.php';
chdir('..');

$sheet1 =  array(
  array('eventid','eventtitle'       ,'datetime'           ,'description'      ,'notes'      ),
  array('5'      ,'Education Seminar','2010-05-12 08:00:00','Increase your WPM',''           ),
  array('6'      ,'Work Party'       ,'2010-05-13 15:30:00','Boss\'s Bday'     ,'bring tacos'),
  array('7'      ,'Conference Call'  ,'2010-05-14 11:00:00','access code x4321',''           ),
  array('8'      ,'Day Off'          ,'2010-05-15'         ,'Go to Home Depot' ,''           ),
);

$sheet2 =  array(
  array('eventid','funny_name'   ),
  array('32'      ,'Adam Baum'    ),
  array('33'      ,'Anne Teak'    ),
  array('34'      ,'Ali Katt'     ),
  array('35'      ,'Anita Bath'   ),  
  array('36'      ,'April Schauer'),
  array('37'      ,'Bill Board'   ),
);

$workbook = new Spreadsheet_Excel_Writer();

$format_und =& $workbook->addFormat();
$format_und->setBottom(2);//thick
$format_und->setBold();
$format_und->setColor('black');
$format_und->setFontFamily('Arial');
$format_und->setSize(8);

$format_reg =& $workbook->addFormat();
$format_reg->setColor('black');
$format_reg->setFontFamily('Arial');
$format_reg->setSize(8);

$arr = array(
      'Calendar'=>$sheet1,
      'Names'   =>$sheet2,
      );

foreach($arr as $wbname=>$rows)
{
    $rowcount = count($rows);
    $colcount = count($rows[0]);

    $worksheet =& $workbook->addWorksheet($wbname);

    $worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
    $worksheet->setColumn(1,3,15.00);
    $worksheet->setColumn(4,4, 8.00);

    for( $j=0; $j<$rowcount; $j++ )
    {
        for($i=0; $i<$colcount;$i++)
        {
            $fmt  =& $format_reg;
            if ($j==0)
                $fmt =& $format_und;

            if (isset($rows[$j][$i]))
            {
                $data=$rows[$j][$i];
                $worksheet->write($j, $i, $data, $fmt);
            }
        }
    }
}

$workbook->send('test.xls');
$workbook->close();

//-----------------------------------------------------------------------------
?>

修改代码以从数据库中检索数据(显示错误)

<?php

    $numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC"); 

    mysql_select_db("brainoidultrafb", $link);
    chdir('phpxls');
    require_once 'Writer.php';
    chdir('..');

    while($row = mysql_fetch_array($numrow12)) {
        $sheet1 =  array(
      array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,),
      array($row['student_id'] ,$row['email_id'],$row['diagnosis'],$row['tstamp'],),
    );

    } 



    $workbook = new Spreadsheet_Excel_Writer();

    $format_und =& $workbook->addFormat();
    $format_und->setBottom(2);//thick
    $format_und->setBold();
    $format_und->setColor('black');
    $format_und->setFontFamily('Arial');
    $format_und->setSize(8);

    $format_reg =& $workbook->addFormat();
    $format_reg->setColor('black');
    $format_reg->setFontFamily('Arial');
    $format_reg->setSize(8);

    $arr = array(
          'Calendar'=>$sheet1,
              );

    foreach($arr as $wbname=>$rows)
    {
        $rowcount = count($rows);
        $colcount = count($rows[0]);

        $worksheet =& $workbook->addWorksheet($wbname);

        $worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
        $worksheet->setColumn(1,3,15.00);
        $worksheet->setColumn(4,4, 8.00);

        for( $j=0; $j<$rowcount; $j++ )
        {
            for($i=0; $i<$colcount;$i++)
            {
                $fmt  =& $format_reg;
                if ($j==0)
                    $fmt =& $format_und;

                if (isset($rows[$j][$i]))
                {
                    $data=$rows[$j][$i];
                    $worksheet->write($j, $i, $data, $fmt);
                }
            }
        }
    }

    $workbook->send('Submissions.xls');
    $workbook->close();

    //-----------------------------------------------------------------------------
    ?>

它显示以下错误

警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在第 13 行的 /home/content/58/9508458/html/psychiatric/subexcel.php 中给出

警告:无法修改标头信息 - 标头已由 /home/content/58/9508458/html/psychiatric/phpxls/ 中的(输出开始于 /home/content/58/9508458/html/psychiatric/subexcel.php:13)发送Writer.php 在第 67 行

警告:无法修改标头信息 - 标头已由 /home/content/58/9508458/html/psychiatric/phpxls/ 中的(输出开始于 /home/content/58/9508458/html/psychiatric/subexcel.php:13)发送Writer.php 在第 68 行

警告:无法修改标头信息 - 标头已由 /home/content/58/9508458/html/psychiatric/phpxls/ 中的(输出开始于 /home/content/58/9508458/html/psychiatric/subexcel.php:13)发送Writer.php 在第 69 行

警告:无法修改标头信息 - 标头已由 /home/content/58/9508458/html/psychiatric/phpxls/ 中的(输出开始于 /home/content/58/9508458/html/psychiatric/subexcel.php:13)发送Writer.php 在第 70 行

警告:无法修改标头信息 - 标头已由 /home/content/58/9508458/html/psychiatric/phpxls/ 中的(输出开始于 /home/content/58/9508458/html/psychiatric/subexcel.php:13)发送Writer.php 在第 71 行

4

2 回答 2

4

现在,您在尝试查询数据库后选择了数据库:

$numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");

mysql_select_db("brainoidultrafb", $link);

那是行不通的。只需交换顺序,以便您选择数据库,然后查询它:

mysql_select_db("brainoidultrafb", $link);

$numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");

至于你的第二个问题,你正在覆盖$sheet1循环的每次迭代。改为这样做:

$sheet1 = array(
    array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,)
);

while($row = mysql_fetch_array($numrow12)) {
    $sheet1[] = array($row['student_id'], $row['email_id'], $row['diagnosis'], $row['tstamp'],);
}
于 2012-07-05T17:32:21.160 回答
0

对于您的第二个问题,请使用: $sheet1[] =

将元素附加到 $sheet1 数组。您当前正在循环的每次迭代后替换它。

您可能还希望$sheet1在 while 循环之外进行初始化以避免警告。$sheet1 = array();

于 2012-07-05T17:42:00.130 回答