下载后从php显示错误导出excel文件并打开文件:
您尝试打开的文件格式不同.xls
我想下载表字段并填充数据并使用php代码再次上传到数据库。
我尝试了很多应用程序类型。我只想给用户一个文件,其结构包含数据库字段名称,用户将添加数据,并将使用php
再次上传到数据库。
<?php
`session`_start();
include 'db.php';
/*******EDIT LINES 3-8*******/
$DB_TBLName = $_POST["tablename"]; //MySQL Table Name
$filename = $_POST["excelfilename"]; //File Name
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/
//create MySQL connection
$sql = "SHOW COLUMNS FROM $DB_TBLName";
//execute query
$result = $conn->query($sql);
$file_ending = "xls";
//header info for browser
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename.xls");
header ('Content-Transfer-Encoding: binary');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
/*header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");*/
/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
while($row = $result->fetch_assoc())
{
echo $row['Field'] . "\t";
}
print("\n");
//end of printing column names
//start while loop to get data
while($row = $result->fetch_assoc())
{
$schema_insert = "";
for($j=0; $j<$result->field_count;$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";
}
?>