使用PHP从MYSQL表生成Excel

时间:2014-03-24 06:17:24

标签: php mysql pear

我的表格如下

StudentId | Subjects | Marks
----------------------------
   S001   | sub1     |  99
   S002   | sub2     |  80
   S003   | sub3     |  89
   S004   | sub1     |  75
     .
     .
     .
   s100   | sub3     |  60

我需要在excel中生成输出。到目前为止,我知道如何使用php pear生成excel。现在的问题是如何获取这些值并将其传递给工作表。

我的代码,

while($records=mysql_fetch.....)
{
$sid[$records['StudentId']]=$records['StudentId'];
$sub[$records['Subjects']]=$records['Subjects'];
$mark[$records['Marks']]=$records['Marks'];
}
$row=2;
$col=0;
$worksheet->write($row,$col,?,$format);
...
...

我不知道要传递什么值?地点。 注意:没有操纵和所有。只需从$ records中获取查询中的值。现在需要像表中那样打印输出。有谁可以帮助我?

3 个答案:

答案 0 :(得分:0)

您可以使用addWorksheet()

添加工作表
$worksheet =& $workbook->addWorksheet('worksheet name');
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
} 
//$workbook->close();

<强>更新

你可以添加像这样的值

//for example you have an array like this

$rowCount=0;
$data = array(
  array('', 'Math', 'Literature', 'Science'),
  array('John', 24, 54, 38),
  array('Mark', 67, 22, 57),
  array('Tim', 69, 32, 58),
  array('Sarah', 81, 78, 68),
  array('Susan', 16, 44, 38),
);

foreach ($data as $row) {
  foreach ($row as $key => $value) {
    $sheet->write($rowCount, $key, $value);    
  }  
  $rowCount++;
}

答案 1 :(得分:0)

这不是您的方法,但您可以使用此方法在Excel中生成输出

 <?php 
    mysql_connect("localhost", "YOUR_MYSQL_USERNAME", "YOUR_MY_SQL_PASSWORD") or die(mysql_error());
    mysql_select_db("DATABASE_NAME") or die(mysql_error());

    $count = 0;

    $sqlquery = "select * from TABLE_NAME" ;
    $result = mysql_query($sqlquery) or die(mysql_error());  
    $count = mysql_num_fields($result);

    for ($i = 0; $i < $count; $i++) {
        $header .= mysql_field_name($result, $i)."\t";
    }

    while($row = mysql_fetch_row($result))  {
      $line = '';
      foreach($row as $value)       {
        if(!isset($value) || $value == "")  {
          $value = "\t";
        }   else  {
    # important to escape any quotes to preserve them in the data.
          $value = str_replace('"', '""', $value);
    # needed to encapsulate data in quotes because some data might be multi line.
    # the good news is that numbers remain numbers in Excel even though quoted.
          $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
      }
      $data .= trim($line)."\n";
    }
    # this line is needed because returns embedded in the data have "\r"
    # and this looks like a "box character" in Excel
      $data = str_replace("\r", "", $data);


    # Nice to let someone know that the search came up empty.
    # Otherwise only the column name headers will be output to Excel.
    if ($data == "") {
      $data = "\nno matching records found\n";
    }

    $count = mysql_num_fields($result);



    # This line will stream the file to the user rather than spray it across the screen
     header("Content-type: application/octet-stream");
    //header("Content-type: text/plain");

    # replace excelfile.xls with whatever you want the filename to default to
    header("Content-Disposition: attachment; filename=excelfile.xls");

    header("Pragma: no-cache");
    header("Expires: 0");

    //echo $header."\n".$data;
    echo $header."\n".$data."\n";
    ?>

答案 2 :(得分:0)

$row=2;

while($records=mysql_fetch.....)
{
   $col = 0;
   $sid[$records['StudentId']]=$records['StudentId'];
   $sub[$records['Subjects']]=$records['Subjects'];
   $mark[$records['Marks']]=$records['Marks'];
   $worksheet->write($row,$col++,$records['StudentId'],$format);
   $worksheet->write($row,$col++,$records['Subjects'],$format);
   $worksheet->write($row,$col++,$records['Marks'],$format);
   $row++;
}