homepage Welcome to WebmasterWorld Guest from 54.205.105.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP with Excel
dbarasuk

5+ Year Member



 
Msg#: 4305771 posted 7:48 am on Apr 29, 2011 (gmt 0)

Hi all,

I am using the next code to change database data into excel sheet.


<?php
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}


// file name for download
$filename = "OBR_Recettes" . time() . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");


$flag = false;


$result = mysqli_query($conn, $sql ) or die('Query failed !: '.mysqli_error($conn));



while($row = mysqli_fetch_assoc($result)) {

if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\n";
}
?>

$result is computed from $sql query.

Problem of this, it is giving a warning when file opens with Excel 2007 and i cannot customize it.

Now, i saw a very powerful Excel Class, called PHPExcel from www.codeplex.com. This one is really
very powerful. But the major drawback, i saw no usage example or tutorial for this. Does someone know how to start off in using this PHPExcel class? (Example or tuto)

Thanks in advance.

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4305771 posted 2:57 pm on Apr 29, 2011 (gmt 0)

Try to make is just a regular ASCII text file, and open it in excel when you download it. I believe this should prompt you to specify a separator character for the import. a CSV file is not in excel format by itself.

explorador

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4305771 posted 3:30 pm on Apr 29, 2011 (gmt 0)

x2 to eelixduppy

you can print plain text and html code (tables) to excel, just print out the right headers first and then go. The file will be downloaded and identified as excel, it will open the app and it will recognize the format and show it to you as excel file. I've done this with both ms office on windows and open office on linux with no problem.

dbarasuk

5+ Year Member



 
Msg#: 4305771 posted 3:36 pm on Apr 29, 2011 (gmt 0)

So no one here has already used the class i mentionned?

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4305771 posted 3:12 pm on May 2, 2011 (gmt 0)

Not me, but I tend to write all my own code and classes ... which I think you will find more often than not with others in this forum.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved