How to generate an Excel document with multiple worksheets from PHP?

后端 未结 4 1253
执笔经年
执笔经年 2020-11-29 00:36

I want to generate an MS Excel file from PHP. I know one can do something like this:

header ( \"Content-type: application/vnd.ms-excel\" );
header ( \"Conten         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 01:22

      array('sheet_title' => 'Products', 
                                       'sheet_heading' => array('Article_Number','Name'),
                                       'sheet_data' => array('1234','Pen')
                                       ),
                            //2nd Sheet Details
                            1 => array('sheet_title' => 'Categories',
                                       'sheet_heading' => array('Category Id','Name'),
                                       'sheet_data' => array(123,'Accessories')
                                      )
                       );
    
     $sheet_count = 0;
     $row = 1;
     $column = 0;
     while ($sheet_count <= count($sheet_details)) {
          $objWorkSheet = '';
          if ($report_file_exists == 0) {
               if ($sheet_count > 0) {
                    $objWorkSheet = $objPHPExcel->createSheet($sheet_count);
               } else {
                    $objWorkSheet = $objPHPExcel->getActiveSheet();
               }
               $row = 1;
               $column = 0;
               foreach ($sheet_details[$sheet_count]['sheet_heading'] as $head) {
                    $objWorkSheet->setCellValue($columns[$column] . $row, $head);
                    $column++;
               }
          } else {
               $objPHPExcel->setActiveSheetIndex($sheet_count);
               $objWorkSheet = $objPHPExcel->getActiveSheet($sheet_count);
          }
    
          $row = $objWorkSheet->getHighestRow() + 1; //row count
          foreach ($sheet_details[$sheet_count]['sheet_data'] as $report_details) {
               $column = 0;
               foreach ($report_details as $data) {
                    $objWorkSheet->setCellValue($columns[$column] . $row, $data);
                    $column++;
               }
               $row++;
          }
    
          $objWorkSheet->setTitle($sheet_details[$sheet_count]['sheet_title']);
          $sheet_count++;
     }
    
     $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    
     $objWriter->save($report_file);
     ?>
    

提交回复
热议问题