简体   繁体   English

PHP 导出到 MYSQL 中的 excel

[英]PHP Export to excel from MYSQL

I'm trying to get my MySQL data to Excel file, but I'm having problems with Excel cells.我正在尝试将我的 MySQL 数据转换为 Excel 文件,但我遇到了 Excel 单元格的问题。 All my text goes to one cell, I would like to have each row value in separate Excel cell.我的所有文本都转到一个单元格,我希望将每一行值放在单独的 Excel 单元格中。 Here is my code:这是我的代码:

 <html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta charset="UTF-8"> <title>Daily Log</title> </head> <body> <?php ini_set('display_errors', 1); error_reporting(~0); $strKeyword = null; if (isset($_POST["txtKeyword"])) { $strKeyword = $_POST["txtKeyword"]; } ?> <form name="frmSearch" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>"> <table> <tr> <th>Date <input name="txtKeyword" type="month" id="txtKeyword" value="<?php echo $strKeyword;?>"> <input type="submit" value="Search"></th> <input type="submit" value="Export"></th> </tr> <tr> <th><label>Date</label></th> <th><label>Requested</label></th> <th><label>Requested Time</label></th> <th><label>Location</label></th> <th><label>Description</label></th> <th><label>Order By</label></th> <th><label>Completed by</label></th> <th><label>Completed Time</label></th> </tr> <tr><!--/*DESC ASP*/--> <?php include('config.php'); $filename = "excelfilename"; $strsql = "select * from log WHERE dateorder LIKE '%".$strKeyword."%'"; $result = mysqli_query($objConnect, $strsql); $strExcelFileName="Member-All.xls"; header("Content-Type: application/x-msexcel; name=\\"$strExcelFileName\\""); header("Content-Disposition: inline; filename=\\"$strExcelFileName\\""); header("Pragma:no-cache"); while ($rs = mysqli_fetch_array($result,MYSQLI_ASSOC)) { ?> <td><?php echo $rs['dateorder'] ?></td> <td><?php echo $rs['request'] ?></td> <td><?php echo date("H:i", strtotime($rs['requesttime'])) ?></td> <td><?php echo $rs['location'] ?></td> <td><?php echo $rs['description'] ?></td> <td><?php echo $rs['orderby'] ?></td> <td><?php echo $rs['completed'] ?></td> <td><?php echo date("H:i", strtotime($rs['completedtime'])) ?></td> </tr> <?php } ?> </table> </form> <script> window.onbeforeunload = function(){return false;}; setTimeout(function(){window.close();}, 10000); </script> </body> </html>

I need Step 1 search button step 2 export button我需要第 1 步搜索按钮第 2 步导出按钮

Consider separating the HTML output and Excel export using the same PHP array fetched from MySQL query.考虑使用从 MySQL 查询中获取的相同 PHP 数组来分离 HTML 输出和 Excel 导出。 Integrate below pieces into your code.将以下部分集成到您的代码中。

Specifically, Excel requires tab-delimited text to map to cells with carriage and line breaks.具体来说,Excel 需要制表符分隔的文本以映射到带有回车符和换行符的单元格。

Database Fetch数据库提取

...same db config...

$data = []
while ($rs = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $data[] = $row; 
}

HTML Output HTML 输出

foreach ($data as $row) { ?>
 <tr>
     <td><?php echo $row['dateorder'] ?></td>
     <td><?php echo $row['request'] ?></td>
     <td><?php echo date("H:i", strtotime($row['requesttime'])) ?></td>
     <td><?php echo $row['location'] ?></td>
     <td><?php echo $row['description'] ?></td>
     <td><?php echo $row['orderby'] ?></td>
     <td><?php echo $row['completed'] ?></td>
     <td><?php echo date("H:i", strtotime($row['completedtime'])) ?></td>
 </tr>
<?php } ?>

Excel Export Excel 导出

$strExcelFileName="Member-All.xls";
header("Content-Type: application/x-msexcel; name=\"$strExcelFileName\"");
header("Content-Disposition: attachment; filename=\"$strExcelFileName\"");
header("Pragma:no-cache");

$i = 1;
foreach ($data as $row) {
   if ($i == 1) {
      // COLUMN HEADERS
      echo implode("\t", array_keys($row)) . "\r\n";
   }
   // DATA ROWS
   echo implode("\t", array_values($row)) . "\r\n";
   $i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM