Page 1 of 1

export to excel

Posted: Wed Jun 17, 2009 6:07 pm
by MrTripi
was just wondering if Joomla 1.5 provides any classes for exporting tables to excel.

Re: export to excel

Posted: Wed Jun 17, 2009 6:15 pm
by jeffchannell
Exporting your database to excel? Your host should have phpMyAdmin installed, and that has Excel export capability built in. :)

Re: export to excel

Posted: Wed Jun 17, 2009 6:20 pm
by MrTripi
True, phpmyadmin would do the trick. Not every user is comfortable touching their database though.

The component displays statistical data on one of my plugins.

I'd like to include a link for exporting the resulting table into excel so that we can generate graphs,etc.

Re: export to excel

Posted: Wed Jun 17, 2009 6:24 pm
by jeffchannell
Ah, I see... try using a content-type header of "csv" and use comma-delimited text. Excel understands this (albeit with no formulas, etc, AFAIK).

Re: export to excel

Posted: Wed Jun 17, 2009 6:39 pm
by MrTripi
soooo...

have the component write a .php file in the temp directory then provide a link to it?

Code: Select all

<?php header('Content-type: application/csv');?>
<table>
<tr><td>

blahblahblah

</td><tr>
</table>


Re: export to excel

Posted: Wed Jun 17, 2009 7:12 pm
by radiant_tech
You could add a php class that handles data exports to your component. For instance, I've used IAM_CSVDump to automate data exports to CSV files. And similarly, I have used ExcelWriterXML to build complex Excel files in the back-end.

Re: export to excel

Posted: Wed Jun 17, 2009 7:23 pm
by MrTripi
nvm, I just wrote an xml file and threw in a few tables with notepad. it seemed to do the trick ;)

Re: export to excel

Posted: Wed Jun 17, 2009 7:31 pm
by MrTripi

Code: Select all

srand ((double) microtime( )*1000000);
$random_number = rand( );

$myFile = '../tmp/'.$random_number.'.xls';

$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $output;
fwrite($fh, $stringData);
fclose($fh);
works :D

Re: export to excel

Posted: Sat Jun 20, 2009 7:05 pm
by zak_the_man
Or you could use the SQL 2 Excel component:

http://extensions.joomla.org/extensions ... 13/details

Re: export to excel

Posted: Mon Jun 22, 2009 10:41 am
by dam-man
Or in your own component you can have this function:

Code: Select all

	###############################################################
## This function will generate a report of invoices XLS Format
	
	function generateReport(){
		
		global $mainframe;
		
		## Make DB connections
		$db    = JFactory::getDBO();
		
		$sql = 'Your Query Goes Here..';
		
		$db->setQuery($sql);
		$rows = $db->loadAssocList();
		
		## If the query doesn't work..
		if (!$db->query() ){
			echo "<script>alert('Please report your problem.');
			window.history.go(-1);</script>\n";		 
		}	
		
		## Empty data vars
		$data = "" ;
		## We need tabbed data
		$sep = "\t"; 
		
		$fields = (array_keys($rows[0]));
		
		## Count all fields(will be the collumns
		$columns = count($fields);
		## Put the name of all fields to $out.  
		for ($i = 0; $i < $columns; $i++) {
		  $data .= $fields[$i].$sep;
		}
		
		$data .= "\n";
		
		## Counting rows and push them into a for loop
		for($k=0; $k < count( $rows ); $k++) {
			$row = $rows[$k];
			$line = '';
			
			## Now replace several things for MS Excel
			foreach ($row as $value) {
			  $value = str_replace('"', '""', $value);
			  $line .= '"' . $value . '"' . "\t";
			}
			$data .= trim($line)."\n";
		}
		
		$data = str_replace("\r","",$data);
		
		## If count rows is nothing show o records.
		if (count( $rows ) == 0) {
		  $data .= "\n(0) Records Found!\n";
		}
		
		## Push the report now!
		$this->name = 'export-invoices';
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=".$this->name.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		header("Lacation: excel.htm?id=yes");
		print $data ;
		die();	
	}


Re: export to excel

Posted: Tue Nov 10, 2009 12:13 am
by imNotScott
This was exactly what I needed, thanks!

Re: export to excel

Posted: Thu Nov 18, 2010 2:29 pm
by ericgibert
Dear Dan-man,

Is there a typo at the line:

header("Lacation: excel.htm?id=yes");

Lacation or Location?

Anyway, I have implemented that script and it works fine (thank you!) but for one caveat: I associated it behind a button and once completed, whatever I do, the page behaves as if I click on the button again! i.e. always asking me to "open or save the XL file".

I tried to replace the die by return or by setRedirect without much success.
Any better way to cleanly quit?

Thank you

Eric

Re: export to excel

Posted: Tue Nov 23, 2010 10:10 pm
by rajshivnesh
instead of this line:

$data = str_replace("\r","",$data);

use the below line of code to go to new line:

$data = str_replace("\r\n","",$data);

Re: export to excel

Posted: Thu Apr 28, 2011 10:12 am
by fa89
thank you very much dam-man your function helped me a lot.
it's very useful :)

Re: export to excel

Posted: Thu Apr 28, 2011 11:01 am
by fa89
hi,
I just want to know if there is a possibility to change the color of the first line that contains the names of fields and the color of the first column in the excel file generated.
i use the function proposed by dam-man .

Re: export to excel

Posted: Sat Oct 08, 2011 8:42 pm
by tarantino
dam-man wrote:Or in your own component you can have this function:

Code: Select all

	###############################################################
## This function will generate a report of invoices XLS Format
	
	function generateReport(){
		
		global $mainframe;
		
		## Make DB connections
		$db    = JFactory::getDBO();
		
		$sql = 'Your Query Goes Here..';
		
		$db->setQuery($sql);
		$rows = $db->loadAssocList();
		
		## If the query doesn't work..
		if (!$db->query() ){
			echo "<script>alert('Please report your problem.');
			window.history.go(-1);</script>\n";		 
		}	
		
		## Empty data vars
		$data = "" ;
		## We need tabbed data
		$sep = "\t"; 
		
		$fields = (array_keys($rows[0]));
		
		## Count all fields(will be the collumns
		$columns = count($fields);
		## Put the name of all fields to $out.  
		for ($i = 0; $i < $columns; $i++) {
		  $data .= $fields[$i].$sep;
		}
		
		$data .= "\n";
		
		## Counting rows and push them into a for loop
		for($k=0; $k < count( $rows ); $k++) {
			$row = $rows[$k];
			$line = '';
			
			## Now replace several things for MS Excel
			foreach ($row as $value) {
			  $value = str_replace('"', '""', $value);
			  $line .= '"' . $value . '"' . "\t";
			}
			$data .= trim($line)."\n";
		}
		
		$data = str_replace("\r","",$data);
		
		## If count rows is nothing show o records.
		if (count( $rows ) == 0) {
		  $data .= "\n(0) Records Found!\n";
		}
		
		## Push the report now!
		$this->name = 'export-invoices';
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=".$this->name.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		header("Lacation: excel.htm?id=yes");
		print $data ;
		die();	
	}

thank you for this but for the unicode like arabian languages the exported xls file is not true words?like this : ط¹ط¨ط§ط³غŒ
i changes the code to :
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
but again not working ?

any solutions?

Re: export to excel

Posted: Thu Feb 09, 2012 5:59 pm
by swadhwa
Hi All

I am trying to using the csv download script in my controller. I have tried by using headers as well php function defined above. It is not working even for simple case.

Here is my function:
function workdownload(){
//global $mainframe;
$data='test1,test2,test3,';
//$model = $this->getModel('listworkdownload');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.xls");
header("Pragma: no-cache");
header("Expires: 0");
header("Lacation: excel.htm?id=yes");
print $data ;
//$mainframe->close();
}

Please help me.

Looking Forward.

Regards
S.Wadhwa

Re: export to excel

Posted: Sat Aug 11, 2012 8:01 pm
by sunil_007
Hi there, I have use the function given by dan-man, it works and export the file in .xls format. but when I try to open the file.. it gives this error =
"the file you are trying to open is in a different format than specified by the file extension xls ......"

How to avoid this error, for smooth opening of file... what to write in the code...?

Regards
Sunil S.

Re: export to excel

Posted: Sun Aug 12, 2012 7:37 am
by Chris Davenport
What is actually being exported here is CSV and not XLS. Perhaps try changing

Code: Select all

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$this->name.".xls");
to

Code: Select all

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$this->name.".csv");
Chris.

Re: export to excel

Posted: Fri Aug 31, 2012 12:19 pm
by ra1
this script is not working for me bcz in my component i hv already used one sql statment and i dont know how to use 2 sql statment in one component .. i am working with virtuemart and i want to store data in both database and excel file.. so plz help me,........ thnxxx