Hi everyone,

I have a code which converts a html page into excel file. The following code has been working well so far, but Im having a problem of converting the html page into excel file after I had changed the localhost to another server. It's not able to generate excel file!

May I know what's wrong with my code? Thanks!:)

<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
//-->
</script>

<?php
include('ADC_project_excel1.php');
$result = mysql_query("SELECT * FROM NewProject");
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}


$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\nno matching records found\n";
}
//header("Content-Type: application/vnd.ms-excel; name='excel'");

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data; 

//print "done";

?>

Recommended Answers

All 7 Replies

If you look at the example you will see that you just need to define the input, the output file name and then include the module.

$dfw_data = $data;
$dfw_filename = "fileout.xls"; 
include ".../desktop_file_write.php";

Your input to this (your $data variable) needs to be a properly formatted html table. Again, see the example. You don't want/need the \t or \n at the end of a line. The line should end with a </tr>.

Yea. It's working. But the same problem happen.

The code is working if i connect to my localhost server. However, once i changed to another server, it's now working. Instead of giving me an excel file, it displays all the result on the html page.

Yea. It's working. But the same problem happen.

The code is working if i connect to my localhost server. However, once i changed to another server, it's now working. Instead of giving me an excel file, it displays all the result on the html page.

Did you change the file path too? I have not looked at the module though!

Hi guys, it's weird. I had managed to get 3 codes (different styles) which convert a html page to excel files. All the three codes work if my server is connected to local host, but only one of them work if i connect to another server.

It's really a bizarre.

Hi guys, it's weird. I had managed to get 3 codes (different styles) which convert a html page to excel files. All the three codes work if my server is connected to local host, but only one of them work if i connect to another server.

It's really a bizarre.

May be they depend on special library that is not installed on your server. Compare output of phpinfo() on both servers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.