首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把classes解压到你项目的一个目录中,重名名为phpexcel,开始喽,(代码都摘自自带实例)
程序部分
require_once
// 首先创建一个新的对象
$objPHPExcel =
// 设置文件的一些属性,在xls文件——>属性——>详细信息里可以看到这些值,xml表格里是没有这些值的
$objPHPExcel
// 位置aaa
// 给表格添加数据
$objPHPExcel->setActiveSheetIndex(0)
//得到当前活动的表,注意下文教程中会经常用到$objActSheet
$objActSheet = $objPHPExcel->getActiveSheet();
// 位置bbb
// 给当前活动的表设置名称
$objActSheet->setTitle('Simple2222');
代码还没有结束,可以复制下面的代码来决定我们将要做什么
我们将要做的是
1,直接生成一个文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->save('myexchel.xlsx');
2、提示下载文件
excel 2003 .xls
// 生成2003excel格式的xls文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->save('php://output');
exit;
excel 2007 .xlsx
// 生成2007excel格式的xlsx文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter
$objWriter->save(
exit;
pdf 文件
// 下载一个pdf文件
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->save('php://output');
exit;
// 生成一个pdf文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->save('a.pdf');
CSV 文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
HTML 文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->setSheetIndex(0);
//$objWriter->setImagesRoot('http://www.example.com');
$objWriter->save(str_replace('.php',
设置表格样式和数据格式
设置默认的字体和文字大小
$objPHPExcel->getDefaultStyle()->getFont()->setName(
$objPHPExcel->getDefaultStyle()->getFont()->setSize(20);
日期格式
//获得秒值变量
$dateTimeNow = time();
//三个表格分别设置为当前实际的 日期格式、时间格式、日期和时间格式
//首先将单元格的值设置为由PHPExcel_Shared_Date::PHPToExcel方法转换后的excel格式的值,然后用过得到该单元格的样式里面数字样式再设置显示格式
$objActSheet->setCellValue(
$objActSheet->getStyle(
$objActSheet->setCellValue(
$objActSheet->getStyle(
$objActSheet->setCellValue(
$objActSheet->getStyle(
//将E4到E13的数字格式设置为EUR
$objPHPExcel->getActiveSheet()->getStyle(
设置列的宽度
$objActSheet->getColumnDimension(
$objActSheet->getColumnDimension(
设置文件打印的页眉和页脚
//设置打印时候的页眉页脚(设置完了以后可以通过打印预览来看效果)字符串中的&*好像是一些变量
$objActSheet->getHeaderFooter()->setOddHeader(
$objActSheet->getHeaderFooter()->setOddFooter(
设置页面文字的方向和页面大小 $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup:: 为页眉添加图片
$objDrawing =
$objDrawing->setName('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setHeight(36);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT
设置单元格的批注
//给单元格添加批注
$objPHPExcel->getActiveSheet()->getComment(
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E13'
$objCommentRichText->getFont()->setBold(
$objPHPExcel->getActiveSheet()->getComment(
$objPHPExcel->getActiveSheet()->getComment(
$objPHPExcel->getActiveSheet()->getComment(
$objPHPExcel->getActiveSheet()->getComment(
$objPHPExcel->getActiveSheet()->getComment(
$objPHPExcel->getActiveSheet()->getComment(
添加文字块
//大概翻译
$objRichText =
$objRichText->createText('This invoice is ');
//添加文字并设置这段文字粗体斜体和文字颜色
$objPayable = $objRichText->createTextRun(
$objPayable->getFont()->setBold(
$objPayable->getFont()->setItalic(
$objPayable->getFont()->setColor(
$objRichText->createText(', unless specified otherwise on the invoice.');
//将文字写到A18单元格中
$objPHPExcel->getActiveSheet()->getCell(
合并拆分单元格
$objPHPExcel->getActiveSheet()->mergeCells(
$objPHPExcel->getActiveSheet()->unmergeCells(
单元格密码保护
// 单元格密码保护不让修改
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(
$objPHPExcel->getActiveSheet()->protectCells(
$objPHPExcel->getActiveSheet()->getStyle(
设置单元格字体
//将B1的文字字体设置为Candara,20号的粗体下划线有背景色
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
文字对齐方式
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);//水平方向上两端对齐
$objPHPExcel->getActiveSheet()->getStyle(
设置单元格边框
$styleThinBlackBorderOutl
);
$objPHPExcel->getActiveSheet()->getStyle(
背景填充颜色
//设置填充的样式和背景色
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
综合设置样例
$objPHPExcel->getActiveSheet()->getStyle(
);
给单元格内容设置url超链接
$objActSheet->getCell('E26')->getHyperlink()->setUrl(
$objActSheet->getCell('E26')->getHyperlink()->setTooltip(
给表中添加图片
$objDrawing =
$objDrawing->setName('Paid');
$objDrawing->setDescription('Paid');
$objDrawing->setPath('./images/paid.png');
$objDrawing->setCoordinates('B15');
$objDrawing->setOffsetX(210);
$objDrawing->setRotation(25);
$objDrawing->setHeight(36);
$objDrawing->getShadow()->setVisible
$objDrawing->getShadow()->setDirection(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//还可以添加有gd库生产的图片,详细见自带实例25
创建一个新工作表和设置工作表标签颜色
$objExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objExcel->getSheet(1)->setTitle(
$objPHPExcel->getActiveSheet()->getTabColor()->setARGB(
添加或删除行和列
$objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10);
$objPHPExcel->getActiveSheet()->removeRow(6, 10);
$objPHPExcel->getActiveSheet()->insertNewColumnBefore(
$objPHPExcel->getActiveSheet()->removeColumn(
隐藏和显示某列
$objPHPExcel->getActiveSheet()->getColumnDimension(
$objPHPExcel->getActiveSheet()->getColumnDimension(
重新命名活动的表的标签名称
$objPHPExcel->getActiveSheet()->setTitle(
设置工作表的安全
$objPHPExcel->getActiveSheet()->getProtection()->setPassword(
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(
$objPHPExcel->getActiveSheet()->getProtection()->setSort(
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(
设置文档安全
$objPHPExcel->getSecurity()->setLockWindows(
$objPHPExcel->getSecurity()->setLockStructure(
$objPHPExcel->getSecurity()->setWorkbookPassword(
样式复制
//将B2的样式复制到B3至B7
$objPHPExcel->getActiveSheet()->duplicateConditionalStyl
Add conditional formatting
echo
$objConditional1 =
$objConditional1->setConditionType(PHPExcel_Style_Conditional
$objConditional1->setOperatorType(PHPExcel_Style_Conditional
$objConditional1->addCondition('200');
$objConditional1->addCondition('400');
设置分页(主要用于打印)
//设置某单元格为页尾
$objPHPExcel->getActiveSheet()->setBreak(
用数组填充表
//吧数组的内容从A2开始填充
$dataArray =
设置自动筛选
$objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimens
//$objPHPExcel->getActiveSheet()->calculateWorksheetDimens打印出的到所有的公式
$objCalc = PHPExcel_Calculation::getInstance();
print_r($objCalc->listFunctionNames())
设置单元格值的范围
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B3'
$objValidation->setType( PHPExcel_Cell_DataValidation::
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(
$objValidation->setShowErrorMessage(
$objValidation->setErrorTitle('Input error');
//$objValidation->setShowDropDown(true);
$objValidation->setError('Only numbers between 10 and 20 are allowed!');
$objValidation->setPromptTitle('Allowed input');
$objValidation->setPrompt('Only numbers between 10 and 20 are allowed.');
$objValidation->setFormula1(10);
$objValidation->setFormula2(120);
//或者这样设置
其他
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getStyle(
$objPHPExcel->getActiveSheet()->getCell(B14)->getValue();
$objPHPExcel->getActiveSheet()->getCell(B14)->getCalculatedValue();//获得算出的值
导入或读取文件
//通过PHPExcel_IOFactory::load方法来载入一个文件,load会自动判断文件的后缀名来导入相应的处理类,读取格式保含xlsx/xls/xlsm/ods/slk/csv/xml/gnumeric
require_once
$objPHPExcel = PHPExcel_IOFactory::load(
//吧载入的文件默认表(一般都是第一个)通过toArray方法来返回一个多维数组
$dataArray = $objPHPExcel->getActiveSheet()->toArray();
//读完直接写到一个xlsx文件里
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
$objWriter->save(str_replace('.php',
读取xml文件
$objReader = PHPExcel_IOFactory::
$objPHPExcel = $objReader->load(
读取ods文件
$objReader = PHPExcel_IOFactory::
$objPHPExcel =
读取numeric文件
$objReader = PHPExcel_IOFactory::
$objPHPExcel = $objReader->load(
读取slk文件
$objPHPExcel = PHPExcel_IOFactory::
循环遍历数据
$objReader = PHPExcel_IOFactory::createReader('Excel2007'
$objPHPExcel = $objReader->load
foreach
}
吧数组插入的表中
//插入的数据 3行数据
$data =
$baseRow = 5;
foreach($data
}
$objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1);
|
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)