Export to CSV or XML extension
Is there an extension or that allows me to quickly setup an export to CSV or XML?
Is there an extension or that allows me to quickly setup an export to CSV or XML?
We don't offer one, but it is easy to build.
Just build an HTML page that displays your content comma separated just like you would any other results page. You will have to view source on the page to make sure it looks right. Commas after each value and new lines for each row.
Once you have it formatted properly you can add a header to the top of the page to make it download like a csv file instead of display in the browser like:
<?php
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="csvfilename.csv";');
?>
Any clue on fixing this? The recordset does exist
<?php
//Export to Excel Server Behavior
if (isset($_GET['Export'])&&($_GET['Export']=="Export Spreadsheet")){
$delim="";
$delim_replace="";
if($delim==""){
$lang=(strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],",")===false)?$_SERVER['HTTP_ACCEPT_LANGUAGE']:substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],","));
$semi_array=array("af","zh-hk","zh-mo","zh-cn","zh-sg","zh-tw","fr-ch","de-li","de-ch","it-ch","ja","ko","es-do","es-sv","es-gt","es-hn","es-mx","es-ni","es-pa","es-pe","es-pr","sw");
$delim=(in_array($lang,$semi_array) || substr_count($lang,"en")>0)?",":";";
}
$output="";
$output="1 = RecruitSavvy 2 = CareerSoft 3 = Warm Calling 4 = Unclaimed Property that 5 = Resume Search Only 6 = Career Changers 7 = Brokerage\n\n";
$include_hdr="1";
if($include_hdr=="1"){
$totalColumns_WADAcustomers=mysql_num_fields($WADAcustomers);
for ($x=0; $x<$totalColumns_WADAcustomers; $x++) {
if($x==$totalColumns_WADAcustomers-1){$comma="";}else{$comma=$delim;}
$output = $output.(ereg_replace("_", " ",mysql_field_name($WADAcustomers, $x))).$comma;
}
$output = $output."\r\n";
}
do{$fixcomma=array();
foreach($row_WADAcustomers as $r){array_push($fixcomma,ereg_replace($delim,$delim_replace,$r));}
$line = join($delim,$fixcomma);
$line=ereg_replace("\r\n", " ",$line);
$line = "$line\n";
$output=$output.$line;}while($row_WADAcustomers = mysql_fetch_assoc($WADAcustomers));
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=client_report.csv");
header("Content-Type: application/force-download");
header("Cache-Control: post-check=0, pre-check=0", false);
echo $output;
die();
}
?>
<?php
//Export to Excel Server Behavior
if (isset($_GET['Export'])&&($_GET['Export']=="Export Spreadsheet")){
$delim="";
$delim_replace="";
if($delim==""){
$lang=(strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],",")===false)?$_SERVER['HTTP_ACCEPT_LANGUAGE']:substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],","));
$semi_array=array("af","zh-hk","zh-mo","zh-cn","zh-sg","zh-tw","fr-ch","de-li","de-ch","it-ch","ja","ko","es-do","es-sv","es-gt","es-hn","es-mx","es-ni","es-pa","es-pe","es-pr","sw");
$delim=(in_array($lang,$semi_array) || substr_count($lang,"en")>0)?",":";";
}
$output="";
$output="1 = RecruitSavvy 2 = CareerSoft 3 = Warm Calling 4 = Unclaimed Property that 5 = Resume Search Only 6 = Career Changers 7 = Brokerage\n\n";
$include_hdr="1";
if($include_hdr=="1"){
$columns = array_keys($WADAcustomers->Results);
for ($x=0; $x<sizeof($columns); $x++) {
if($x!=0) $output.=","
$output.= (ereg_replace("_", " ",$columns[$x]));
}
$output = $output."\r\n";
}
for ($x=0; $x<sizeof($WADAcustomers->Results); $x++) {
$fixcomma=array();
foreach($WADAcustomers->Results[$x] as $r){array_push($fixcomma,ereg_replace($delim,$delim_replace,$r));}
$line = join($delim,$fixcomma);
$line=ereg_replace("\r\n", " ",$line);
$line = "$line\n";
$output=$output.$line;
}
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=client_report.csv");
header("Content-Type: application/force-download");
header("Cache-Control: post-check=0, pre-check=0", false);
echo $output;
die();
}
?>
There's a problem with this line:
$output.= (ereg_replace("_", " ",$columns[$x]));
The line:
$columns = array_keys($WADAcustomers);
should be:
$columns = array_keys($WADAcustomers->Results);
I've updated it above.
Still no good. there was a semicolon missing also which I fixed here
if($x!=0) $output.=",";
But when I hit the Export Spreadsheet I get a 500 error
Your code uses the function: ereg_replace();
That function has been removed in php7, so the code had to be updated accordingly. Here is the updated code:
<?php
//Export to Excel Server Behavior
if (isset($_GET['Export'])&&($_GET['Export']=="Export Spreadsheet")){
$delim="";
$delim_replace="";
if($delim==""){
$lang=(strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],",")===false)?$_SERVER['HTTP_ACCEPT_LANGUAGE']:substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'],","));
$semi_array=array("af","zh-hk","zh-mo","zh-cn","zh-sg","zh-tw","fr-ch","de-li","de-ch","it-ch","ja","ko","es-do","es-sv","es-gt","es-hn","es-mx","es-ni","es-pa","es-pe","es-pr","sw");
$delim=(in_array($lang,$semi_array) || substr_count($lang,"en")>0)?",":";";
}
$output="";
$output="1 = RecruitSavvy 2 = CareerSoft 3 = Warm Calling 4 = Unclaimed Property that 5 = Resume Search Only 6 = Career Changers 7 = Brokerage\n\n";
$include_hdr="1";
if($include_hdr=="1"){
$columns = array_keys($WADAcustomers->Results[0]);
for ($x=0; $x<sizeof($columns); $x++) {
if($x!=0) $output.=",";
$output.= str_replace("_", " ",$columns[$x]);
}
$output = $output."\r\n";
}
for ($x=0; $x<sizeof($WADAcustomers->Results); $x++) {
$fixcomma=array();
foreach($WADAcustomers->Results[$x] as $r){array_push($fixcomma,str_replace($delim,$delim_replace,$r));}
$line = join($delim,$fixcomma);
$line=str_replace("\r\n", " ",$line);
$line = "$line\n";
$output=$output.$line;
}
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=client_report.csv");
header("Content-Type: application/force-download");
header("Cache-Control: post-check=0, pre-check=0", false);
echo $output;
die();
}
?>
This stuff is way beyond my paygrade. I'm having a tough time outputting the contents of my table because there's commas in the stored fields. Any chance I can get some help, Ray? Sorry, I was going to start a new thread, but this seemed like a good place to start.
You can use this function:
https://www.php.net/manual/en/function.fputcsv.php
It will account for commas and other special characters when creating csv files.
Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.