Problem with exporting unicode dataset to excel

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
jegegio
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed May 20, 2015 9:03 am

Problem with exporting unicode dataset to excel

Post by jegegio » Wed May 20, 2015 9:27 am

Hello All,

I have created my custom component for joomla 2.5 according to the MVC architecture
and called it "com_employees".
I have the model file called: "employeelistexcel.php" with a function "getdata()":

Code: Select all

public function getData(){
		if (empty( $this->_data )){
			$firstname = JRequest::getString('firstname',  '');
			$lastname = JRequest::getString('lastname',  '');
			if (!empty($firstname)){
				$where_firstname = ' AND firstname like "%'. $firstname . '%"';
			}
			if (!empty($lastname)){
				$where_lastname = ' AND lastname like "%'. $lastname . '%"';
			}
			$db = JFactory::getDBO();
			$query = ' SELECT id,user_id,firstname,lastname
			FROM bitnami_joomla.hr_employee 
			WHERE user_id>42 ' . $where_firstname . $where_lastname;
			$this->_data = $this->_getList( $query );
		}
		return $this->_data;
	}
Then I have the file with the same name in controllers folder with the following function:

Code: Select all

	protected $params = null;

	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct(){
		parent::__construct();
		
		// Set reference to parameters
		$this->params = JComponentHelper::getParams( 'com_employees' );
		//$dummy = $this->params->get('parm_text');
	}
in the com_employees/views/employeelistexcel folder I have a view.html.php file:

Code: Select all

class EmployeesViewEmployeelistExcel extends JViewLegacy{
	function display($tpl = null){
		$app = JFactory::getApplication();
		$document  = JFactory::getDocument();
		$document->_styleSheets = array(); 

		/*
		// load component parameters
		$params = JComponentHelper::getParams( 'com_employees' );
		$params = $app->getParams( 'com_employees' );	
		$dummy = $params->get( 'dummy_param', 1 ); 

		// load another model
		JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_employees/models');
		$otherModel = JModelLegacy::getInstance( 'Record', 'RecordModel' );
		*/
	
		$data = $this->get('Data');
		$this->assignRef('data', $data);
		
		$pagination = $this->get('Pagination');
		$this->assignRef('pagination', $pagination);

		parent::display($tpl);
	}
}
and in com_employees/views/employeelistexcel/tmpl/ the "default.php" file:

Code: Select all

header('Content-Encoding: UTF-8');
header('Content-Type: application/x-msexcel; charset=UTF-8; format=attachment;');
header("Content-disposition: attachment; filename=My_example.csv");
$utf8_text="id,user_id,firstname,lastname\n";
foreach ($this->data as $row)  {
	$utf8_text.=$row->id.",";
	$utf8_text.=$row->user_id.",";
	$utf8_text.=$row->lastname.",";
	$utf8_text.=$row->middlename."\n";

}
	$utf8_text = strip_tags($utf8_text);
    echo "\xEF\xBB\xBF"; // UTF-8 BOM
	echo $utf8_text;	
The page works, I download the CSV file but when I open it, I see the html tags and stylesheet links in it with my data which is weird symbols and not in unicode.
you can see the file screenshot in attached image.
Untitled.jpg
Can anyone somehow help me to remove this html staff and display the data in unicode?
You do not have the required permissions to view the files attached to this post.

Locked

Return to “Joomla! 2.5 Coding”