phpexecel自己的采纳记录,PHP导出多少到表格的实例


$user_sql = “select * from `table`”;

一、PHPExcel简介

上面是总计的多少个利用形式

我发现以来的门类须要导出Excel表的页面很是的多,想来这一个也是咱们常用的效果了,现在自我得以很在行的导出了,不过记得及时祥和第三次导出时如故绕了有的弯路的,那么现在自己就来记录下自家本次用exshop框架项目下的导出(其实在不一致的框架下Excel的导出原理都是大概的)

$user_list = $db->fetchAll($user_sql);//数据库查询

PHPEXCEL提供了一多级的API,能够分析与生成excel,pdf之类的文档。

  include
‘PHPExcel.php’;


require_once dirname(__FILE__) .
‘/../../class/PHPExcel.php’;//加载phpexcel.php,根据自己项目标途径调整

PHPEXCEL即便强大,但是使用起来相对有些繁锁,如若需求输出较为复杂格式时,是一个正确的选料。可以到官方下载到源码。

  include ‘PHPExcel/Writer/Excel2007.php’;

前端

$objPHPExcel = new PHPExcel();

phpexecel自己的采纳记录,PHP导出多少到表格的实例。二、PHPEXCEL部分函数

  //或者include ‘PHPExcel/Writer/Excel5.php’; 用于输出.xls的

 <a href="javascript:;" id="export_all" class="coolbg">导出</a>

<script>
    //导出数据
    $('#export_all').click(function(){
        window.open('index.php?app=craft_order&act=export', '_blank');
    });
</script>

$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”)
->setLastModifiedBy(“Maarten Balliauw”)
->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”);

设置当前的工作簿,重临该工作簿对象:
$excelSheet = $excel->setActiveSheetIndex(0);

  创造一个excel

控制器
“`
// 导出多少
public function export() {
$result =
$this->_oaOrderModel->getAllOrderListForManager($this->store_id,
$orderSn=null, $buyer_id=null, $buyer_name=null, $consignee=null,
$phone=null, $company_name=null, $status=null, $s_time=null,
$e_time=null, $page=null, $listRows=null, $execl=true);
//那几个是赢得多少的代码-model里
$orderList = $result[‘orderList’];
if (!empty($orderList)) {
$j = 1;
$stmt = array();
foreach ($orderList as $val) {
$stmt[$j][‘网站ID’] = $val[‘store_id’];
$stmt[$j][‘订单新闻’] = $val[‘order_sn’];
$stmt[$j][‘商品信息’] = $val[‘inventory_sn_count_chinese’];
$stmt[$j][‘工艺接纳’] = $val[‘craft_count_chinese’];
$stmt[$j][‘商品总数量’] = $val[‘real_phpexecel自己的采纳记录,PHP导出多少到表格的实例。goods_total_count’];
$stmt[$j][‘提交日期’] = date(“Y-m-d H:i:s”,$val[‘add_time’]);
$stmt[$j][‘客户名称’] = $val[‘company_name’];
$stmt[$j][‘联系人’] = $val[‘consignee’];
$stmt[$j][‘联系情势’] = $val[‘phone_mob’];
$stmt[$j][‘订单成功率’] = $val[‘percentage_complete’];
$stmt[$j][‘订单状态’] = $val[‘statusChinese’];
$j++;
}
$current_path = dirname(FILE);
$home_path = dirname($current_path);
require_once ROOT_PATH . ‘/includes/libraries/PHPExcel.php’;
require_once ROOT_PATH .
‘/includes/libraries/PHPExcel/IOFactory.php’;
$objPHPExcel = new PHPExcel(); //这一个主意自己下载放到公共艺术里
$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”)
->setLastModifiedBy(“Maarten Balliauw”)
->setTitle(“Office 2007 XLSX Test Document”)
->setSubject(“Office 2007 XLSX Test Document”)
->setDescription(“Document for Office 2007 XLSX, generated using
PHP classes.”)
->setKeywords(“office 2007 openxml php”)
->setCategory(“Test result file”);

//设置标题名称
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue(‘A1’, ‘编号’)
->setCellValue(‘B1’, ‘在线人数’)
->setCellValue(‘C1’, ‘手机’)

联合单元格,再次来到该单元格对象,以下示例即联合A列第一行与第二行所在单元格:

  $objPHPExcel = new PHPExcel();

        // 行高
        for ($i = 2; $i <= count($stmt); $i++) {
            $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(22);
        }
        foreach ($stmt as $fid => $fval) {
            if ($fid == 1) {
                $key = 0;
                foreach ($fval as $title => $first) {
                    //如果一级标题
                    $objPHPExcel->getActiveSheet()->setCellValue(chr($key + 65) . '1', $title);
                    $objPHPExcel->getActiveSheet()->getStyle(chr($key + 65) . '1')->getFont()->setBold(true);       // 加粗
                    $key ++;
                }
            }
            $cid = 0;
            $row_id = $fid + 1;
            foreach ($fval as $cval) {
                $objPHPExcel->getActiveSheet()->setCellValue(chr($cid + 65) . (string) ($row_id), $cval);
                $cid++;
            }
        }
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('Excel表');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //$objWriter->save('订单列表详细.xls');
        //输出到浏览器
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="订单列表.xls"');
        header("Content-Transfer-Encoding: binary");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
}

```

