1

i have found this answer ,

PHP Converting CSV to XLS - phpExcel error

but i have tried it in Laravel 4 and i am not able to get it to work , any help would be appreciated.

My Code

    public function CsvExcelConverter($filename){
        $objReader = Excel::createReader('CSV');
        $objReader->setDelimiter(";");
        $objPHPExcel = $objReader->load('uploads/'.$filename);
        $objWriter = Excel::createWriter($objPHPExcel, 'Excel5');
       //new file
       $new_filename = explode('.',$filename);
       $new_name = $new_filename[1];
       $objWriter->save($new_name.'.xls');
       return $new_name.'.xls';
    }
3
  • Where is your code? Do you want to use maatwebsite.nl/laravel-excel/docs ? You should provide your code and sample CSV file Commented Oct 15, 2014 at 6:32
  • Hello Thanks for the reply; yes i am using maatwebsite.nl/laravel-excel/docs , and i found the code in the example but system complains that function createReader and createWriter do not exist. Commented Oct 15, 2014 at 6:36
  • You should put your code into question Commented Oct 15, 2014 at 6:39

4 Answers 4

1

thank for the answers, but for some reason we cant seem to set the delimiter on load but i have found that you can set it in the config file .

vendeor/maatwebsite/excel/src/config/csv.php

then just specify the delimiter. this way when loading the file it actually separates each entry and when converting it each entry is in its own cell.

thanks for all the help.

Sign up to request clarification or add additional context in comments.

Comments

0
/* Get the excel.php class here: http://www.phpclasses.org/browse/package/1919.html */
require_once("../classes/excel.php");
$inputFile=$argv[1];
$xlsFile=$argv[2];

if( empty($inputFile) || empty($xlsFile) ) {
    die("Usage: ". basename($argv[0]) . " in.csv out.xls\n" );
}

$fh = fopen( $inputFile, "r" );
if( !is_resource($fh) ) {
    die("Error opening $inputFile\n" );
}

/* Assuming that first line is column headings */
if( ($columns = fgetcsv($fh, 1024, "\t")) == false ) {
    print( "Error, couldn't get header row\n" );
    exit(-2);
}
$numColumns = count($columns);

/* Now read each of the rows, and construct a
    big Array that holds the data to be Excel-ified: */
$xlsArray = array();
$xlsArray[] = $columns;
while( ($rows = fgetcsv($fh, 1024, "\t")) != FALSE ) {
    $rowArray = array();
    for( $i=0; $i<$numColumns;$i++ ) {
        $key = $columns[$i];
        $val = $rows[$i];
        $rowArray["$key"] = $val;
    }
    $xlsArray[] = $rowArray;
    unset($rowArray);
}
fclose($fh);

/* Now let the excel class work its magic. excel.php
    has registered a stream wrapper for "xlsfile:/"
    and that's what triggers its 'magic': */
$xlsFile = "xlsfile://".$xlsFile;
$fOut = fopen( $xlsFile, "wb" );
if( !is_resource($fOut) ) {
    die( "Error opening $xlsFile\n" );
}
fwrite($fOut, serialize($xlsArray));
fclose($fOut);

exit(0);

Comments

0

If you use the maatwebsite/excel library in Laravel, you can only use native PHPExcel instance methods, not static methods. To convert from CSV to excel, this code can be found at Documentation page

Excel::load($filename, function($file) {
    // modify file content
})->setFileName($new_name)->store('xls');

Comments

0

In theory, you should create your custom class to set delimiter:

class CSVExcel  extends Excel {
    protected $delimiter  = ';';
}

and now you could use:

CSVExcel::load('csvfilename.csv')->setFileName('newfilename')->export('xls');

But the problem is, that $delimiter isn't used in this case. Delimiter support seems to be added not long time ago, so maybe there is a bug or it needs to be used in the other way. I've added issue just in case for that: https://github.com/Maatwebsite/Laravel-Excel/issues/262

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.