How to export an CSV excel file to pipe (|) delimited/separated in Laravel 4.2 with example

Hello Friends, sometime requirement is comes, out of default things provided by the frameworks or CMS, so in this case we needs customization in code or we needs to overrides the configuration settings if there.

Recently, I worked on generating csv file to create a pipe delimited file from an Excel Spreadsheet. Please go through the steps below to make it happened in Laravel 4.2

Note:You can keep any name for route, controller name and it's method name

Step 1: First of all we need to set Route.

For example: Route::any('/pipedelimited', array('uses' => 'PipedelimitedController@pipedelimited')); 

Step 2: Create controller at path app\controllers\PipedelimitedController.php and add pipedelimited() method in this controller. Also I am using here custom log and try catch blocks for logging and generating errors or exception while executing this script which is I recommended you for best practice standard.

For example::
<?php
use Monolog\Logger;//Use for log
use Monolog\Handler\StreamHandler;//Use for log

class PipedelimitedController extends BaseController
{
 
  public function pipedelimited() 
  {
  $error_log = new Logger('Test: CSV Pipe Delimited');
$error_log->pushHandler(new StreamHandler(storage_path().'/logs/pipedelimited.log', Logger::INFO));
$error_log->addInfo('CSV Pipe Delimited Start ');
try{

Config::set('excel::csv.delimiter', '|'); // Override default config

Excel::create('Newfile', function($excel) {

$excel->sheet('Newsheet', function($sheet) {

$sheet->loadView('pipedelimited');

});

})->store('csv', storage_path('exports'));

}catch (\Illuminate\Database\QueryException $ex) {           
            $error_log->addInfo($ex->getMessage());            
        } catch (\Exception $exx) {           
            $error_log->addInfo($exx->getMessage());            
        }
$error_log->addInfo('CSV Pipe Delimited END ');

  }
}

?>


As you can see in above public function pipedelimited() method, We are overriding the csv default delimiter Comma (,) configuration by using this code Config::set('excel::csv.delimiter', '|');

Step 3: Now we are creating the blade file that is the template file in which we will create a sample html table content with sample data. Create a pipedelimited.blade file at path app\views\pipedelimited.blade.php and add following content there.

For example::
<table>
<tr>
<th>Test1</th>
<th>Test1</th>
<th>Test1</th>
<th>Test1</th>
</tr>
<tr>
<td>Test2</td>
<td>Test2</td>
<td>Test2</td>
<td>Test2</td>
</tr>
</table>

Step 4: Now we ready to execute our script, we need to execute the URL http://yourhost.name/pipedelimited and the Newfile.csv file will be generate in folder app\storage\exports as we set this path while creating the csv.

This is it! We made it and please share your comments here if you are facing any issue or want to suggest anything related to this blog. Thanks!!!
How to export an CSV excel file to pipe (|) delimited/separated in Laravel 4.2 with example How to export an CSV excel file to pipe (|) delimited/separated in Laravel 4.2 with example Reviewed by Web Technology Funda on 2:29:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!