<legend id='nYgR1'><style id='nYgR1'><dir id='nYgR1'><q id='nYgR1'></q></dir></style></legend>
  • <i id='nYgR1'><tr id='nYgR1'><dt id='nYgR1'><q id='nYgR1'><span id='nYgR1'><b id='nYgR1'><form id='nYgR1'><ins id='nYgR1'></ins><ul id='nYgR1'></ul><sub id='nYgR1'></sub></form><legend id='nYgR1'></legend><bdo id='nYgR1'><pre id='nYgR1'><center id='nYgR1'></center></pre></bdo></b><th id='nYgR1'></th></span></q></dt></tr></i><div id='nYgR1'><tfoot id='nYgR1'></tfoot><dl id='nYgR1'><fieldset id='nYgR1'></fieldset></dl></div>

        • <bdo id='nYgR1'></bdo><ul id='nYgR1'></ul>

        <small id='nYgR1'></small><noframes id='nYgR1'>

        <tfoot id='nYgR1'></tfoot>

        使用PhpSpreadsheet导出数据到excel表格

        时间:2024-09-22
        <i id='opCw7'><tr id='opCw7'><dt id='opCw7'><q id='opCw7'><span id='opCw7'><b id='opCw7'><form id='opCw7'><ins id='opCw7'></ins><ul id='opCw7'></ul><sub id='opCw7'></sub></form><legend id='opCw7'></legend><bdo id='opCw7'><pre id='opCw7'><center id='opCw7'></center></pre></bdo></b><th id='opCw7'></th></span></q></dt></tr></i><div id='opCw7'><tfoot id='opCw7'></tfoot><dl id='opCw7'><fieldset id='opCw7'></fieldset></dl></div>

            <legend id='opCw7'><style id='opCw7'><dir id='opCw7'><q id='opCw7'></q></dir></style></legend>

          • <small id='opCw7'></small><noframes id='opCw7'>

                <tfoot id='opCw7'></tfoot>
                  <tbody id='opCw7'></tbody>
                • <bdo id='opCw7'></bdo><ul id='opCw7'></ul>
                  随着Php版本的不断升级,以前的excel读取和导出工具PhpExcel库中的一些语法已经不被识别,而PhpExcel库也停止了维护,现在都转移到另外一个升级版本PhpSpreadSheet库里面来。

                  以下是本人在近期开发一个简历的excel导出功能时写过的一些PhpSpreadSheet代码,供大家参考:

                  一、前置条件

                  安装PhpSpreadsheet,代码开始部分做如下声明:

                  header('Content-Type:text/html; charset=utf-8');
                  require 'PhpSpreadsheet-master/vendor/autoload.php';
                  use PhpOffice\PhpSpreadsheet\Spreadsheet;
                  use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
                  use PhpOffice\PhpSpreadsheet\Style\Color;
                  use PhpOffice\PhpSpreadsheet\Style\Alignment;
                  use PhpOffice\PhpSpreadsheet\Style\Border;
                  use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
                  use PhpOffice\PhpSpreadsheet\Style\Fill;
                  

                  二、excel导出流程

                  1.配置属性

                  代码如下:
                   

                          //设置excel属性
                          $objActSheet = $excel->getActiveSheet();
                          
                          //设置当前的sheet
                          $excel->setActiveSheetIndex(0);
                          
                          //设置sheet的name
                          $objActSheet->setTitle($sheetname);
                  
                          //根据有生成的excel多少列,$letter长度要大于等于这个值
                          $letter1 = array('A','B','C','D','E','F');
                  
                          //设置表头
                          $objActSheet->setCellValue("A1","编号");
                          $objActSheet->setCellValue("B1","地区");
                          $objActSheet->setCellValue("C1","企业名称");
                          $objActSheet->setCellValue("D1","岗位名称");
                          $objActSheet->setCellValue("E1","招聘人数");
                          $objActSheet->setCellValue("F1","联系电话");
                          $excel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
                          $excel->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
                          
                          //设置表头
                          for($i = 0;$i < count($letter1);$i++) 
                          {
                              for($j = 1; $j <=1; $j++)
                              {
                                  //设置表头字体样式
                                  $objActSheet->getStyle("$letter1[$i]$j")->getFont()->setName('微软雅黑');
                                  //设置表头字体大小
                                  $objActSheet->getStyle("$letter1[$i]$j")->getFont()->setSize(12);
                                  //设置表头字体是否加粗
                                  $objActSheet->getStyle("$letter1[$i]$j")->getFont()->setBold(true);
                                  //设置表头文字垂直居中
                                  $objActSheet->getStyle("$letter1[$i]$j")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
                                  //设置文字上下居中
                                  $objActSheet->getStyle($letter1[$i])->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
                                  //设置表头外的文字垂直居中
                                  $excel->setActiveSheetIndex(0)->getStyle($letter1[$i])->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
                                  
                                  
                                  
                              }
                          }
                          
                          //设置各列的宽度
                          $objActSheet->getColumnDimension($letter1[0])->setWidth(5);//编号
                          $objActSheet->getColumnDimension($letter1[1])->setWidth(20);//地区
                          $objActSheet->getColumnDimension($letter1[2])->setWidth(50);//企业名称
                          $objActSheet->getColumnDimension($letter1[3])->setWidth(50);//岗位名称
                          $objActSheet->getColumnDimension($letter1[4])->setWidth(16);//招聘人数
                          $objActSheet->getColumnDimension($letter1[5])->setWidth(16);//联系电话
                  
                          //设置边框
                          $style_array = array(
                          'borders' => array(
                          'allborders' => array(
                          'style' => Border::BORDER_THIN
                          )
                          ) );
                          $objActSheet->getStyle('A1:F1')->applyFromArray($style_array);
                  

                  2.设置数据

                  代码如下(示例)

                          $j = 2;
                          for ($i = 0;$i < count($data);$i++,$j++) 
                          {
                      	    
                      	    $objActSheet->setCellValue("A".$j,$data[$i]['编号']);
                      	    $objActSheet->setCellValue("B".$j,$data[$i]['地区']);
                      	    $objActSheet->setCellValue("C".$j,$data[$i]['企业名称']);
                      	    $objActSheet->setCellValue("D".$j,$data[$i]['岗位名称']);
                      	    $objActSheet->setCellValue("E".$j,$data[$i]['招聘人数']);
                      	    $objActSheet->setCellValue("F".$j,$data[$i]['联系电话']);
                          }
                  

                  该处使用的url网络请求的数据。

                  3.数据导出

                  代码如下(示例):

                          ob_end_clean();
                  
                          ob_start();
                          header('Content-Type: application/vnd.ms-excel');
                          
                          //下载的excel文件名称,为Excel5,后缀为xls,不过影响似乎不大
                          header('Content-Disposition: attachment;filename="' . $savefile . '.xlsx"'); 
                          header('Cache-Control: max-age=0');
                          // 用户下载excel
                          $objWriter = new Xlsx($excel);
                  
                          $objWriter->save('php://output');
                  
                  上一篇:PHP使用PhpSpreadsheet导出数据的详细操作 下一篇:没有了

                  相关文章

                      • <bdo id='Dkfzd'></bdo><ul id='Dkfzd'></ul>
                      <tfoot id='Dkfzd'></tfoot>

                    1. <legend id='Dkfzd'><style id='Dkfzd'><dir id='Dkfzd'><q id='Dkfzd'></q></dir></style></legend>

                      <small id='Dkfzd'></small><noframes id='Dkfzd'>

                      <i id='Dkfzd'><tr id='Dkfzd'><dt id='Dkfzd'><q id='Dkfzd'><span id='Dkfzd'><b id='Dkfzd'><form id='Dkfzd'><ins id='Dkfzd'></ins><ul id='Dkfzd'></ul><sub id='Dkfzd'></sub></form><legend id='Dkfzd'></legend><bdo id='Dkfzd'><pre id='Dkfzd'><center id='Dkfzd'></center></pre></bdo></b><th id='Dkfzd'></th></span></q></dt></tr></i><div id='Dkfzd'><tfoot id='Dkfzd'></tfoot><dl id='Dkfzd'><fieldset id='Dkfzd'></fieldset></dl></div>