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

        <bdo id='nwOoo'></bdo><ul id='nwOoo'></ul>

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

        PHP使用PhpSpreadsheet导出数据的详细操作

        PHP使用PhpSpreadsheet导出数据的详细操作,这篇文章主要介绍了PhpSpreadsheet设置单元格常用操作汇总 安装 composer require phpoffice/phpspreadsheet 使用 #在控制中引入use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xl

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

            1. <tfoot id='GgqGm'></tfoot>

                  PHP使用PhpSpreadsheet导出数据的详细操作,这篇文章主要介绍了PhpSpreadsheet设置单元格常用操作汇总

                  安装
                  composer require phpoffice/phpspreadsheet

                  使用
                  #在控制中引入 
                  use PhpOffice\PhpSpreadsheet\Spreadsheet;
                  use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
                  封装导出方法
                  /**
                   * excel表格导出
                   * @param string $fileName 文件名称 $name='测试导出';
                   * @param array $headArr 表头名称 $header=['表头A','表头B'];
                   * @param array $data 要导出的数据 $data=[['测试','测试'],['测试','测试']]
                   * @param bool $auto 是否开启根据表头自适应宽度 默认开启
                   * @author php  */
                  function excelExport($fileName = '', $headArr = [], $data = [], $auto = true)
                  {
                  
                      $fileName .= ".xlsx";
                      $objPHPExcel = new Spreadsheet();
                      $objPHPExcel->getProperties();
                      $key = ord("A"); // 设置表头
                      $key2 = ord("@"); //    超过26列会报错的解决方案
                  
                      // 居中
                      $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
                      $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
                  
                      // 设置表头
                      foreach ($headArr as $v) {
                          // 超过26列会报错的解决方案
                          if ($key > ord("Z")) {
                              $key2 += 1;
                              $key = ord("A");
                              $colum = chr($key2) . chr($key); //超过26个字母时才会启用
                          } else {
                              if ($key2 >= ord("A")) {
                                  $colum = chr($key2) . chr($key);
                              } else {
                                  $colum = chr($key);
                              }
                          }
                          // 写入表头
                          $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
                          // 自适应宽度
                          if ($auto) {
                              // $len = strlen(iconv('utf-8','gb2312',$v));//会报错
                              $len = strlen(iconv('utf-8', 'gbk', $v));
                              $objPHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth($len + 5);
                          }
                          $key += 1;
                      }
                  
                      $column = 2;
                      $objActSheet = $objPHPExcel->getActiveSheet();
                      // 写入行数据
                      foreach ($data as $key => $rows) {
                          $span = ord("A");
                          $span2 = ord("@");
                          // 按列写入
                          foreach ($rows as $keyName => $value) {
                              // 超过26列会报错的解决方案
                              if ($span > ord("Z")) {
                                  $span2 += 1;
                                  $span = ord("A");
                                  $tmpSpan = chr($span2) . chr($span); //超过26个字母时才会启用
                              } else {
                                  if ($span2 >= ord("A")) {
                                      $tmpSpan = chr($span2) . chr($span);
                                  } else {
                                      $tmpSpan = chr($span);
                                  }
                              }
                              // 写入数据
                              $objActSheet->setCellValue($tmpSpan . $column, $value);
                              $span++;
                          }
                          $column++;
                      }
                  
                      // 自动加边框
                      $styleThinBlackBorderOutline = array(
                          'borders' => array(
                              'allborders' => array( //设置全部边框
                                  'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //粗的是thick
                              ),
                  
                          ),
                      );
                      $objPHPExcel->getActiveSheet()->getStyle('A1:' . $colum . --$column)->applyFromArray($styleThinBlackBorderOutline);
                      // 重命名表
                      // $fileName = iconv("utf-8", "gb2312", $fileName);
                      $fileName = iconv("utf-8", "gbk", $fileName);
                      // 设置活动单指数到第一个表,所以Excel打开这是第一个表
                      $objPHPExcel->setActiveSheetIndex(0);
                      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                      header("Content-Disposition: attachment;filename=$fileName");
                      header('Cache-Control: max-age=0');
                      // $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
                      $writer = new Xlsx($objPHPExcel);
                      $writer->save('php://output'); // 文件通过浏览器下载
                      exit();
                  }

                  PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到。

                  在调试设置时,确保引入了正确的文件并实例化。
                   
                  use PhpOffice\PhpSpreadsheet\Spreadsheet;
                  $spreadsheet = new Spreadsheet();
                  $worksheet = $spreadsheet->getActiveSheet();
                  字体
                  第1行代码将A7至B7两单元格设置为粗体字,Arial字体,10号字;第2行代码将B1单元格设置为粗体字。
                  $spreadsheet->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true)->setName('Arial')
                  ->setSize(10);;
                  $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
                  颜色
                  将文字颜色设置为红色。
                  $spreadsheet->getActiveSheet()->getStyle('A4')
                  ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
                  图片
                  可以将图片加载到Excel中。
                  $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                  $drawing->setName('Logo');
                  $drawing->setDescription('Logo');
                  $drawing->setPath('./images/officelogo.jpg');
                  $drawing->setHeight(36);
                  列宽

                  将A列宽度设置为30(字符)。
                  $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(30);

                  如果需要自动计算列宽,可以这样:
                  $spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);

                  设置默认列宽为12。
                  $spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);
                  行高
                  设置第10行行高为100pt。
                  $spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100);

                  设置默认行高。
                   
                  $spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);

                  **对齐
                  **
                  将A1单元格设置为水平居中对齐。
                  $styleArray = [
                    'alignment' => [
                      'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                    ],
                  ];
                  $worksheet->getStyle('A1')->applyFromArray($styleArray);

                  合并
                  将A18到E22合并为一个单元格。
                  $spreadsheet->getActiveSheet()->mergeCells('A18:E22');
                  拆分
                  将合并后的单元格拆分。
                  $spreadsheet->getActiveSheet()->unmergeCells('A18:E22');

                  边框
                  将B2至G8的区域添加红色边框。
                  $styleArray = [
                    'borders' => [
                      'outline' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                        'color' => ['argb' => 'FFFF0000'],
                      ],
                    ],
                  ];
                  $worksheet->getStyle('B2:G8')->applyFromArray($styleArray);

                  工作表标题

                  设置当前工作表标题。
                  $spreadsheet->getActiveSheet()->setTitle('Hello');

                  日期时间
                  设置日期格式。
                  $spreadsheet->getActiveSheet()
                  ->setCellValue('D1', '2018-06-15');
                  $spreadsheet->getActiveSheet()->getStyle('D1')
                  ->getNumberFormat()
                  ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2);
                  换行
                  使用\n进行单元格内换行,相当于(ALT+"Enter")。
                  $spreadsheet->getActiveSheet()->getCell('A4')->setValue("hello\nworld");
                  $spreadsheet->getActiveSheet()->getStyle('A4')->getAlignment()->setWrapText(true);

                  使用函数

                  使用SUM计算B5到C5之间单元格的总和。其他函数同理:最大数(MAX),最小数(MIN),平均值(AVERAGE)。
                   
                  $spreadsheet->getActiveSheet()
                  ->setCellValue('B7', '=SUM(B5:C5)');

                  设置文档属性

                  可以设置Excel文档属性。
                  $spreadsheet->getProperties()
                  ->setCreator("Helloweba") //作者
                  ->setLastModifiedBy("Yuegg") //最后修改者
                  ->setTitle("Office 2007 XLSX Test Document") //标题
                  ->setSubject("Office 2007 XLSX Test Document") //副标题
                  ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") //描述
                  ->setKeywords("office 2007 openxml php") //关键字
                  ->setCategory("Test result file"); //分类
                  此外,除了提供丰富的Excel文件处理接口外,PhpSpreadshee还提供了CSV,PDF,HTML以及XML等文件处理接口。
                  更多使用设置请参照官网文档:​ ​https://phpspreadsheet.readthedocs.io/en/stable/。​​
                  导入:
                  <?php
                  require 'vendor/autoload.php';
                  $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('tmp.xlsx');
                  $sheet = $spreadsheet->getActiveSheet();
                  $data = $sheet->toArray();
                  var_dump($data);
                  导出:
                  <?php
                  require 'vendor/autoload.php';
                  $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
                  $sheet = $spreadsheet->getActiveSheet();
                  $sheet->getDefaultColumnDimension()->setWidth(40);
                  $sheet->getColumnDimensionByColumn(2)->setWidth(100);
                   
                  $sheet->setCellValue('A2', 'Hello World !');
                  for ($i = 1; $i < 10; $i++) {
                    $sheet->setCellValueByColumnAndRow($i, 1, 'Col'.$i);
                  }
                   
                  $fileName = '01simple.xlsx';
                  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                  header('Content-Disposition: attachment;filename="'. $fileName .'"');
                  header('Cache-Control: max-age=0');
                  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                  header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
                  header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
                  header('Pragma: public'); // HTTP/1.0 
                  $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
                  $writer->save('php://output');
                  exit;
                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  随着Php版本的不断升级,以前的excel读取和导出工具PhpExcel库中的一些语法已经不被识别,而PhpExcel库也停止了维护,现在都转移到另外一个升级版本PhpSpreadSheet库里面来。 以下是本人在近期开发一个简历的excel导出功能时写过的一些PhpSpreadSheet代码,供
                  在 PHP 中导出 Excel 文件可以使用多种库,其中最流行的是 PhpSpreadsheet。这个库是由 PHPExcel 演变而来的,功能强大且适用于各种 Excel 操作。下面是一个简单的示例,展示如何使用 PhpSpreadsheet 库来创建和导出一个 Excel 文件。 1. 安装 PhpSpreadsheet
                  php判断一个变量是否为正整数,其实方法还是很多的,下面列举了三种方法,希望可以帮大家。 方法1: $keyword = '10'; // 0 1.1 1if(preg_match("/^[1-9][0-9]*$/",$keyword)){ echo "是正整数!"; exit();} 方法2: if ((floor($jp_total) - $jp_total) !==0
                  为了提供API接口,我们常常在读取数据库后,将数据转换为数组,通过json_encode转为JSON,即可满足使用需要。现将代码粘帖如下: 读取一条记录,转为数组并输出JSON include("../../db/conn.php");//数据库连接;echo "pre";//数据库读取后,直接转换为数组
                  输出字符串的数据: $sql="select * from student";//sql语句$res=$dbquery($sql);$arr=$res-fetch_all();//把查询所有数据提取出来,放到二维数组中$str='';//定义一个空变量foreach($arr as $k=$v){//遍历查询的数据 $str.=implode(',',$v).',';//用逗号把每
                  以下是“学习php开源项目的源码指南”的完整攻略:
                1. <i id='vzAyR'><tr id='vzAyR'><dt id='vzAyR'><q id='vzAyR'><span id='vzAyR'><b id='vzAyR'><form id='vzAyR'><ins id='vzAyR'></ins><ul id='vzAyR'></ul><sub id='vzAyR'></sub></form><legend id='vzAyR'></legend><bdo id='vzAyR'><pre id='vzAyR'><center id='vzAyR'></center></pre></bdo></b><th id='vzAyR'></th></span></q></dt></tr></i><div id='vzAyR'><tfoot id='vzAyR'></tfoot><dl id='vzAyR'><fieldset id='vzAyR'></fieldset></dl></div>
                  <legend id='vzAyR'><style id='vzAyR'><dir id='vzAyR'><q id='vzAyR'></q></dir></style></legend>

                    1. <tfoot id='vzAyR'></tfoot>

                        <bdo id='vzAyR'></bdo><ul id='vzAyR'></ul>

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

                            <tbody id='vzAyR'></tbody>