使用PHP从MYSQL表生成Excel

使用PHP从MYSQL表生成Excel,php,mysql,pear,Php,Mysql,Pear,我的桌子如下所示 StudentId | Subjects | Marks ---------------------------- S001 | sub1 | 99 S002 | sub2 | 80 S003 | sub3 | 89 S004 | sub1 | 75 . . . s100 | sub3 | 60 我需要在excel中生成一个输出。到目前为止,我知道

我的桌子如下所示

StudentId | Subjects | Marks
----------------------------
   S001   | sub1     |  99
   S002   | sub2     |  80
   S003   | sub3     |  89
   S004   | sub1     |  75
     .
     .
     .
   s100   | sub3     |  60
我需要在excel中生成一个输出。到目前为止,我知道如何使用php生成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中的查询从表中获取值。现在需要像表中那样打印输出。有人能帮我吗?

您可以使用
addWorksheet()添加工作表。

更新

您可以像这样添加值

//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++;
}

这不是您的方法,但您可以使用此方法在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";
    ?>

除了我的代码之外,还有其他方法吗?感谢php pear excel的帮助,你指的是软件包吗?@Bjoern..当然,我只使用电子表格\u excel\u编写器。@feroz…添加工作表并生成excel以及我所知道的一切..问题是如何从表中获取值并传递到$WORK($row,$col,?)…提前谢谢..可以这样更新。但我有100多行。那样的话就不可能了。我对此不确定。我会检查这个…非常感谢@feroz…您将您的值转换为数组,然后您只需添加values@pinu...it正在工作,但它只生成最后一行(单行)。我无法获取所有行。提前谢谢。我想您正在关闭while循环中的工作表。while循环完成后,在结束括号后关闭工作簿,而不是关闭while循环。在While循环中,所有行都已创建,因此我认为您正在关闭循环中的工作簿,因此它只显示一条记录,即最后一条记录。它看起来很有用,同时看起来很复杂。我将在将来使用这些方法…谢谢srinath。。
 <?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";
    ?>
$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++;
}