Asked  7 Months ago    Answers:  5   Viewed   42 times

Is it possible to append content to an .xls file using PHP fwrite()?

When I try this using fwrite(), the resulting file causes an error message in Excel 2007.

Is there a specific separator I can use to accomplish this?

Is it possible without a third party library?

 Answers

98

You can use the PhpSpreadsheet library, to read an existing Excel file, add new rows/columns to it, then write it back as a real Excel file.

Disclaimer: I am one of the authors of this library.

Wednesday, March 31, 2021
 
mozlima
answered 7 Months ago
47

Thanks for Helping me I have found the answer for my own question.

    <?php

    $rowNumber = 12;
    foreach($list as $data){
$this->excel->getActiveSheet()->setCellValue('A'.$rowNumber, $data->code_caption)
->setCellValue('B'.$rowNumber, $data->color);

if(file_exists('./media/img/gallery/items/'.$data->image.'TH.jpg'))
{
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath('./media/img/gallery/items/'.$data->image.'TH.jpg');
    $objDrawing->setCoordinates('C'.$rowNumber);
    $objDrawing->setWorksheet($this->excel->getActiveSheet());
    $this->excel->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(120);
}
else
{
    $this->excel->getActiveSheet()->setCellValue('C'.$rowNumber, '');
}

$this->excel->getActiveSheet()->setCellValue('D'.$rowNumber, $data->desc);
$this->excel->getActiveSheet()->setCellValue('E'.$rowNumber, $data->$d_met);
$this->excel->getActiveSheet()->setCellValue('F'.$rowNumber, $data->$w_met);
$this->excel->getActiveSheet()->setCellValue('G'.$rowNumber, $data->$h_met);
$this->excel->getActiveSheet()->setCellValue('H'.$rowNumber, $data->qty);
$this->excel->getActiveSheet()->setCellValue('I'.$rowNumber,$data->$cur_dat);           
$this->excel->getActiveSheet()->setCellValue('J'.$rowNumber,$data->$total_price);
$rowNumber++;
    }?>
Wednesday, March 31, 2021
 
Laimoncijus
answered 7 Months ago
31

Yes.

function addItem($serializedArray, $item)
{
   $a = unserialize($serializedArray);
   $a[] = $item;
   return serialize($a);
}
Wednesday, March 31, 2021
 
astaykov
answered 7 Months ago
89

Apparently it was the application/vnd.ms-excel header that caused the problem. Setting it to octet-stream solved the issue, but then caused problems in IE8. So I used this code to totally solve the problem:

if(stristr($_SERVER['HTTP_USER_AGENT'], 'ipad') OR stristr($_SERVER['HTTP_USER_AGENT'],     'iphone') OR stristr($_SERVER['HTTP_USER_AGENT'], 'ipod')) 
{ 
  header("Content-Type: application/octet-stream"); 
}else{ 
  header('Content-Type: application/vnd.ms-excel'); 
}
Wednesday, March 31, 2021
 
dotoree
answered 7 Months ago
92

The error MethodNotAllowedHttpException means the route exists, but the HTTP method (GET) is wrong. You have to change it to POST:

Route::post('test/register', array('uses'=>'TestController@create'));

Also, you need to hash your passwords:

public function create()
{
    $user = new User;

    $user->username = Input::get('username');
    $user->email = Input::get('email');
    $user->password = Hash::make(Input::get('password'));
    $user->save();

    return Redirect::back();
}

And I removed the line:

$user= Input::all();

Because in the next command you replace its contents with

$user = new User;

To debug your Input, you can, in the first line of your controller:

dd( Input::all() );

It will display all fields in the input.

Sunday, August 22, 2021
 
muued
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :