How add layout export to ms.excel?
- Single Page
Posted 12 September 2012 - 05:10 AM
{
private $theme_path = null;
private $views_as_string = '';
private $echo_and_die = false;
protected $theme = null;
protected $default_true_false_text = array('inactive' , 'active');
protected $css_files = array();
protected $js_files = array();
protected function set_basic_Layout()
{
if(!file_exists($this->theme_path.$this->theme.'/views/list_template.php'))
{
throw new Exception('The template does not exist. Please check your files and try again.', 12);
die();
}
}
protected function showList($ajax = false, $state_info = null)
{
$data = $this->get_common_data();
$data->order_by = $this->order_by;
$data->types = $this->get_field_types();
$data->list = $this->get_list();
$data->list = $this->change_list($data->list , $data->types);
$data->list = $this->change_list_add_actions($data->list);
$data->total_results = $this->get_total_results();
$data->columns = $this->get_columns();
$data->success_message = $this->get_success_message_at_list($state_info);
$data->primary_key = $this->get_primary_key();
$data->add_url = $this->getAddUrl();
$data->edit_url = $this->getEditUrl();
$data->delete_url = $this->getDeleteUrl();
$data->ajax_list_url = $this->getAjaxListUrl();
$data->ajax_list_info_url = $this->getAjaxListInfoUrl();
//$data->export_url = $this->getExportToExcelUrl();
//$data->print_url = $this->getPrintUrl();
$data->actions = $this->actions;
$data->unique_hash = $this->get_method_hash();
$data->unset_add = $this->unset_add;
$data->unset_edit = $this->unset_edit;
$data->unset_delete = $this->unset_delete;
//$data->unset_export = $this->unset_export;
//$data->unset_print = $this->unset_print;
$ci = &get_instance();
$ci->load->config('grocery_crud');
$default_per_page = $ci->config->item('grocery_crud_default_per_page');
$data->paging_options = array('10','25','50','100');
$data->default_per_page = is_numeric($default_per_page) && $default_per_page >1 && in_array($default_per_page,$data->paging_options)? $default_per_page : 25;
if($data->list === false)
{
throw new Exception('It is impossible to get data. Please check your model and try again.', 13);
$data->list = array();
}
foreach($data->list as $num_row => $row)
{
$data->list[$num_row]->edit_url = $data->edit_url.'/'.$row->{$data->primary_key};
$data->list[$num_row]->delete_url = $data->delete_url.'/'.$row->{$data->primary_key};
}
if(!$ajax)
{
$data->list_view = $this->_theme_view('list.php',$data,true);
$this->_theme_view('list_template.php',$data);
}
else
{
$this->set_echo_and_die();
$this->_theme_view('list.php',$data);
}
}
protected function exportToExcel($state_info = null)
{
$data = $this->get_common_data();
$data->order_by = $this->order_by;
$data->types = $this->get_field_types();
$data->list = $this->get_list();
$data->list = $this->change_list($data->list , $data->types);
$data->list = $this->change_list_add_actions($data->list);
$data->total_results = $this->get_total_results();
$data->columns = $this->get_columns();
$data->primary_key = $this->get_primary_key();
@ob_end_clean();
$this->_export_to_excel($data);
}
protected function _export_to_excel($data)
{
/**
* No need to use an external library here. The only bad thing without using external library is that Microsoft Excel is complaining
* that the file is in a different format than specified by the file extension. If you press "Yes" everything will be just fine.
* */
$string_to_export = "";
foreach($data->columns as $column){
$string_to_export .= $column->display_as."\t";
}
$string_to_export .= "\n";
foreach($data->list as $num_row => $row){
foreach($data->columns as $column){
$string_to_export .= $this->_trim_export_string($row->{$column->field_name})."\t";
}
$string_to_export .= "\n";
}
// Convert to UTF-16LE and Prepend BOM
$string_to_export = "\xFF\xFE" .mb_convert_encoding($string_to_export, 'UTF-16LE', 'UTF-8');
$filename = "export-".date("Y-m-d_H:i:s").".xls";
header('Content-type: application/vnd.ms-excel;charset=UTF-16LE');
header('Content-Disposition: attachment; filename='.$filename);
header("Cache-Control: no-cache");
echo $string_to_export;
die();
}
protected function print_webpage($state_info = null)
{
$data = $this->get_common_data();
$data->order_by = $this->order_by;
$data->types = $this->get_field_types();
$data->list = $this->get_list();
$data->list = $this->change_list($data->list , $data->types);
$data->list = $this->change_list_add_actions($data->list);
$data->total_results = $this->get_total_results();
$data->columns = $this->get_columns();
$data->primary_key = $this->get_primary_key();
@ob_end_clean();
$this->_print_webpage($data);
}
protected function _print_webpage($data)
{
$string_to_print = "<meta charset=\"utf-8\" /><style type=\"text/css\" >
#print-table{ color: #000; background: #fff; font-family: Verdana,Tahoma,Helvetica,sans-serif; font-size: 13px;}
#print-table table tr td, #print-table table tr th{ border: 1px solid black; border-bottom: none; border-right: none; padding: 4px 8px 4px 4px}
#print-table table{ border-bottom: 1px solid black; border-right: 1px solid black}
#print-table table tr th{text-align: left;background: #ddd}
#print-table table tr:nth-child(odd){background: #eee}
</style>";
$string_to_print .= "<div id='print-table'>";
$string_to_print .= '<table width="100%" cellpadding="0" cellspacing="0" ><tr>';
foreach($data->columns as $column){
$string_to_print .= "<th>".$column->display_as."</th>";
}
$string_to_print .= "</tr>";
foreach($data->list as $num_row => $row){
$string_to_print .= "<tr>";
foreach($data->columns as $column){
$string_to_print .= "<td>".$this->_trim_print_string($row->{$column->field_name})."</td>";
}
$string_to_print .= "</tr>";
}
$string_to_print .= "</table></div>";
echo $string_to_print;
die();
}
protected function _trim_export_string($value)
{
$value = str_replace(array(" ","&",">","<"),array(" ","&",">","<"),$value);
return str_replace(array("\t","\n","\r"),"",$value);
}
protected function _trim_print_string($value)
{
$value = str_replace(array(" ","&",">","<"),array(" ","&",">","<"),$value);
//If the value has only spaces and nothing more then add the whitespace html character
if(str_replace(" ","",$value) == "")
$value = " ";
return $value;
}
[attachment=277:layout.png][attachment=277:layout.png]
Posted 17 February 2014 - 02:36 AM
Hi there
The code which supports to add layout export to ms.excel is so complicated for me.I want to know that if there is an excel tool which supports to do that directly without code?
Thanks a lot