|
|
|
[แจก Code PHP Export to Excel] Example แบบบ้านๆง่ายๆ ส่งค่าผ่าน $_POST |
|
|
|
|
|
|
|
Code.php test_PHP_2_excel.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL To CSV</title>
</head>
<body>
<?php
$search=$_POST["search"];
$objConnect = mysql_connect("localhost","root","44@44") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE CustomerID ='$search' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form action="#" method="post" enctype="multipart/form-data">
<input type="search" name="search"> <input type="submit" name="ok" placeholder="search for Export to Excel">
</form>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
<td><?php echo $objResult["Name"];?></td>
<td><?php echo $objResult["Email"];?></td>
<td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
<td align="right"><?php echo $objResult["Budget"];?></td>
<td align="right"><?php echo $objResult["Used"];?></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close($objConnect);
?>
<div align="center"><br>
<input name="btnExport" type="button" value="Export" onClick="JavaScript:window.location='phpCSVMySQLExport.php?CustomerID=<?php echo $search; ?>';">
</div>
</body>
</html>
code.php phpCSVMySQLExport.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL To CSV</title>
</head>
<body>
<?php
$search=$_POST["search"];
$filName = "customer.csv";
$objWrite = fopen("customer.csv", "w");
$objConnect = mysql_connect("localhost","root","44@44") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE CustomerID ='$_GET[CustomerID]' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
fwrite($objWrite, "\"$objResult[CustomerID]\",\"$objResult[Name]\",\"$objResult[Email]\",");
fwrite($objWrite, "\"$objResult[CountryCode]\",\"$objResult[Budget]\",\"$objResult[Used]\" \n");
}
fclose($objWrite);
echo "<br>Generate CSV Done.<br><a href=$filName>Download</a>";
?>
</table>
</body>
</html>
PHP Convert/Export to CSV files ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP เขียนไฟล์ลง CSV เปิดด้วยโปรแกรม Microsoft Excel
https://www.thaicreate.com/php/php-convert-export-mysql-to-csv.html
Tag : PHP, Ms Access, HTML, CSS, Apache, XAMPP
|
|
|
|
|
|
Date :
2017-08-28 10:23:04 |
By :
Bouasavanh HararRock |
View :
4195 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2017-08-28 11:23:01 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เข้าใจแล้วครับ วันหลังว่างๆ จะ Upcode
|
|
|
|
|
Date :
2017-08-28 13:59:51 |
By :
Bouasavanh HararRock |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|