簡體   English   中英

使用PHP從MYSQL表生成Excel

[英]Excel generation from MYSQL table using PHP

我的桌子如下

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中獲取的查詢從表中獲取值。 現在需要像表中那樣打印輸出。 有人可以幫我嗎?

您可以使用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++;
}

這不是您的方法,但是您可以在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";
    ?>
$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++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM