Export to CSV (Excel)?
- Single Page
Posted 30 January 2012 - 17:59 PM
I'm working with CodeIgniter and Grocery CRUD more and more, and like both very much!
I have a project where I need to generate CSV files from a databases of form submissions.
I know CodeIgniter's "$this->dbutil->csv_from_result()" together with "write_file" can probably accomplish this. But I was thinking Grocery CRUD might have a more direct approach.
Any ideas?
Thanks again,
Matthew
Posted 30 January 2012 - 21:02 PM
I still don't have any export button or a working example to show you. I have it of course in mind to have something like this in the future, but it is still not planned when and at what version. So you have to do something custom to make it work. I don't know if any other has an extension for this and help you.
Posted 30 January 2012 - 21:36 PM
OK, no problem!
I'll use the built-in CodeIgniter functions I mentioned above. I just wanted to make sure.
Thanks again for your excellent work,
Matthew
Posted 05 June 2012 - 17:48 PM
I'm a newbie here ... Do you have to run the same query again and execute dbutil ?
Posted 24 June 2012 - 13:25 PM
Below is my hack, not very elegant but it works and if version changes, it should be easy to maintain.
You can skip item 1 (unset_exp), it will be even more maintainable, but this will mean that every list page will display the 'Export CSV' button and you cannot hide it...
1. create an 'unset_exp' similar to 'unset_add', in /application/libraries/grocery_crud.php
I simple searched for unset_add, and added unset_exp below
e.g.
...
$data->unset_add = $this->unset_add;
$data->unset_exp = $this->unset_exp; // add this
...
protected $unset_add = false;
protected $unset_exp = false; // add this
...
2. also, add the following line after $this->getAddUrl(); in function showList(...) in /application/libraries/grocery_crud.php
$data->list_url = $this->getListUrl();
3. next edit the list_template.php (for both Datatables and Flexigrid), example for Datatables:
<?php if(!$unset_add){?>
<a role="button" class="edit_button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" href="<?php echo $add_url?>">
<span class="ui-button-icon-primary ui-icon ui-icon-circle-plus"></span>
<span class="ui-button-text"><?php echo $this->l('list_add'); ?> <?php echo $subject?></span>
</a>
<?php }?>
<!-- ADD THE LINES BELOW -->
<?php if(!$unset_exp){?>
<a role="button" class="edit_button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" target='blank' href="<?php echo $list_url.'/export'; ?>">
<span class="ui-button-icon-primary ui-icon ui-icon-circle-plus"></span>
<span class="ui-button-text"><?php echo 'EXPORT CSV' ?></span>
</a>
<?php }?>
For Flexigrid,find below line 37 of grocerycrud version 1.2.3:
<div id='main-table-box'>
Replace:
<?php if(!$unset_add){?>
<div class="tDiv">
<div class="tDiv2">
<a href='<?php echo $add_url?>' title='<?php echo $this->l('list_add'); ?> <?php echo $subject?>' class='add-anchor'>
<div class="fbutton">
<div>
<span class="add"><?php echo $this->l('list_add'); ?> <?php echo $subject?></span>
</div>
</div>
</a>
<div class="btnseparator">
</div>
</div>
<div class='clear'></div>
</div>
<?php }?>
with
<?php if(!$unset_add || !$unset_exp){ ?>
<div class="tDiv">
<div class="tDiv2">
<?php if(!$unset_add){?>
<a href='<?php echo $add_url?>' title='<?php echo $this->l('list_add'); ?> <?php echo $subject?>' class='add-anchor'>
<div class="fbutton">
<div>
<span class="add"><?php echo $this->l('list_add'); ?> <?php echo $subject?></span>
</div>
</div>
</a>
<div class="btnseparator">
</div>
<?php }?>
<?php if(!$unset_exp){?>
<a href='<?php echo $list_url.'/export'; ?>' target='blank' title='EXPORT CSV' class='add-anchor'>
<div class="fbutton">
<div>
<span class="add">EXPORT CSV</span>
</div>
</div>
</a>
<div class="btnseparator">
</div>
<?php }?>
</div>
<div class='clear'></div>
</div>
<?php }?>
Two Notes:
I added target='blank' to open export in new window
href points to $list_url.'/export';
In my controller...
function report_SIM_registration_by_year($operation = null) {
try{
if ($operation == 'export')
{
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=xyz.csv');
echo $this->dbutil->csv_from_result($query);
die();
}
// otherwise display as per normal...
...
$crud = new grocery_CRUD();
...
}
Hope this helps.
Posted 14 September 2012 - 09:34 AM
Posted 09 November 2013 - 19:05 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();
$output = $gcrud->render();
// overriding the export action
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)
Then you're done!
Don't forget to click the Like button!
Ed Ramirez
Posted 24 November 2015 - 07:01 AM
I am exporting the content from a table.But when it get exported in one column it show ####.I am using custom model for this. my code is