导出html表格到xls模板PHPExcel

所有的标题,

我只想使用PHPExcelhtml表导出为.xls模板

我google了两天,但我找不到一个工作的解决scheme。

提前致谢。

编辑

我发现这个在stackoverflow,但我无法使用它

// $sql = sql query eg "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在服务器上运行,生成一个HTML页面,只有生成的页面被发送到客户端!

PHPExcel是一个PHP库,因此也在服务器上运行,生成发送给客户端的Excel文件(而不是HTML页面)。 您不能使用PHPExcel从HTML页面生成XLS文件,因为该页面驻留在客户端。 PHPExcel只能访问服务器端的信息(例如本地PHPvariables或SQL数据库)。

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