Skip to content

Commit

Permalink
Updated feature Excel import/export now generates an Excel 2003-2007 …
Browse files Browse the repository at this point in the history
…formatted Excel file instead of a tab delimited one.
  • Loading branch information
TMSWhite committed Mar 19, 2012
1 parent 0a638f7 commit 7cc9465
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
34 changes: 31 additions & 3 deletions admin/export_structure_excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* $Id: export_structure_xml.php 11607 2011-12-06 23:19:52Z tmswhite $
*/


include_once("login_check.php");
include_once(dirname(__FILE__)."/classes/pear/Spreadsheet/Excel/Writer.php");

if (!isset($surveyid))
{
Expand Down Expand Up @@ -43,14 +43,42 @@

if (!isset($copyfunction))
{
$fn = "limesurvey_survey_$surveyid.txt";
$fn = "limesurvey_survey_$surveyid.xls";
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$fn");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public"); // HTTP/1.0
echo LimeExpressionManager::ExcelSurveyExport($surveyid);

$data =& LimeExpressionManager::ExcelSurveyExport($surveyid);

// actually generate an Excel workbook
$workbook = new Spreadsheet_Excel_Writer();
$workbook->setVersion(8);
$workbook->send($fn);

$sheet =& $workbook->addWorksheet(); // do not translate/change this - the library does not support any special chars in sheet name
$sheet->setInputEncoding('utf-8');

$rc = -1; // row counter
$cc = -1; // column counter
foreach($data as $row)
{
++$rc;
$cc=-1;
foreach ($row as $col)
{
// Enclose in \" if begins by =
++$cc;
if (substr($col,0,1) == "=")
{
$col = "\"".$col."\"";
}
$sheet->write($rc, $cc, $col);
}
}
$workbook->close();
exit;
}
?>
10 changes: 5 additions & 5 deletions admin/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1756,18 +1756,18 @@
."<div class='header ui-widget-header'>"
.$clang->gT("Export Survey Structure")."\n</div><br />\n"
."<ul style='margin-left:35%;'>\n"
."<li><input type='radio' class='radiobtn' name='action' value='exportstructurexml' checked='checked' id='surveyxml'"
."<label for='surveycsv'>"
."<li><input type='radio' class='radiobtn' name='action' value='exportstructurexml' checked='checked' id='surveyxml'>"
."<label for='surveyxml'>"
.$clang->gT("LimeSurvey XML survey file (*.lss)")."</label></li>\n";

$exportstructure.="<li><input type='radio' class='radiobtn' name='action' value='exportstructurequexml' id='queXML'"
$exportstructure.="<li><input type='radio' class='radiobtn' name='action' value='exportstructurequexml' id='queXML'>"
."<label for='queXML'>"
.str_replace('queXML','<a href="http://quexml.sourceforge.net/" target="_blank">queXML</a>',$clang->gT("queXML Survey XML Format (*.xml)"))." "
."</label></li>\n";

$exportstructure.="<li><input type='radio' class='radiobtn' name='action' value='exportstructureexcel' id='surveyexcel'"
$exportstructure.="<li><input type='radio' class='radiobtn' name='action' value='exportstructureexcel' id='surveyexcel'>"
."<label for='surveyexcel'>"
.$clang->gT("LimeSurvey Excel survey file (tab delimited, *.txt)")."</label></li>\n";
.$clang->gT("LimeSurvey Excel survey file (*.xls)")."</label></li>\n";

// XXX
//include("../config.php");
Expand Down
15 changes: 5 additions & 10 deletions classes/expressions/LimeExpressionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7060,7 +7060,7 @@ static public function ShowSurveyLogicFile($sid, $gid=NULL, $qid=NULL,$LEMdebugL
* @param type $sid
* @return type
*/
static public function ExcelSurveyExport($sid)
static public function &ExcelSurveyExport($sid)
{
$fields = array(
'class',
Expand Down Expand Up @@ -7443,26 +7443,21 @@ static public function ExcelSurveyExport($sid)
}
}
}
// Now generate the CSV output
// Now generate the array out output data
$out = array();
$out[] = implode("\t",$fields);
$out[] = $fields;

foreach ($rows as $row)
{
$tsv = array();
foreach ($fields as $field)
{
$val = (isset($row[$field]) ? $row[$field] : '');
if ($val != '')
{
$val = str_replace(array("\n","\r","\t"),array(" ", " ", " "),$val);
}
$tsv[] = $val;
}
$out[] = implode("\t",$tsv);
$out[] = $tsv;
}
$output = implode("\n",$out);
return $output;
return $out;
}
}
/**
Expand Down

0 comments on commit 7cc9465

Please sign in to comment.