2016-06-14 58 views
1

所有的称号,导出HTML表格XLS模板PHPExcel

我只想一个HTML表导出到 .xls的模板使用PHPExcel

我搜索了最近两天,但我找不到工作解决方案。

在此先感谢。

编辑

我看到这个计算器,但我无法与它合作

// $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'); 
      } 
+0

如果您尝试通过'.xls'保存html'table'内容,您将得到相同的结果..试试这个! –

+0

为什么html?你可以直接从php获取行来生成excel:https://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home – Mimouni

+0

发布你的搜索结果,让我们知道你在做什么! –

回答

0

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

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

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