用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

用PHPExcel导出excel

2014-12-03 作者: php源代码大全举报

[php]代码库

vendor("PHPExcel176.PHPExcel");
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->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");
        //set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
        //设置行高度
        //$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
        //$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
        //set font size bold
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        //设置水平居中
        //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //合并cell
        //$objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        //set font size bold
        //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);����ˮƽ����
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //�ϲ�cell
        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        // set table header content
        $objPHPExcel->setActiveSheetIndex(0)
           ->setCellValue('A1', iconv("utf-8","gb2312","订单数据汇总  时间:'.date('Y-m-d H:i:s')"))
            ->setCellValue('A2', iconv("utf-8","gb2312","订单ID"))
            ->setCellValue('B2', iconv("utf-8","gb2312","下单人"))
            ->setCellValue('C2', iconv("utf-8","gb2312","客户名称"))
            ->setCellValue('D2', iconv("utf-8","gb2312","下单时间"))
            ->setCellValue('E2', iconv("utf-8","gb2312","需求机型"))
            ->setCellValue('F2', iconv("utf-8","gb2312","需求数量"));
        // Miscellaneous glyphs, UTF-8
      for($i=0;$i<count($OrdersData)-1;$i++){
            $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['id']));
            $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['reid']));
            $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['topid']));
            //$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), iconv('utf-8','gb2312',toDate($OrdersData[$i]['typename']))); //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;common.php&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;
            $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['typename']));
            $objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['typelink']));
            $objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+3),  iconv("utf-8","gb2312",$OrdersData[$i]['keyword']));
            //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':F'.($i+3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':F'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            //$objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
        }
        //  sheet
        $objPHPExcel->getActiveSheet()->setTitle( iconv("utf-8","gb2312",'订单汇总表'));
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        // excel头参数
        header('Content-Type: application/vnd.ms-excel; charset=utf-8');
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream');
        header('Content-Type: application/download');
        //header('Content-type:charset=utf-8);
        //header('Content-Transfer-Encoding: utf-8');
        header('Content-Disposition: attachment;filename='.iconv("utf-8","gb2312","订单汇总".date('Ymd-His').".xls"));  //日期为文件名后缀
        //header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'excel5');  //excel5为xls格式,excel2007为xlsx格式
        $objWriter->save('php://output');
//该片段来自于http://yuncode.net


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...