PHP之PHPExcel

news/2024/11/16 13:46:00/
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的
//创建一个excel
$objPHPExcel = new PHPExcel();
// 输出Excel表格到浏览器下载
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="abc.xls"');
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 = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');
——————————————————————————————————————–
//设置excel的属性:
//创建人
$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.");
//关键字
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
//种类
$objPHPExcel->getProperties()->setCategory("Test result file");
——————————————————————————————————————–//设置当前的sheet
$objPHPExcel->setActiveSheetIndex(0);
//设置sheet的name
$objPHPExcel->getActiveSheet()->settitle('Simple');
//设置单元格的值
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'String');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
$objPHPExcel->getActiveSheet()->setCellValue('A3', true);
$objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)');
$objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)');
//合并单元格
$objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
//分离单元格
$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28');
//冻结窗口
$objPHPExcel->getActiveSheet()->freezePane('A2');
//保护cell
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
//设置格式
// Set cell number formats
echo date('H:i:s') . " Set cell number formats\n";
$objPHPExcel->getActiveSheet()->getstyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicatestyle( $objPHPExcel->getActiveSheet()->getstyle('E4'), 'E5:E13' );
//设置宽width
// Set column widths
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
// 设置单元格高度
// 所有单元格默认高度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
// 第一行的默认高度
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
//设置font
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->setName('Candara');
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->setUnderline(PHPExcel_style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->getColor()->setARGB(PHPExcel_style_Color::COLOR_WHITE);
//objPHPExcel->getActiveSheet()->getstyle('B1')->getFont()->setColor(new \PHPExcel_Style_Color("FFFF00FF")); 
// PHPExcel_style_Color 
//COLOR_WHITE = "FFFFFFFF" 
//COLOR_BLACK = 'FF000000';
//COLOR_RED   = 'FFFF0000';
//android 定义颜色color时6位或8位值的区别:
//6位(#000000)就是RGB值
//8位(#1e000000)ARGB 头两位是透明度,00是完全透明,ff是完全不透明,后6位是RGB值,比较适中的透明度值
$objPHPExcel->getActiveSheet()->getstyle('E1')->getFont()->getColor()->setARGB(PHPExcel_style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getstyle('D13')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getstyle('E13')->getFont()->setBold(true);
//设置align
$objPHPExcel->getActiveSheet()->getstyle('D11')->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getstyle('D12')->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getstyle('D13')->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getstyle('A18')->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_JUSTIFY);
//垂直居中
$objPHPExcel->getActiveSheet()->getstyle('A18')->getAlignment()->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
设置column的border
$objPHPExcel->getActiveSheet()->getstyle('A4')->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle('B4')->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle('C4')->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle('D4')->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle('E4')->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
//设置border的color
$objPHPExcel->getActiveSheet()->getstyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getstyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getstyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getstyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getstyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getstyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');
//设置填充颜色
$objPHPExcel->getActiveSheet()->getstyle('A1')->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getstyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getstyle('B1')->getFill()->getStartColor()->setARGB('FF808080');
//加图片
/*实例化插入图片类*/
$objDrawing = new PHPExcel_Worksheet_Drawing();
/*设置图片路径 切记:只能是本地图片*/
$objDrawing->setPath($img_val);
/*设置图片高度*/
$objDrawing->setWidth(200);
$img_height[] = $objDrawing->getHeight();
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates($img_k[$j].$i);
/*设置图片所在单元格的格式*/
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY(10);
$objDrawing->setRotation(0);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(50);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());//导出Excel表格例子
$datas = array(
array('王城', '男', '18', '1997-03-13', '18948348924'),
array('李飞虹', '男', '21', '1994-06-13', '159481838924'),
array('王芸', '女', '18', '1997-03-13', '18648313924'),
array('郭瑞', '男', '17', '1998-04-13', '15543248924'),
array('李晓霞', '女', '19', '1996-06-13', '18748348924'),
);
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的
// 创建一个excel
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Phpmarker")->setLastModifiedBy("Phpmarker")->settitle("Phpmarker")->setSubject("Phpmarker")->setDescription("Phpmarker")->setKeywords("Phpmarker")->setCategory("Phpmarker");
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '名字')->setCellValue('B1', '性别')->setCellValue('C1', '年龄')->setCellValue('D1', '出生日期')->setCellValue('E1', '电话号码');// Rename worksheet
$objPHPExcel->getActiveSheet()->settitle('Phpmarker-' . date('Y-m-d'));// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
$objPHPExcel->getActiveSheet()->freezePane('A2');
$i = 2;
foreach($datas as $data){
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data[0])->getstyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data[1]);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data[2]);
$objPHPExcel->getActiveSheet()->setCellValueExplicit('D'. $i, $data[3],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle('D' . $i)->getNumberFormat()->setFormatCode("@");// 设置文本格式
$objPHPExcel->getActiveSheet()->setCellValueExplicit('E'. $i, $data[4],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle('E' . $i)->getAlignment()->setWrapText(true);
$i ;
}
$objActSheet = $objPHPExcel->getActiveSheet();// 设置CELL填充颜色
$cell_fill = array(
'A1',
'B1',
'C1',
'D1',
'E1',
);
foreach($cell_fill as $cell_fill_val){
$cellstyle = $objActSheet->getstyle($cell_fill_val);
// background
// $cellstyle->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID)->getStartColor()->setARGB('fafa00');
// set align
$cellstyle->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
// font
$cellstyle->getFont()->setSize(12)->setBold(true);
// border
$cellstyle->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000');
//android 定义颜色color时6位或8位值的区别:
//6位(#000000)就是RGB值
//8位(#1e000000)ARGB 头两位是透明度,00是完全透明,ff是完全不透明,后6位是RGB值,比较适中的透明度值
$cellstyle->getBorders()->getBottom()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000');
$cellstyle->getBorders()->getLeft()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000');
$cellstyle->getBorders()->getRight()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000');
}$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);$objActSheet->getColumnDimension('A')->setWidth(18.5);
$objActSheet->getColumnDimension('B')->setWidth(23.5);
$objActSheet->getColumnDimension('C')->setWidth(12);
$objActSheet->getColumnDimension('D')->setWidth(12);
$objActSheet->getColumnDimension('E')->setWidth(12);$filename = '2015030423';
ob_end_clean();//清除缓冲区,避免乱码 
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
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 = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');
?> 

