2

I'm having the following code that I'm using to load, read, write and download an excel file.

//include PHPExcel library

require_once "Classes/PHPExcel/IOFactory.php";

//load Excel template file

$objTpl = PHPExcel_IOFactory::load("excel.xls");
$objTpl->setActiveSheetIndex(0);  //set first sheet as active

$objTpl->getActiveSheet()->setCellValue('C2', date('Y-m-d'));  //set C1 to current date

$objTpl->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //C1 is right-justified

$strData = $objTpl->getActiveSheet()->getCell('C3')->getValue();
$strData .= $objTpl->getActiveSheet()->getCell('C4')->getValue();

$objTpl->getActiveSheet()->setCellValue('C3', 'It\'s working');
$objTpl->getActiveSheet()->setCellValue('C4', 'Dynamic excel read and writing');
$objTpl->getActiveSheet()->setCellValue('C6', 'Data read: C3 = '.substr($strData,0,1).' and C4 = '.substr($strData,1,1));

$objTpl->getActiveSheet()->getStyle('C4')->getAlignment()->setWrapText(true);  //set wrapped for some long text message

$objTpl->getActiveSheet()->getColumnDimension('C')->setWidth(40);  //set column C width

$objTpl->getActiveSheet()->getRowDimension('4')->setRowHeight(120);  //set row 4 height

$objTpl->getActiveSheet()->getStyle('A4:C4')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); //A4 until C4 is vertically top-aligned

//prepare download

$filename=mt_rand(1,100000).'.xls'; //just some random filename

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)

$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!

My problem is that I want to load the excel file from a different source(url,stream), not from the local directory

2
  • 1
    stackoverflow.com/questions/10127289/… Try this link. Commented Jan 29, 2013 at 8:51
  • @nsutgio thanx for the link. It provided me with more vital info. Commented Jan 29, 2013 at 9:01

1 Answer 1

2

Simply save the file to your server and then read it.

    file_put_contents('excel.xls',  
          file_get_contents('https://www.myweburl.com/spreadsheets.xls')
        );

    $objTpl = PHPExcel_IOFactory::load("excel.xls");
Sign up to request clarification or add additional context in comments.

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.