gpt4 book ai didi

php - 通过php和html为mysql数据库创建excel文件

转载 作者:行者123 更新时间:2023-11-29 22:48:11 25 4
gpt4 key购买 nike

我正在尝试创建一个 html 文件,它将有一个下载按钮。我找到了一个将mysql数据库导出到excel文件的php代码。

PHP 代码:

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
/***** EDIT BELOW LINES *****/
$DB_Server = "localhost"; // MySQL Server
$DB_Username = "admin"; // MySQL Username
$DB_Password = ""; // MySQL Password
$DB_DBName = "login"; // MySQL Database Name
$DB_TBLName = "logintable"; // MySQL Table Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name

/***** DO NOT EDIT BELOW LINES *****/
// Create MySQL connection
$sql = "Select * from $DB_TBLName";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());

// Header info settings
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");

/***** Start of Formatting for Excel *****/
// Define separator (defines columns in excel &amp; tabs in word)
$sep = "\t"; // tabbed character

// Start of printing column names as names of MySQL fields
for ($i = 0; $i<mysql_num_fields($result); $i++) {
echo mysql_field_name($result, $i) . "\t";
}
print("\n");
// End of printing column names

// Start while loop to get data
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result); $j++)
{
if(!isset($row[$j])) {
$schema_insert .= "NULL".$sep;
}
elseif ($row[$j] != "") {
$schema_insert .= "$row[$j]".$sep;
}
else {
$schema_insert .= "".$sep;
}
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>
</body></html>

现在,当我在浏览器中打开文件时,文件的结果是:

username password flag counter admin pass1 1 1 admin2 pass2 0 2

它不会创建任何 Excel 文件(或者我找不到它),我想从 html 文件调用此 php,该文件将有一个 Excel 文件的下载按钮。

最佳答案

您可以尝试这个脚本:https://github.com/elidickinson/php-export-data
它易于使用并使用 header 创建供下载的文件

示例:

// When executed in a browser, this script will prompt for download 
// of 'test.xls' which can then be opened by Excel or OpenOffice.

require 'php-export-data.class.php';

// 'browser' tells the library to stream the data directly to the browser.
// other options are 'file' or 'string'
// 'test.xls' is the filename that the browser will use when attempting to
// save the download
$exporter = new ExportDataExcel('browser', 'test.xls');

$exporter->initialize(); // starts streaming data to web browser

// pass addRow() an array and it converts it to Excel XML format and sends
// it to the browser
$exporter->addRow(array("This", "is", "a", "test"));
$exporter->addRow(array(1, 2, 3, "123-456-7890"));

// doesn't care how many columns you give it
$exporter->addRow(array("foo"));

$exporter->finalize(); // writes the footer, flushes remaining data to browser.

exit(); // all done

?>

关于php - 通过php和html为mysql数据库创建excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com