dcclassics
dcclassics

Reputation: 896

Parse PHPExcel file as HTML

I've got an upload page for users to upload excel files, this is part of a task request type thing. I am trying to have it so when the task is submitted, you can click a link that will then display an html table of the excel file they uploaded. PHP-excel-reader would work perfectly, but it doesn't support xlsx files. I'm looking at PHPExcel, but can't quite understand how I take those outputs and make an html table out of them. I'm also worried I can't get ZipArchive supported in PHP.

Does anyone know of an example where they've converted the PHPExcel example to an html table?

EDIT:

The following code is working great. It's creating a new file when the script is run. I can't quite figure out how to rename the file correctly. Currently it's renaming the actual phpexcel.php file containing the code below to phpexcel.htm. I would like to take the name of the $inputFileName and rename that to exceluploads/Book7.htm for example. I wasn't sure if it ws as simple as changing the $objWriter->save(path/name.htm); but that did not work, I received:

Warning: fopen(/exceluploads/Book7.htm) [function.fopen]: failed to open stream: No such file or directory in /var/www/Classes/PHPExcel/Writer/HTML.php on line 164

Fatal error: Uncaught exception 'Exception' with message 'Could not open file /exceluploads/Book7.htm for writing.'

Code:

    <?php

    /** Error reporting */
    error_reporting(E_ALL);

    /** Include PHPExcel */
    require_once dirname(__FILE__) . '/Classes/PHPExcel.php';


    // Create new PHPExcel object
    echo date('H:i:s') , " Create new PHPExcel object" , PHP_EOL;
    $objPHPExcel = new PHPExcel();

    $inputFileName = 'exceluploads/Book7.xlsx';
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

    echo date('H:i:s') , " Write to HTML format" , EOL;
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
    $objWriter->setSheetIndex(0);
    //$objWriter->setImagesRoot('http://www.example.com');
    $objWriter->save(str_replace('.php', '.htm', __FILE__));

    echo date('H:i:s') , " File written to " , str_replace('.php', '.htm', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;


    // Echo memory peak usage
    echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

    // Echo done
    echo date('H:i:s') , " Done writing file" , EOL;
    echo 'File has been created in ' , getcwd() , EOL;

    ?>

Upvotes: 1

Views: 20631

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

To read a file:

$inputFileName = 'example.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

To write a file:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('example.html');

Upvotes: 10

Related Questions