zoukankan      html  css  js  c++  java
  • 导出excel

      /**
         * @DESC 数据导出
         * @notice max column is z OR 26,overiload will be ignored
         * @notice
         * @example
         *  $data = [[1, "小明", "25"]];
         *  $header = ["id", "姓名", "年龄"];
         *  Myhelpers::exportData($data, $header);
         * @return void, Browser direct output
         */
        public static function exportData($data, $header, $title = "test", $filename = "data")
        {
            if (!is_array($data) || !is_array($header)) return false;
            $objPHPExcel = new PHPExcel();
    
            // Set properties
            $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
            $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
            $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
            $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
            $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
            // Add some data
            $obj = $objPHPExcel->setActiveSheetIndex(0);
            //添加头部
            $hk = 0;
            foreach ($header as $k => $v) {
                $colum = PHPExcel_Cell::stringFromColumnIndex($hk);
                $obj->setCellValue($colum . "1", $v);
                $hk += 1;
            }
            $column = 2;
            $objActSheet = $objPHPExcel->getActiveSheet();
            foreach ($data as $key => $rows)  //行写入
            {
                $span = 0;
                foreach ($rows as $keyName => $value) // 列写入
                {
                    $j = PHPExcel_Cell::stringFromColumnIndex($span);
                    // 前面加一个空格 会将值变成字符串
                    $objActSheet->setCellValue($j . $column, ' '.$value);
                    $span++;
                }
                $column++;
            }
            // Rename sheet
            if ($title) {
                $objActSheet->setTitle($title);
            }
            // Save
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header("Pragma:public");
    //        header('Content-type: application/vnd.ms-excel');
            header("Content-Type:application/vnd.ms-excel;name="{$filename}.xlsx"");
    //        header("Content-Type:application/x-msexecl;name="{$filename}.xls"");
            header("Content-Disposition:inline;filename="{$filename}.xlsx"");
            $objWriter->save("php://output");
        }
  • 相关阅读:
    element-ui 中 el-table 根据scope.row行数据变化动态显示行内控件
    vue.js 父组件主动获取子组件的数据和方法、子组件主动获取父组件的数据和方法
    把json1赋值给json2,修改json2的属性,json1的属性也一起变化
    win10下当前目录右键添加CMD快捷方式
    element-ui
    vscode 头部注释插件
    IE浏览器new Date()带参返回NaN解决方法
    常用css
    使用DataGridView控件显示数据
    第四章 ADO.NET
  • 原文地址:https://www.cnblogs.com/lglblogadd/p/7442943.html
Copyright © 2011-2022 走看看