利用PHPExcel转Excel饼图
PHPExcel处理Excel档真是个强大的工具,我有张报表,带饼图,需要转成Excel, PHPExcel有一个相关的例子,参考并修改后实现了这个效果,
可以让用户在点击下载过程中生成和下载Excel档,并在Excel中档生成饼图。
对其例子主要做了两方面的修改:
1. 改成从MySQL数据库取资料
2. 加上了中文文件名在部份浏览器,如IE下,下载时名字乱码的解决方法.
PHP报表如下:
转成xls的效果图:
代码如下:
<?php
/*** PHPExcel** Copyright (C) 2006 - 2014 PHPExcel** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Lesser General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this library; if not, write to the Free Software* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA** @category PHPExcel* @package PHPExcel* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL* @version 1.8.0, 2014-03-02*/header("Content-type: text/html; charset=utf-8");
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
//date_default_timezone_set('Europe/London');
date_default_timezone_set("Asia/Shanghai");if (PHP_SAPI == 'cli')die('This example should only be run from a Web Browser');/** Include PHPExcel */
require_once dirname(__FILE__) . '/libxls/Classes/PHPExcel.php';
require_once dirname(__FILE__) .'/inc/xcl_conn.php';//// Create new PHPExcel object
$objPHPExcel = new PHPExcel();// Set document properties
$objPHPExcel->getProperties()->setCreator("XiongChuanLiang")->setLastModifiedBy("XiongChuanLiang")->setTitle("汇总表");$objActSheet = $objPHPExcel->getActiveSheet(); $objActSheet->getColumnDimension('A')->setWidth(50);
$objActSheet->getColumnDimension('B')->setWidth(50); $objActSheet->getRowDimension(1)->setRowHeight(30);
$objActSheet->getRowDimension(2)->setRowHeight(16); $objActSheet->mergeCells('A1:C1');
$objActSheet->mergeCells('A2:C2');
//设置居中对齐
$objActSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objFontA1 = $objActSheet->getStyle('A1')->getFont();
$objFontA1->setSize(18);
$objFontA1->setBold(true); /$sql = mysql_query("SELECT * AS state_name, count( * ) AS stat_countFROM (......) kGROUP BY statusORDER BY status ");$info = mysql_fetch_array($sql);$objActSheet->setCellValue('A1', '汇总表');if(strlen( trim( $sdev_model)) > 0 ){ $objActSheet->setCellValue('A2',"型号:xxxxxx");}$row=3;$objActSheet->setCellValue('A'.$row,'状态');$objActSheet->setCellValue('B'.$row, '总数量');$row=4;do{ $objActSheet->setCellValueExplicit('A'.$row,$info['state_name'],PHPExcel_Cell_DataType::TYPE_STRING);$objActSheet->setCellValueExplicit('B'.$row,$info['stat_count'],PHPExcel_Cell_DataType::TYPE_NUMERIC);$objActSheet->setCellValue('A'.$row, $info['state_name']);$objActSheet->setCellValue('B'.$row, $info['stat_count']);$row++;}while($info=mysql_fetch_array($sql));/for ($currrow = 3; $currrow < $row; $currrow++) {//设置边框 $objActSheet->getStyle('A'.$currrow)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('A'.$currrow)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('A'.$currrow)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('A'.$currrow)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('B'.$currrow)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('B'.$currrow)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('B'.$currrow)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); $objActSheet->getStyle('B'.$currrow)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN ); }
////// Set the Labels for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataseriesLabels1 = array(new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$3', NULL, 1), );
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues1 = array(new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$4:$A$'.$row, NULL, 4),
);// Set the Data values for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataSeriesValues1 = array(new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$4:$B$'.$row, NULL, 4),
);// Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotTypePHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGroupingrange(0, count($dataSeriesValues1)-1), // plotOrder$dataseriesLabels1, // plotLabel$xAxisTickValues1, // plotCategory$dataSeriesValues1 // plotValues
);// Set up a layout object for the Pie chart
$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE);
$layout1->setShowPercent(TRUE);// Set the series in the plot area
$plotarea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
// Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);$title1 = new PHPExcel_Chart_Title('汇总表');// Create the chart
$chart1 = new PHPExcel_Chart('chart1', // name$title1, // title$legend1, // legend$plotarea1, // plotAreatrue, // plotVisibleOnly0, // displayBlanksAsNULL, // xAxisLabelNULL // yAxisLabel - Pie charts don't have a Y-Axis
);// Set the position where the chart should appear in the worksheet
$row += 2;
$chart1->setTopLeftPosition('A'.$row);
$row += 10;
$chart1->setBottomRightPosition('C'.$row);// Add the chart to the worksheet
$objPHPExcel->getActiveSheet()->addChart($chart1);
//// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);$filename = '汇总表_'.date("Y_m_d").".xlsx";// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Content-Disposition: attachment;filename="'.$filename.'"'); //devrent.xlsx//处理中文文件名乱码问题
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20",$encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment;filename="' . $encoded_filename . '"');
}else if (preg_match("/Firefox/", $ua)){ header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
}else { header('Content-Disposition: attachment; filename="' . $filename . '"');
}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');// 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$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');
exit;
另要注意的地方是,Excel的饼图,通过指定其标签,值所对应的单元格范围,自动生成,所以主要是在代码中计算好。另在非Windows服务器,生成会失败。
MAIL: xcl_168@aliyun.com
BLOG: http:/./blog.csdn.ent/xcl168
利用PHPExcel转Excel饼图相关推荐
- php写excel文件_利用PHPExcel实现Excel文件的写入和读取
作为一个原本的Java党,用过PHP才知道,原来对于Excel文件的写入和读取原来可以这么简单! 利用PHP实现对于Excel的读取,主要借助于PHPExcel插件来完成. PHPExcel下载地址: ...
- PHP利用phpExcel实现Excel数据的导入导出
phpExcel包的下载地址:http://download.csdn.net/detail/kesixin/9920920 首先先说一下,这段例程是使用在Thinkphp的开发框架上,要是使用在其他 ...
- 利用PHPExcel转Excel柱形图
这在附还有一个转柱形图的效果及代码. 原PHP报表效果: 转成Excel后的效果: 附上代码: <? php /** * PHPExcel * * Copyright (C) 2006 - 20 ...
- Thinkphp 3.2.2 利用phpexcel完成excel导出功能
首先百度搜索phpexcel 包,放到项目的这个目录下 接下来 是controller里的导出代码 public function expUser(){// $p_name = $_POST['o ...
- php excel 获取列数,利用PHPEXCEL读取EXCEL内容,主要是读取EXCEL可用列数 | 学步园...
require_once '../Classes/PHPExcel.php'; $objPHPExcel = PHPExcel_IOFactory::load('simple.xls'); $objA ...
- python xlrd读取文件报错_python利用xlrd读取excel文件始终报错原因
1.代码按照网上百度的格式进行书写如下: 但运行后,始终报错如下: 百度了xlrd网页: 分明支持xls和xlsx两种格式的文件,但运行始终报错. 最后找到原因是因为我所读取的文件虽然是以.xls命名 ...
- php读取excel并导入数据,PHPExcel读取excel并导入数据库
PHPExcel是一款php对于excel数据表读写的一个非常棒的插件了,下面我来给大家介绍利用PHPExcel读取excel并导入mysql数据库方法. 例1,代码示例,代码如下:require_o ...
- 使用phpexcel导出excel常用函数
下面主要总结了在工作中,我们利用phpexcel导出excel时常用的函数方法. <?php// 这边需要引入你自己的phpexcel内库 include_once './phpexcel/PH ...
- ThinkPHP 3.2.3 使用 PHPExcel 处理 Excel 表格
下载 PHPExcel(https://github.com/PHPOffice/PHPExcel) 把下载的 zip 包解压至 ./ThinkPHP/Library/Vendor 下 一.导入 Ex ...
最新文章
- ios开发 微博图片缩放处理错误_H5响应式开发必会之Viewport(视窗)详解
- mysql三表查询数据重复_解决mybatis三表连接查询数据重复的问题
- JavaScript修饰器-让代码更干净
- Divan and a New Project 贪心,模拟(1000)
- Redis布隆过滤器
- el-input输入金额,保留两位小数
- JSP中使用iframe导致内层网页CSS失效问题的解决方案
- 《算法图解》——广度优先探索与队列
- python自编中值滤波器
- 湖南职称计算机模拟试题,湖南职称计算机考试培训软件:集职称计算机考试模拟题、长沙职称计算机考试题库...
- 算法11 抓住波粒二象性的火星人
- html自动幻灯片代码,简单常用的幻灯片播放实现代码
- 创业感悟:有舍才会得,舍得才见风景
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- pandoc实现文档不同格式的转换
- 高德坐标系转天地图坐标系(GCJ02转WGS坐标系)
- MAC 启动jar失败 显示 Unable to access jarfile xxx.jar
- kali linux 打不开终端的问题
- UE4后期处理之水下效果
- UVA707Robbery(记忆化搜索)
热门文章
- Springboot​ mybatis-plus
- Mr. Kitayuta vs. Bamboos[二分+贪心][图像分析]
- 提升目标检测模型性能的tricks
- 阿里云云计算ACP实验考试之使用OSS对图片进行基本处理
- Selenium(2): DOM元素定位、操作
- 手机QQ浏览器的HTML管理器,手机qq浏览器中文件管理器有哪些功能
- 奔驰S400豪华型升级后排电动腿托系统,提升后排乘坐舒适性
- 精挑细选的良心APP,每款都非常惊艳
- [米家]窗帘电机【立创开源】
- 超好用的免费PDF转换器,各种互相转换,功能齐全到你不敢相信!