Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySql export to XLS (by php) problem

Posted on 2009-05-08
6
Medium Priority
?
1,088 Views
Last Modified: 2012-05-06
Hellow all,

I have this script for exporting a mysql query to excel. This script is downloaden from: http://www.fundisom.com/phparadise/php/databases/mySQL_to_excel

One of the pieces of data that are exporter are id numbers. These are random numbers of 10 digits. But when I make an export all the id numbers starting with a zero are stripped of this zero. So numbers like '9876569033' remain like this, but numers like '0345606541'  become '345606541' in the exported xls file.
// piece of code that exports as xls
 
}else if(isset($_POST['filetype']) && ($_POST['filetype'] == 'xls')){
	/*	FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
	//create title with timestamp:
	if ($Use_Title == 1)
	{
		echo("$title\n");
	}
	//define separator (defines columns in excel & 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))
	{
		//set_time_limit(60); // HaRa
		$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);
		//following fix suggested by Josue (thanks, Josue!)
		//this corrects output in excel when table fields contain \n or \r
		//these two characters are now replaced with a space
		$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
		$schema_insert .= "\t";
		print(trim($schema_insert));
		print "\n";
	}
}else ...

Open in new window

0
Comment
Question by:wal_toor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:ycTIN
ID: 24333876
easily to solve this problem

just need add the ' into the value front
'0345606541

you can try the different in the MS Excel
when you type 0345606541 and enter, excel will auto convert to 345606541
type '0345606541, excel will show 0345606541
0
 
LVL 8

Author Comment

by:wal_toor
ID: 24333941
Hi ycTIN,

I have tried this, and works when directly working in excel, but the mysql export shows '0345606541 and will not be automatically converted.

greetz,
walter
0
 
LVL 7

Accepted Solution

by:
ycTIN earned 2000 total points
ID: 24334105
Hi wal_toor,

please try this code

<?php
define('COLUMNS',10);
define('ROWS',10);
 
//filename
$filename = 'yctin.xls';
 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=$filename ");
header("Content-Transfer-Encoding: binary ");
?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
 
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<body link=blue vlink=purple>
<table>
 
<?php
//content start
$row = ROWS;
while ($row--) {
	$col = COLUMNS;
	echo '<tr>';
	while ($col--) {
		$value = sprintf("%010d",rand(1,9999999999));
		echo "<td x:str=\"'$value\">";	
		echo $value;
		echo '</td>';
	}
	echo '</tr>';
}
//content end
?>
</table>
</body>
</html>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Expert Comment

by:ycTIN
ID: 24334115
the '  is not required in this solution

echo "<td x:str=\"$value\">";	

Open in new window

0
 
LVL 8

Author Comment

by:wal_toor
ID: 24334301
Yes, this works. I have implemented this in the existing script and works perfectly. Thanks a lot!

greetw,
walter
0
 
LVL 8

Author Closing Comment

by:wal_toor
ID: 31579338
Works great! Thankz
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question