将html表导出到xls模板PHPExcel

时间:2016-06-14 12:52:06

标签: php excel phpexcel

所有标题,

我只想使用 PHPExcel html表格导出到 .xls模板

我在最近两天搜索了一下,但我找不到合适的解决方案。

提前致谢。

修改

我在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');
            }

1 个答案:

答案 0 :(得分:0)

PHP在服务器上运行,生成一个HTML页面,只有生成的页面才会发送到客户端!

PHPExcel是一个PHP库,因此也可以在服务器上运行,生成一个Excel文件(而不是HTML页面),然后发送到客户端。您无法使用PHPExcel从HTML页面生成XLS文件,因为该页面位于客户端。 PHPExcel只能访问服务器端的信息(例如,本地PHP变量或SQL数据库)。

如果您想使用PHPExcel生成Excel文件,请复制服务器上的PHPExcel源文件和示例。只需将浏览器指向其中一个示例页面,然后查看它如何生成Excel文件,为您提供下载选项,而不是在浏览器中显示。