->setCellValue(‘D1’, ‘记录时间’);  

 代码如下

  保存excel—2007格式

在意:PHPExcel(); //那几个点子自己下载放到公共艺术里

//循环每个数据,如果赶上手机号码的数目导出,能够在相应的数码前、后添加一个空格,达到将数据变成字符串的艺术,令execl不是讲其当成科学数据法的不二法门显示出来
foreach ($user_list as $k => $v) {
$num=$k+2;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue(‘A’.$num, $v[‘id’])
->setCellValue(‘B’.$num, $v[‘online’])

$excelSheet->mergeCells(‘A1:A2’);

  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

成果图

->setCellValue(‘C’.$num, ‘ ‘.$v[‘phone_num’].’ ‘)
->setCellValue(‘D’.$num, date(‘Y-m-d H:i:s’,$v[‘dateline’]));
}

设置单元格的值,参数:单元格名称,值:
$excelSheet->setCellValue(‘A1’, ‘字符串内容’);
$excelSheet->setCellValue(‘A2’, 26); //数值
$excelSheet->setCellValue(‘A3’, true); //布尔值
$excelSheet->setCellValue(‘A4’, ‘=SUM(A2:A2)’); //公式

  //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
非2007格式

www.5929.com 1

$objPHPExcel->getActiveSheet()->getColumnDimension(‘C’)->setWidth(20);//对某列单元格设置宽度

phpexcel用法介绍

  $objWriter->save(”xxx.xlsx”);

www.5929.com 2

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle(‘sheet1’);

 

  直接出口到浏览器

www.5929.com 3

// Set active sheet index to the first sheet, so Excel opens this as the
first sheet
$objPHPExcel->setActiveSheetIndex(0);

 代码如下

  $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);


$time_name =
‘在线人数’.date(‘YmdHis’,time()).’.xls’;//设置下载时文件的默许名

include ‘PHPExcel.php’;

  header(”Pragma: public”);

心得

// Redirect output to a client’s web browser (Excel5)
header(‘Content-Type: application/vnd.ms-excel’);
header(“Content-Disposition: attachment;filename=’$time_name'”);
header(‘Cache-Control: max-age=0’);
// If you’re serving to IE 9, then the following may be needed
header(‘Cache-Control: max-age=1’);

  include ‘PHPExcel/Writer/Excel2007.php’;

  header(”Expires: 0″);

有时蒙受这几个难题可以多研讨,多看看它的规律,原理驾驭了下次做别的的也是会的,但最重大的是要知道做笔录,大家的记得并没有想象的那么好

// If you’re serving to IE over SSL, then the following may be needed
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

  //或者include ‘PHPExcel/Writer/Excel5.php’; 用于输出.xls的

  header(”Cache-Control:must-revalidate, post-check=0, pre-check=0″);

注:文章来源雨中笑记录实习期遭遇的题材与体会,转发请表达原文

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
‘Excel5’);
$objWriter->save(‘php://output’);
die;

  创制一个excel

  header(”Content-Type:application/force-download”);

  $objPHPExcel = new PHPExcel();

  header(”Content-Type:application/vnd.ms-execl”);

  保存excel—2007格式

  header(”Content-Type:application/octet-stream”);

  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

  header(”Content-Type:application/download”);;

  //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
非2007格式

  header(’Content-Disposition:attachment;filename=”resume.xls”‘);

  $objWriter->save(”xxx.xlsx”);

  header(”Content-Transfer-Encoding:binary”);

  直接出口到浏览器

  $objWriter->save(’php://output’);

  $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

  ——————————————————————————————————————–

  header(”Pragma: public”);

  设置excel的属性:

  header(”Expires: 0″);

  创建人

  header(”Cache-Control:must-revalidate, post-check=0, pre-check=0″);

  $objPHPExcel->getProperties()->setCreator(”Maarten Balliauw”);

  header(”Content-Type:application/force-download”);

  最终修改人

  header(”Content-Type:application/vnd.ms-execl”);

  $objPHPExcel->getProperties()->setLastModifiedBy(”Maarten
Balliauw”);

  header(”Content-Type:application/octet-stream”);

  标题

  header(”Content-Type:application/download”);;

  $objPHPExcel->getProperties()->setTitle(”Office 2007 XLSX Test
Document”);

  header(’Content-Disposition:attachment;filename=”resume.xls”‘);

  题目

  header(”Content-Transfer-Encoding:binary”);

  $objPHPExcel->getProperties()->setSubject(”Office 2007 XLSX
Test Document”);

  $objWriter->save(’php://output’);

  描述

  

  $objPHPExcel->getProperties()->setDescription(”Test document
for Office 2007 XLSX, generated using PHP classes.”);

——————————————————————————————————————–

  关键字

  设置excel的属性:

  $objPHPExcel->getProperties()->setKeywords(”office
2007 openxml php”);

  

  种类

 代码如下

  $objPHPExcel->getProperties()->setCategory(”Test result
file”);

创建人

  ——————————————————————————————————————–

  $objPHPExcel->getProperties()->setCreator(”Maarten Balliauw”);

  设置当前的sheet

  最终修改人

  $objPHPExcel->setActiveSheetIndex(0);

  $objPHPExcel->getProperties()->setLastModifiedBy(”Maarten
Balliauw”);

  设置sheet的name

  标题

  $objPHPExcel->getActiveSheet()->setTitle(’Simple’);

  $objPHPExcel->getProperties()->setTitle(”Office 2007 XLSX Test
Document”);

  设置单元格的值

  题目

  $objPHPExcel->getActiveSheet()->setCellValue(’A1′, ‘String’);

  $objPHPExcel->getProperties()->setSubject(”Office 2007 XLSX
Test Document”);

  $objPHPExcel->getActiveSheet()->setCellValue(’A2′, 12);

  描述

  $objPHPExcel->getActiveSheet()->setCellValue(’A3′, true);

  $objPHPExcel->getProperties()->setDescription(”Test document
for Office 2007 XLSX, generated using PHP classes.”);

  $objPHPExcel->getActiveSheet()->setCellValue(’C5′,
‘=SUM(C2:C4)’);

  关键字

  $objPHPExcel->getActiveSheet()->setCellValue(’B8′,
‘=MIN(B2:C5)’);

  $objPHPExcel->getProperties()->setKeywords(”office 2007
openxml php”);

  合并单元格

  种类

  $objPHPExcel->getActiveSheet()->mergeCells(’A18:E22′);

  $objPHPExcel->getProperties()->setCategory(”Test result
file”);

  分离单元格

  ——————————————————————————————————————–

  $objPHPExcel->getActiveSheet()->unmergeCells(’A28:B28′);

  设置当前的sheet

生成Excel常用艺术

  $objPHPExcel->setActiveSheetIndex(0);

<?
//设置PHPExcel类库的include path
set_include_path(‘.’. PATH_SEPARATOR .
‘D:ZealPHP_LIBS’ . PATH_SEPARATOR .
get_include_path());

  设置sheet的name

require_once ‘PHPExcel.php’;

  $objPHPExcel->getActiveSheet()->setTitle(’Simple’);

// uncomment
////require_once ‘PHPExcel/Writer/Excel5.php’; // 用于其他低版本xls
// or
////require_once ‘PHPExcel/Writer/Excel2007.php’; // 用于 excel-2007
格式

  设置单元格的值

// 创立一个拍卖目标实例
$objExcel = new PHPExcel();

  $objPHPExcel->getActiveSheet()->setCellValue(’A1′, ‘String’);

// 创制文件格式写入对象实例, uncomment
////$objWriter = new PHPExcel_Writer_Excel5($objExcel); //
用于其余版本格式
// or
////$objWriter = new PHPExcel_Writer_Excel2007($objExcel); // 用于
2007 格式
//$objWriter->setOffice2003Compatibility(true);

  $objPHPExcel->getActiveSheet()->setCellValue(’A2′, 12);

//*************************************
//设置文档基本特性
$objProps =
$objExcel->getProperties();
$objProps->setCreator(“Zeal Li”);
$objProps->setLastModifiedBy(“Zeal Li”);
$objProps->setTitle(“Office XLS Test Document”);
$objProps->setSubject(“Office XLS Test Document, Demo”);
$objProps->setDescription(“Test document, generated by PHPExcel.”);
$objProps->setKeywords(“office excel PHPExcel”);
$objProps->setCategory(“Test”);

  $objPHPExcel->getActiveSheet()->setCellValue(’A3′, true);

//*************************************
//设置当前的sheet索引,用于后续的始末操作。
//一般唯有在利用五个sheet的时候才要求呈现调用。
//缺省气象下,PHPExcel会自动创设首个sheet被安装SheetIndex=0
$objExcel->setActiveSheetIndex(0);

  $objPHPExcel->getActiveSheet()->setCellValue(’C5′,
‘=SUM(C2:C4)’);

$objActSheet = $objExcel->getActiveSheet();

  $objPHPExcel->getActiveSheet()->setCellValue(’B8′,
‘=MIN(B2:C5)’);

//设置当前活动sheet的名号
$objActSheet->setTitle(‘测试Sheet’);

  合并单元格

//*************************************
//设置单元格内容
//
//由PHPExcel依据传入内容自动判断单元格内容类型
$objActSheet->setCellValue(‘A1’, ‘字符串内容’); // 字符串内容
$objActSheet->setCellValue(‘A2’, 26); // 数值
$objActSheet->setCellValue(‘A3’, true); // 布尔值
$objActSheet->setCellValue(‘A4’, ‘=SUM(A2:A2)’); // 公式

  $objPHPExcel->getActiveSheet()->mergeCells(’A18:E22′);

//显式指定内容类型
$objActSheet->setCellValueExplicit(‘A5’, ‘847475847857487584’,
PHPExcel_Cell_DataType::TYPE_STRING);

  分离单元格

//合并单元格
$objActSheet->mergeCells(‘B1:C22’);

  $objPHPExcel->getActiveSheet()->unmergeCells(’A28:B28′);

//分离单元格
$objActSheet->unmergeCells(‘B1:C22’);

 三、PHPEXCEL举例应用

//*************************************
//设置单元格样式
//

全部代码如下(值得注意的是表头用了$orderCellData记录了每个商户编号的逐一,为了在表体把相应的数额取出):

//设置宽度
$objActSheet->getColumnDimension(‘B’)->setAutoSize(true);
$objActSheet->getColumnDimension(‘A’)->setWidth(30);

 代码如下

$objStyleA5 = $objActSheet->getStyle(‘A5’);

<<?php

//设置单元格内容的数字格式。
//
//如若采纳了 PHPExcel_Writer_Excel5 来扭转内容的话,
//那里要求专注,在 PHPExcel_Style_NumberFormat 类的 const 变量定义的
//种种自定义格式化措施中,其他种类都可以正常使用,但当setFormatCode
//为 FORMAT_NUMBER 的时候,实际出来的作用被没有把格式设置为”0″。须求
//修改 PHPExcel_Writer_Excel5_Format 类源代码中的 getXf($style)
方法,
//在 if ($this->_BIFF_version == 0x0500) {
(第363行附近)前边扩充一
//行代码:
//if($ifmt === ‘0’) $ifmt = 1;
//
//设置格式为PHPExcel_Style_NumberFormat::FORMAT_NUMBER,防止某些大数字
//被使用正确记数格局显示,协作下边的 setAutoSize
方法可以让每一行的始末
//都按原来内容总体突显出来。
$objStyleA5
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

   

//设置字体
$objFontA5 = $objStyleA5->getFont();
$objFontA5->setName(‘Courier New’);
$objFontA5->setSize(10);
$objFontA5->setBold(true);
$objFontA5->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objFontA5->getColor()->setARGB(‘FF999999’);

    require_once ‘../../../libs/PHPExcel/Classes/PHPExcel.php’;

//设置对齐方式
$objAlignA5 = $objStyleA5->getAlignment();
$objAlignA5->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objAlignA5->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

    require_once
‘../../../libs/PHPExcel/Classes/PHPExcel/Writer/Excel5.php’;

//设置边框
$objBorderA5 = $objStyleA5->getBorders();
$objBorderA5->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getTop()->getColor()->setARGB(‘FFFF0000’); //
color
$objBorderA5->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    include_once
‘../../../libs/PHPExcel/Classes/PHPExcel/IOFactory.php’;

//设置填充颜色
$objFillA5 = $objStyleA5->getFill();
$objFillA5->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objFillA5->getStartColor()->setARGB(‘FFEEEEEE’);

    include ‘../common/config.php’;

//从指定的单元格复制样式音讯.
$objActSheet->duplicateStyle($objStyleA5, ‘B1:C22’);

    // 创立一个拍卖目的实例(此目的对于2003 2007是如出一辙的)

//*************************************
//添加图片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘ZealImg’);
$objDrawing->setDescription(‘Image inserted by Zeal’);
$objDrawing->setPath(‘./zeali.net.logo.gif’);
$objDrawing->setHeight(36);
$objDrawing->setCoordinates(‘C23’);
$objDrawing->setOffsetX(10);
$objDrawing->setRotation(15);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(36);
$objDrawing->setWorksheet($objActSheet);

    $objExcel = new PHPExcel();

//添加一个新的worksheet
$objExcel->createSheet();
$objExcel->getSheet(1)->setTitle(‘测试2’);

   

//保养单元格
$objExcel->getSheet(1)->getProtection()->setSheet(true);
$objExcel->getSheet(1)->protectCells(‘A1:C22’, ‘PHPExcel’);

    //设置属性(那段代码非亲非故首要,其中的情节可以替换为您须求的)

//*************************************
//输出内容
//
$outputFileName = “output.xls”;
//到文件
////$objWriter->save($outputFileName);
//or
//到浏览器
////header(“Content-Type: application/force-download”);
////header(“Content-Type: application/octet-stream”);
////header(“Content-Type: application/download”);
////header(‘Content-Disposition:inline;filename=”‘.$outputFileName.'”‘);
////header(“Content-Transfer-Encoding: binary”);
////header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
www.5929.com,////header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”);
////header(“Cache-Control: must-revalidate, post-check=0,
pre-check=0”);
////header(“Pragma: no-cache”);
////$objWriter->save(‘php://output’);

    $objExcel->getProperties()->setCreator(“office 2003 excel”);

?>

    $objExcel->getProperties()->setLastModifiedBy(“office 2003
excel”);

    $objExcel->getProperties()->setTitle(“Office 2003 XLS Test
Document”);

    $objExcel->getProperties()->setSubject(“Office 2003 XLS Test
Document”);

    $objExcel->getProperties()->setDescription(“Test document for
Office 2003 XLS, generated using PHP classes.”);

    $objExcel->getProperties()->setKeywords(“office 2003 openxml
php”);

    $objExcel->getProperties()->setCategory(“Test result file”);

   

    //开头拍卖数据(索引从0起始)

    $objExcel->setActiveSheetIndex(0);

 

    $conn =
mssql_connect($config[‘mssql’][‘host’],$config[‘mssql’][‘user’],$config[‘mssql’][‘password’]);

    mssql_select_db($config[‘mssql’][‘dbname’],$conn);

   

    $tm=$_REQUEST[‘tm’];

   

    $sql = “exec HNow05_getTTSpace ”,'”.$tm.”‘,”,1″;

    $sql=mb_convert_encoding($sql,’GBK’,’UTF-8′);

    $res=mssql_query($sql);

 

    $i=0;

    $k = array(‘站码’,’站名’,’河系’,’来报时间’,’水位’,’水势’);

    $count = count($k);

    $arrs = array(‘A’,’B’,’C’,’D’,’E’,’F’);

    //添加表头

    for($i=0;$i<$count;$i++){

       $objExcel->getActiveSheet()->setCellValue($arrs[$i].”1″,
“$k[$i]”);

      

    }

   

    /*——–从数据库读取数据——-*/

    $i=0;

    while($arr=mssql_fetch_array($res))

    {

       $stcd = $arr[“STCD”];

       $stnm = $arr[“STNM”];

       $rvnm = $arr[“RVNM”];

       $tm= $arr[“TM”];

       $tdz= $arr[“TDZ”];

        $tdptn= $arr[“TDPTN”];

       if($tdptn==’6′){

           $tdptn=’平’;

       }else if($tdptn==’5′){

           $tdptn=’涨’;

       }else if($tdptn==’4′){

           $tdptn=’落’;

       }

 

       $u1=$i+2;

       $stnm=iconv(“GBK”,”utf-8″,$stnm);

       $rvnm=iconv(“GBK”,”utf-8″,$rvnm);

       $tm=iconv(“GBK”,”utf-8″,$tm);

 

       /*———-写入内容————-*/

       $objExcel->getActiveSheet()->setCellValue(‘a’.$u1,
“$stcd”);

       $objExcel->getActiveSheet()->setCellValue(‘b’.$u1,
“$stnm”);

       $objExcel->getActiveSheet()->setCellValue(‘c’.$u1,
“$rvnm”);

       $objExcel->getActiveSheet()->setCellValue(‘d’.$u1, “$tm”);

       $objExcel->getActiveSheet()->setCellValue(‘e’.$u1, “$tdz”);

       $objExcel->getActiveSheet()->setCellValue(‘f’.$u1,
“$tdptn”);

 

       $i++;

    }

 

    /*———-设置单元格边框和颜料————-*/

    $rows = mssql_num_rows($res);

    for($i=0;$i<($rows+1);$i++){

       for($j=0;$j<$count;$j++){

           $a = $i+1;

          
$objExcel->getActiveSheet()->getStyle($arrs[$j].$a)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

          
$objExcel->getActiveSheet()->getStyle($arrs[$j].$a)->getBorders()->getAllBorders()->getColor()->setARGB(‘FF00BBcc’);

           //水平居中

          
$objExcel->getActiveSheet()->getStyle($arrs[$j].$a)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

       }

    }

 

    // 高置列的拉长率

   
$objExcel->getActiveSheet()->getColumnDimension(‘A’)->setWidth(10);

   
$objExcel->getActiveSheet()->getColumnDimension(‘B’)->setWidth(15);

   
$objExcel->getActiveSheet()->getColumnDimension(‘C’)->setWidth(15);

   
$objExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(20);

   
$objExcel->getActiveSheet()->getColumnDimension(‘E’)->setWidth(10);

   
$objExcel->getActiveSheet()->getColumnDimension(‘F’)->setWidth(10);

   

    // 设置页眉和页脚。如果没有两样的标题奇数/即便是运用单头假定.

   
$objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(‘&L&BPersonal
cash register&RPrinted on &D’);

   
$objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(‘&L&B’
. $objExcel->getProperties()->getTitle() . ‘&RPage &P of &N’);

   

    // 设置页方向和规模

   
$objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);

   
$objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

   

    // 重命名表

    $objExcel->getActiveSheet()->setTitle(‘实时潮汐景况’);

   

    // Set active sheet index to the first sheet, so Excel opens this as
the first sheet

    $objExcel->setActiveSheetIndex(0);

   

    // Redirect output to a client’s web browser
(Excel5)保存为excel2003格式

    //设置Excel的名字

    $excelName = ‘实时潮汐情状(‘.$tm.’)’;

    //$excelName = ‘Excel_’.date(“YmdHis”);

    header(‘Content-Type: application/vnd.ms-excel’);

    header(‘Cache-Control: max-age=0’);

    header( ‘Content-Disposition: attachment; filename=’.iconv(“utf-8”,
“GBK”, $excelName).’.xls’);

    $objWriter = PHPExcel_IOFactory::createWriter($objExcel, ‘Excel5’);

    $objWriter->save(‘php://output’);

    exit;

?>

Leave a Comment.