⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Export button



Ralph Schipper
  • profile picture
  • Member

Posted 09 November 2013 - 08:26 AM

Hello peole,

I saw that there is a export button that exports the list to xls.
It opens fine in excell, but it is actualy a csv.
When I want to ceate a mailinglist in word and connect to this xls file it gives a error.
I can solve this by rename .xls to .csv.
Is there a way that when I hit the export button, that it saves the list with the extention .csv instead of .xls?

Regards

Ralph

edramirez
  • profile picture
  • Member

Posted 09 November 2013 - 11:54 AM

Here's a cheesy way of doing it:

 

In your editor, open Grocery_CRUD.php inside the libraries folder under the applications folder.

 

Proceed to line# 1602. 

 

You will find these last few characters in that line: ".xls";

 

Replace the xls with csv.

 

Save the file and you're good to go.

 

That's it!

 

Regards,

 

Ed

 

P.S. Don't forget to click the LIKE button!


edramirez
  • profile picture
  • Member

Posted 09 November 2013 - 19:07 PM

Exporting to CSV instead of XLS can be accomplished several ways. The neater way of doing this is to replace the default grocerycrud action for export using the getState function. Always remember that this procedure works only if you use the datatables theme.

 

step 1 - Insert the code to use the datatables theme.

example:

  $gcrud->set_theme('datatables');

 

step 2 - Insert the code to override using getState. The code is placed between the render and the view.

For example, your code might look like this:

  $output = $gcrud->render();

  $this->load->view('report.php',$output); 
 
Insert getState, adjusting the location of your view:

  $output = $gcrud->render();

  // overriding the export action

  $state = $gcrud->getState();
  $state_info = $gcrud->getStateInfo();
  if($state == 'export')
  {
    // this new function replaces grocerycrud's export action
    $this->export_to_csv($state_info);
  }
  else
  {
    // your view is repositioned to this location
    $this->load->view('report.php',$output); 
  }
 

step 3 - Add these 2 new functions to replace grocerycrud's export function. Copy exactly as is:

public function export_to_csv($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_csv($data);
}
 
 
protected function _export_to_csv($data)
{
$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").".csv";
 
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();
}
 

Then you're done!

 

Don't forget to click the Like button!

 

Ed Ramirez


Ralph Schipper
  • profile picture
  • Member

Posted 10 November 2013 - 07:43 AM

Hmm ok, i like the first post better and easyer.
Why use such a bunch of text while you can fix it with changing one line.
The thing is that you dont save a xls file, you are saving a csv file with the extention xls
Try opening a real xls file in notepad, you get rubish,
Try open this export file and you see a readable tab sepperated file.


So why is the second post neater?

Freindly regards Ralph

edramirez
  • profile picture
  • Member

Posted 11 November 2013 - 02:25 AM

The second option is for others who are following this thread and want a solution that applies to one particular controller only but don't want to fiddle around with the core grocerycrud files.

 

Regards,

 

 

Ed Ramirez


Ralph Schipper
  • profile picture
  • Member

Posted 11 November 2013 - 20:42 PM

The second option is for others who are following this thread and want a solution that applies to one particular controller only but don't want to fiddle around with the core grocerycrud files.

 

Regards,

 

 

Ed Ramirez

 

oh yes ofcourse,

 

that makes perfect sense so people have a choice.

 

thanks for the clarification!

 

Ralph


dhoni223
  • profile picture
  • Member

Posted 25 January 2018 - 05:47 AM

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Main extends CI_Controller {
 
function __construct()
{
        parent::__construct();
$this->load->database();
$this->load->helper('url');
  $this->load->library('grocery_CRUD');
 }
public function index()
{
}
function _example_output($output = null)
{
$this->load->view('our_template.php',$output);    
}
public function users()
{
$crud = new grocery_CRUD();
 
$crud->set_theme('datatables');
$crud->set_table('subscription');
$output = $crud->render();
 $state = $crud->getState();
  $state_info = $crud->getStateInfo();
  if($state == 'export')
  {
$this->export_to_csv();
  }
  else
  {
$this->_example_output($output);    
}
}
public function export_to_csv()
 
{
$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_csv($data);
}
protected function _export_to_excel($data)
{
$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";
}
$string_to_export = "\xFF\xFE" .mb_convert_encoding($string_to_export, 'UTF-16LE', 'UTF-8');
 
$filename = "export-".date("Y-m-d_H:i:s").".doc";
 
header('Content-type: application/msword;charset=UTF-8');
header('Content-Disposition: attachment; filename='.$filename);
header("Cache-Control: no-cache");
echo $string_to_export;
die();
}
}

  why its not working