常见问题:

1.最近在使用PHPExcel循环生成多个sheet时,遇到You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1错误

原因:产生这个错误的原因是PHPExcel会自动创建第一个sheet,因此我们可以直接创建一个PHPEXCEL对象并且操作第一个sheet

$excel = new PHPExcel();
$excel->setactivesheetindex(0);

但是默认第一个sheet是已经创建的, 所以只需要创建后面的就可以

$excel = new PHPExcel();
for($i=0;$i<5;$i++){if($i>0){$excel->createSheet();}$excel->setactivesheetindex($i);
}

http://www.ppmy.cn/news/1022672.html

相关文章

面试热题(路径总和II)

给你二叉树的根节点 root 和一个整数目标和 targetSum &#xff0c;找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。 叶子节点 是指没有子节点的节点。 在这里给大家提供两种方法进行思考&#xff0c;第一种方法是递归&#xff0c;第二种方式使用回溯的方式进行爆…

新加坡的区块链和NFT市场调研

新加坡的区块链和NFT市场调研 基本介绍 略 移动互联网发展情况 拥有 92% 的互联网渗透率&#xff0c;并且高达 89.5% 的新加坡民众皆为活跃的社交媒体用户 社交媒体 WhatsappFacebook用户&#xff1a; 355万, 占整个新加坡总人口的60%Twitter (12.45%)、Pinterest (6.62%…

Docker启动一个Centos镜像

搜索可用的centos的docker镜像 docker search <image>&#xff1a;在docker index中搜索imagedocker search centos 下载centos镜像&#xff08;拉取镜像&#xff09; docker pull centos:latest查看镜像docker images&#xff1a;列出imagesdocker images -a&#xff…

人工智能可解释性(补充)

目录 1.定义 2.详述 2.1局部解释 可视化方法 梯度计算 2.2积分梯度Integrated Gradients&#xff08;梯度计算进阶&#xff09; 2. 3全局解释 2.3.1Activation Maximization 2.3.2GAN,VAE 2. 4用一个可解释模型解释不可解释模型 2. 4.1LIME 局部解释 参考文献 1.定义 可…

Git (2)

文章目录 1. 删除文件2. 分支管理2.1 理解分支2.2 分支创建 &#xff0c; 分支切换2.3 分支合并2.4 删除分支2.5 合并冲突2.6 合并模式2.7 分支策略2.8 bug 分支2.9 强制删除分支 3. 远程操作3.1 创建远程仓库3.2 克隆远程仓库3.3 推送3.4 拉取3.5 gitignore 文件3.6 配置别名 …

从零开始学习 Java:简单易懂的入门指南之面向对象(九)

面向对象进阶 前情回顾1.1 如何定义类1.2 如何通过类创建对象1.3 封装1.3.1 封装的步骤1.3.2 封装的步骤实现 1.4 构造方法1.4.1 构造方法的作用1.4.2 构造方法的格式1.4.3 构造方法的应用 1.5 this关键字的作用1.5.1 this关键字的作用1.5.2 this关键字的应用1.5.2.1 用于普通的…

Python“牵手”京东工业商城商品详情数据方法介绍

京东工业平台&#xff08;imall.jd.com&#xff09;是一个 B2B 电商平台&#xff0c;提供了丰富的工业品类商品&#xff0c;涵盖了机械、化工、建材、劳保用品等品类。如果您需要采集京东工业平台的商品详情数据&#xff0c;可以尝试以下步骤&#xff1a; 选定目标品类和 SKU …

APP备案明明是好事,为啥有些人反对呢?

我是卢松松&#xff0c;点点上面的头像&#xff0c;欢迎关注我哦&#xff01; APP和小程序备案&#xff0c; 这事在网上闹的沸沸扬扬&#xff0c;明明是好事&#xff0c;可为啥那么多人反对呢?而且最近出现了好多阴阳怪气的声音。 话说从2005年3月起&#xff0c;国内所有的网…