phpexcel导出html文件

2020-10-29

phpexcel可以讲excel转换为html文件,但是并没有直接将excel转为html的方法,我们可以自定义一个方法。

<?php

/*
\PHPExcel\Writer\HTML.php文件
在PHPExcel_Writer_HTML类中直接添加方法
*/
class PHPExcel_Writer_HTML extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {

    //将excel指定列转换为html
    public function get_html() {
        $this->_phpExcel->garbageCollect();
    
        // Build CSS
        $this->buildCSS(!$this->_useInlineCss);
        $html = $this->generateHTMLHeader(!$this->_useInlineCss);
        if ((!$this->_isPdf) && ($this->_generateSheetNavigationBlock)) {
            $html .= $this->generateNavigation();
        }
        $html .= $this->generateSheetData() . $this->generateHTMLFooter();
        return $html;
    }
}
//调用:
$path = '1.xlsx';
$file_type = \PHPExcel_IOFactory::identify($path);
$excel_io = \PHPExcel_IOFactory::createReader($file_type);
$phpexcel = $excel_io->load($path);
$objWriter = new \PHPExcel_Writer_HTML($phpexcel);
$objWriter->setSheetIndex(0);
$html = $objWriter->get_html();

此方法可以将excel的全部内容转换为html,但是如果excel表格的格式不规范,有一些无用的内容不想转换,那么就需要修改源码的方法,转换时只转换固定行列的excel单元格

<?php

/*
\PHPExcel\Writer\HTML.php文件
只有方法的部分代码
*/
class PHPExcel_Writer_HTML extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {

    //修改前
    /*public function generateSheetData() {

        // Get worksheet dimension
        $dimension = explode(':', $sheet->calculateWorksheetDimension());
    }*/
    //修改后
    public function generateSheetData($cell = null) {

        // Get worksheet dimension
        $dimension = explode(':', $sheet->calculateWorksheetDimension($cell));
    }
}


/*
\PHPExcel\Worksheet.php文件
只有方法的部分代码*
*/
class PHPExcel_Worksheet implements PHPExcel_IComparable
{
    /*修改前
    public function calculateWorksheetDimension()
    {
        // Return
        return 'A1' . ':' .  $this->getHighestColumn() . $this->getHighestRow();
    }*/

    //修改后;指定开始与结束列的索引 例:A1:I70
    public function calculateWorksheetDimension($cell = null)
    {
        // Return
        return is_null($cell) ? ('A1' . ':' . $this->getHighestColumn() . $this->getHighestRow()) : $cell;
    }
}

修改源码后修改生成html文件的方法

<?php

/*
\PHPExcel\Writer\HTML.php文件
在PHPExcel_Writer_HTML类中直接添加方法
*/
class PHPExcel_Writer_HTML extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {

    //将excel指定列转换为html
    public function get_html($cell = null) {
        $this->_phpExcel->garbageCollect();
    
        // Build CSS
        $this->buildCSS(!$this->_useInlineCss);
        $html = $this->generateHTMLHeader(!$this->_useInlineCss);
        if ((!$this->_isPdf) && ($this->_generateSheetNavigationBlock)) {
            $html .= $this->generateNavigation();
        }
        $html .= $this->generateSheetData($cell) . $this->generateHTMLFooter();
        return $html;
    }
}
//调用:
$path = '1.xlsx';
$file_type = \PHPExcel_IOFactory::identify($path);
$excel_io = \PHPExcel_IOFactory::createReader($file_type);
$phpexcel = $excel_io->load($path);
$objWriter = new \PHPExcel_Writer_HTML($phpexcel);
$objWriter->setSheetIndex(0);
$html = $objWriter->get_html('A1:I70');

excel文件转换为html文件

<?php

$path = '1.xlsx';
$file_type = \PHPExcel_IOFactory::identify($path);
$excel_io = \PHPExcel_IOFactory::createReader($file_type);
$phpexcel = $excel_io->load($path);

$savePath = '1.html';
$objWriter = new \PHPExcel_Writer_HTML($phpexcel);
$objWriter->setSheetIndex(0);
$objWriter->save($savePath);
{/if}