开发者

php export html also with data

I have the following code. It export the data to excel if do not add any html tag in page. When i add the html tag in the page.It also export the tag as well. I would like to export only data. I will have the drop down and text box for user input. Code are as under

<?php ?>
<html>
<head>
<h1> this is heading will display when page load</h1>
开发者_开发问答<head>
<body>
<p> user inputs </p>
<?PHP 

 *** sql connection string omit....***

 $filename = "website_data_" . date('Ymd') . ".xls"; 
  header("Content-Type: application/vnd.ms-excel");
 header("Content-Disposition: attachment; filename=\"" . $filename . "\"");

  $flag = false; 
 $result = mysql_query("SELECT trandate,price FROM price") or die('Query failed!'); 
 while(false !== ($row = mysql_fetch_assoc($result))) { 
 if(!$flag) {
 // display field/column names as first row 
 echo implode("\t", array_keys($row)) . "\n"; 
 $flag = true; 
 } 
 echo implode("\t", array_values($row)) . "\n";
 } 

 ?>
 </body>
 </html>


Error of excel gone after enter the following on client machine.

The easiest way to solve this problem, is to insert the following registry key. This will suppress the notification:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security]
“ExtensionHardening”=dword:00000000

The step-by-step instructions are as follows:

  1. Click Start, click Run, type regedit.exe and press ENTER. This will open your Registry
  2. Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
  3. Right click in the right window and choose New -> DWORD
  4. In the Name field, type “ExtensionHardening“ (without the quotes)
  5. Verify that the data has the value “0″

I advice the steps above to solve the problem, despite the fact that it is quick and dirty.


You can save your data in file and give simple redirect

<?php ?>
<html>
<head>
<h1> this is heading will display when page load</h1>
<head>
<body>
<p> user inputs </p>
<?PHP 

 *** sql connection string omit....***

 $filename = "website_data_" . date('Ymd') . ".xls"; 
 $filedata = '';

  $flag = false; 
 $result = mysql_query("SELECT trandate,price FROM price") or die('Query failed!'); 
 while(false !== ($row = mysql_fetch_assoc($result))) { 
 if(!$flag) {
 // display field/column names as first row 
 $filedata .= implode("\t", array_keys($row)) . "\n"; 
 $flag = true; 
 } 
 $filedata .= implode("\t", array_values($row)) . "\n";
 } 

 file_put_contents($filename, $filedata);

 echo '<meta http-equiv="refresh" content="0;url=' . $filename . '"';

 ?>
 </body>
 </html>


Here is a simple solution:

Place the PHP code in another file called "excel.php" Add this to the top of the file:

<?php
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename=excel.xls');

Then add a link from your existing file to this file:

<a href="excel.php">Download</a>


try this script and that's working for me

<form action="itemexcel.php" method="post" 
onsubmit='$("#datatodisplay").val( $("<div>")
.append( $("#ReportTable").eq(0).clone() ).html() )'>

<table id="ReportTable" width="781" border="2"> Or <div id id="ReportTable">
<tr>
<td><input type="hidden" id="datatodisplay" name="datatodisplay"></td>
<td><input class="bg" type="submit" value="Export To Excel"></td>
</tr></table></div>

<input type="hidden" id="datatodisplay" name="datatodisplay">
<input class="bg" type="submit" value="Export To Excel">

itemexcel Page

<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=guestexcel.xls');
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