简体   繁体   English

将html表导出到xls模板PHPExcel

[英]Export html table to xls template PHPExcel

All in the title, 全部在标题中,

I just want to export a html table to .xls template using PHPExcel . 我只想使用PHPExcelhtml表导出到.xls模板

I googled the last two days but I couldn't find a working solution. 我在谷歌搜索了最近两天,但我找不到一个有效的解决方案。

Thanks in advance. 提前致谢。

EDIT 编辑

I found this in stackoverflow, but I was unable to work with it 我在stackoverflow中找到了这个,但我无法使用它

  // $sql = sql query e.g "select * from mytablename"
    // $filename = name of the file to download 
        function queryToExcel($sql, $fileName = 'name.xlsx') {
                // initialise excel column name
                // currently limited to queries with less than 27 columns
        $columnArray = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
                // Execute the database query
                $result =  mysql_query($sql) or die(mysql_error());

                // Instantiate a new PHPExcel object
                $objPHPExcel = new PHPExcel();
                // Set the active Excel worksheet to sheet 0
                $objPHPExcel->setActiveSheetIndex(0);
                // Initialise the Excel row number
                $rowCount = 1;
    // fetch result set column information
                $finfo = mysqli_fetch_fields($result);
// initialise columnlenght counter                
$columnlenght = 0;
                foreach ($finfo as $val) {
// set column header values                   
  $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$columnlenght++] . $rowCount, $val->name);
                }
// make the column headers bold
                $objPHPExcel->getActiveSheet()->getStyle($columnArray[0]."1:".$columnArray[$columnlenght]."1")->getFont()->setBold(true);

                $rowCount++;
                // Iterate through each result from the SQL query in turn
                // We fetch each database result row into $row in turn

                while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
                    for ($i = 0; $i < $columnLenght; $i++) {
                        $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$i] . $rowCount, $row[$i]);
                    }
                    $rowCount++;
                }
// set header information to force download
                header('Content-type: application/vnd.ms-excel');
                header('Content-Disposition: attachment; filename="' . $fileName . '"');
                // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file        
                // Write the Excel file to filename some_excel_file.xlsx in the current directory                
                $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
                // Write the Excel file to filename some_excel_file.xlsx in the current directory
                $objWriter->save('php://output');
            }

PHP runs on the server, generates an HTML page, and only the generated page is sent to the client! PHP在服务器上运行,生成HTML页面,只有生成的页面才会发送到客户端!

PHPExcel is a PHP library, and hence also runs on the server, generating an Excel file (instead of an HTML page), which is sent to the client. PHPExcel是一个PHP库,因此也可以在服务器上运行,生成一个Excel文件(而不是HTML页面),然后发送到客户端。 You can't use PHPExcel to generate an XLS file from the HTML page, since that page resides on the client side. 您无法使用PHPExcel从HTML页面生成XLS文件,因为该页面位于客户端。 PHPExcel only has access to the information at the server side (for example, local PHP variables or an SQL database). PHPExcel只能访问服务器端的信息(例如,本地PHP变量或SQL数据库)。

If you want to use PHPExcel to generate your Excel file, please copy over the PHPExcel source files and examples on your server. 如果要使用PHPExcel生成Excel文件,请复制服务器上的PHPExcel源文件和示例。 Just point your browser to one of the example pages, and see how it generates the Excel file, giving you a download option, instead of showing it in browser. 只需将浏览器指向其中一个示例页面,然后查看它如何生成Excel文件,为您提供下载选项,而不是在浏览器中显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM