【问题标题】:Excel generation from MYSQL table using PHP使用 PHP 从 MYSQL 表生成 Excel
【发布时间】:2014-05-01 09:21:22
【问题描述】:

我的表是这样的

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 中获取的查询从表中获取值。现在需要像在表中一样打印输出。谁能帮帮我?

【问题讨论】:

  • 除了我的代码,还有其他方法可以得到这个吗?谢谢提前
  • 用php pear excel,你指的是Spreadsheet_Excel_Writer包吗?
  • @Bjoern..当然,我只使用 Spreadsheet_Excel_Writer。

标签: php mysql pear


【解决方案1】:

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

【讨论】:

  • @feroz...添加工作表并生成到 excel 中,我只知道..问题是如何从表中获取值并传递到 $worksheet($row,$col,???? ???)..谢谢提前..
  • 可以这样更新。但我有超过 100 行。在那种情况下是不可能的。我不确定这一点。我会检查这个...非常感谢@feroz....
  • 您将值转换为数组,然后您可以简单地添加值
【解决方案2】:

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

【讨论】:

  • 它看起来很有用,但看起来很复杂。我将在以后的使用中使用这些方法...谢谢 srinath..
【解决方案3】:
$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++;
}

【讨论】:

  • @pinu...它正在工作,但它只生成最后一行(单行)。我无法获得所有行..提前谢谢..
  • 我认为您正在关闭 while 循环内的工作表。一旦 while 循环完成,然后在右括号之后关闭工作簿,而不是在 while 循环内。在 While 循环中创建了所有行,因此我认为您正在关闭循环内的工作簿,因此它仅显示一条记录,即最后一条记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
相关资源
最近更新 更多