Note that there are some explanatory texts on larger screens.

plurals
  1. POCant export to excel 2010 student edition via Zend Framework
    text
    copied!<p>I'm trying to export data to ms excel, I have Office 2010 student edition for my application by following Pablo Viquez's post <a href="http://www.pabloviquez.com/2009/08/export-excel-spreadsheets-using-zend-framework/" rel="nofollow">http://www.pabloviquez.com/2009/08/export-excel-spreadsheets-using-zend-framework/</a>. </p> <p>However I can't get it working the following happens </p> <p>1.On entering reports/report/todaysappointmentsreport/format/excel into the address bar the workbook attempts to save as excel.xls </p> <p>2.When I open the file Excel gives me the following warning: The file you are trying to open 'excel.xls', is in a different format than specified by the file extension verify the file is not corrupt and is from a trusted source before opening the file. </p> <ol> <li>On opening the file all that is shown is the code in my todaysappointmentsreport.export.phtml file </li> </ol> <p>Can anyone tell me where I'm going wrong, as I need to get this working. </p> <p>I can verify that the query works and the data appears in my todaysappointmentsreport.phtml file on screen. I can also verify that the component is installed in pear along with the OLE component that it is dependent on. </p> <p>My Controller code </p> <pre><code>class Reports_ReportController extends Zend_Controller_Action { public function init() { // Excel format context $excelConfig = array('excel' =&gt; array ('suffix' =&gt; 'excel', 'headers' =&gt; array( 'Content-type' =&gt; 'application/vnd.ms-excel')), ); //initalise context switch $contextSwitch = $this-&gt;_helper-&gt;contextSwitch(); // Add the new context $contextSwitch-&gt;setContexts($excelConfig); // Set the new context to the reports action $contextSwitch-&gt;addActionContext('todaysappointmentsreport', 'excel'); $contextSwitch-&gt;initContext(); } // action to redirect user straight to login page public function preDispatch() { // set admin layout // check if user is authenticated // if not, redirect to login page $url = $this-&gt;getRequest()-&gt;getRequestUri(); if (!Zend_Auth::getInstance()-&gt;hasIdentity()) { $session = new Zend_Session_Namespace('petmanager.auth'); $session-&gt;requestURL = $url; $this-&gt;_redirect('/login'); } } // report to print todays appointments public function todaysappointmentsreportAction() { $t=date('y-m-d'); $q = Doctrine_Query::create() -&gt;from('PetManager_Model_Groomappointments g') -&gt;leftJoin('g.PetManager_Model_Clients c') -&gt;leftJoin('g.PetManager_Model_Pets p') -&gt;leftJoin('g.PetManager_Model_Users u') -&gt;leftJoin('g.PetManager_Model_Groomservices s') -&gt;leftJoin('s.PetManager_Model_Groomprocedures r') -&gt;where('g.gapmtStatus = 1 AND g.gapmtDate = ?',$t) -&gt;orderBy('g.gapmtSTime,g.gapmtSTime,u.name'); $result = $q-&gt;fetchArray(); if (count($result) &gt;= 1) { $this -&gt; view -&gt; records = $result; } } </code></pre> <p>My todaysappointmentsreport.excel.phtml code as I said this is what appears in the excel file when I open it. </p> <pre><code> // Change error reporting for compatibility // Spreadsheet Excel Writter was built using PHP4, // so there's a lot of DEPRECATED notices error_reporting(E_ERROR | E_WARNING | E_PARSE); /** * PEAR package * * @link http://pear.php.net/package/Spreadsheet_Excel_Writer * @see PEAR/Spreadsheet/Excel/Writer.php */ require_once 'Spreadsheet/Excel/Writer.php'; // Lets define some custom colors codes define('CUSTOM_DARK_BLUE', 20); define('CUSTOM_BLUE', 21); define('CUSTOM_LIGHT_BLUE', 22); define('CUSTOM_YELLOW', 23); define('CUSTOM_GREEN', 24); // First, we create a Workbook $workbook = new Spreadsheet_Excel_Writer(); // Add one sheet, called: Users Report $worksheet = &amp;$workbook-&gt;addWorksheet('Todays Grooming Appointments Report'); // Create the custom colors on our new workbook // This function takes 4 params: // - Code index [1 to 64] // - RGB colors (0-255) $workbook-&gt;setCustomColor(CUSTOM_DARK_BLUE, 31, 73, 125); $workbook-&gt;setCustomColor(CUSTOM_BLUE, 0, 112, 192); $workbook-&gt;setCustomColor(CUSTOM_LIGHT_BLUE, 184, 204, 228); $workbook-&gt;setCustomColor(CUSTOM_YELLOW, 255, 192, 0); $workbook-&gt;setCustomColor(CUSTOM_GREEN, 0, 176, 80); // Lets hide gridlines //$worksheet-&gt;hideScreenGridlines(); // Lets create some custom styles $formatHeader = &amp;$workbook-&gt;addFormat(); $formatHeader = &amp;$workbook-&gt;addFormat( array('Size' =&gt; 16, 'VAlign' =&gt; 'vcenter', 'HAlign' =&gt; 'center', 'Bold' =&gt; 1, 'Color' =&gt; 'white', 'FgColor' =&gt; CUSTOM_DARK_BLUE)); $formatReportHeader = &amp;$workbook-&gt;addFormat( array('Size' =&gt; 9, 'VAlign' =&gt; 'bottom', 'HAlign' =&gt; 'center', 'Bold' =&gt; 1, 'FgColor' =&gt; CUSTOM_LIGHT_BLUE, 'TextWrap' =&gt; true)); $formatData = &amp;$workbook-&gt;addFormat( array( 'Size' =&gt; 8, 'HAlign' =&gt; 'center', 'VAlign' =&gt; 'vcenter')); /** * First, format the worksheet, adding the headers * and row/columns custom sizes */ // Create a nice header with a dark blue background // The function setRow takes 3 parameters: // - row index // - row height // - Format to apply to row [Optional] $worksheet-&gt;setRow(0, 11, $formatHeader); $worksheet-&gt;setRow(1, 46, $formatHeader); $worksheet-&gt;setRow(2, 11, $formatHeader); $worksheet-&gt;setRow(3, 11, $formatHeader); $worksheet-&gt;setRow(4, 11, $formatHeader); // Set the size of the columns // The function setColumn takes 5 params: // - First column // - Last column // - Column Width // - Format [Optional, default = 0] // - Hidden [Optional, default = 0] $worksheet-&gt;setColumn(0, 0, 7); // shrink it to 7 $worksheet-&gt;setColumn(1, 1, 12); // set the width to 12 $worksheet-&gt;setColumn(1, 1, 15); // set the width to 15 $worksheet-&gt;setColumn(1, 1, 15); // set the width to 15 $worksheet-&gt;setColumn(1, 1, 15); // set the width to 15 /** * * Once we have the format ready, add the text to the spreadsheet * */ // Write a text header $worksheet-&gt;write(1, 1, 'Todays Grooming Appointments Report', $formatHeader); // Create the header for the data starting @ row 6 $indexCol = 0; $indexRow = 6; $worksheet-&gt;write($indexRow, $indexCol++, 'Scheduled Time', $formatReportHeader); $worksheet-&gt;write($indexRow, $indexCol++, 'Client', $formatReportHeader); $worksheet-&gt;write($indexRow, $indexCol++, 'Pet', $formatReportHeader); $worksheet-&gt;write($indexRow, $indexCol++, 'Procedure', $formatReportHeader); $worksheet-&gt;write($indexRow, $indexCol++, 'Groomer', $formatReportHeader); $indexRow++; // Advance to the next row $indexCol = 0; // Start @ column 0 // Print the report data if(count($this-&gt;records) == 0) { // No data $worksheet-&gt;write( $indexRow, $indexCol, 'No Appointments', $formatData); } else { // Write the data foreach ($this-&gt;records as $r) { $worksheet-&gt;write( $indexRow, $indexCol++, $this-&gt;$r['gapmtSTime'] - $this-&gt;substr$r['gapmtETime'], $formatData); $worksheet-&gt;write( $indexRow, $indexCol++, $this-&gt;$r['PetManager_Model_Clients']['firstName'] $this-&gt;$r ['PetManager_Model_Clients']['lastName'], $formatData); $worksheet-&gt;write( $indexRow, $indexCol++, $this-&gt;$r['PetManager_Model_Pets']['name'], $formatData); $worksheet-&gt;write( $indexRow, $indexCol++, $this-&gt;$r['PetManager_Model_Groomservices']['PetManager_Model_Groomprocedures']['groomprocedure'], $formatData); $worksheet-&gt;write( $indexRow, $indexCol++, $this-&gt;$r['PetManager_Model_Users']['name'], $formatData); // Advance to the next row $indexRow++; } } /** * * Response with the excel file * */ // Sends HTTP headers for the Excel file. $workbook-&gt;send('todaysappointmentsreport.xls'); // Calls finalization methods. // This method should always be the last one to be called on every workbook $workbook-&gt;close(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload