phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
随风飘扬 php 2016年08月08日11:37 2130
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把classes解压到你项目的一个目录中,重名名为phpexcel,开始喽,(代码都摘自自带实例)
程序部分
require_once './phpexcel/PHPExcel.php';
// 首先创建一个新的对象 PHPExcel object
$objPHPExcel = new PHPExcel();
// 设置文件的一些属性,在xls文件——>属性——>详细信息里可以看到这些值,xml表格里是没有这些值的
$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"); //设置类别
// 位置aaa *为下文代码位置提供锚
// 给表格添加数据
$objPHPExcel->setActiveSheetIndex(0) //设置第一个内置表(一个xls文件里可以有多个表)为活动的
->setCellValue( 'A1', 'Hello' ) //给表的单元格设置数据
->setCellValue( 'B2', 'world!' ) //数据格式可以为字符串
->setCellValue( 'C1', 12) //数字型
->setCellValue( 'D2', 12) //
->setCellValue( 'D3', true ) //布尔型
->setCellValue( 'D4', '=SUM(C1:D2)' );//公式
//得到当前活动的表,注意下文教程中会经常用到$objActSheet
$objActSheet = $objPHPExcel->getActiveSheet();
// 位置bbb *为下文代码位置提供锚
// 给当前活动的表设置名称
$objActSheet->setTitle('Simple2222');
代码还没有结束,可以复制下面的代码来决定我们将要做什么
我们将要做的是
1,直接生成一个文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$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, 'Excel5');
$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 = PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');
$objWriter->save( 'php://output');
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, 'PDF');
$objWriter->save('php://output');
exit;
// 生成一个pdf文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('a.pdf');
CSV 文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter(',' ) //设置分隔符
->setEnclosure('"' ) //设置包围符
->setLineEnding("\r\n" )//设置行分隔符
->setSheetIndex(0) //设置活动表
->save(str_replace('.php' , '.csv' , __FILE__));
HTML 文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML'); //将$objPHPEcel对象转换成html格式的
$objWriter->setSheetIndex(0); //设置活动表
//$objWriter->setImagesRoot('http://www.example.com');
$objWriter->save(str_replace('.php', '.htm', __FILE__)); //保存文件
设置表格样式和数据格式
设置默认的字体和文字大小 锚:aaa
$objPHPExcel->getDefaultStyle()->getFont()->setName( 'Arial');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(20);
日期格式 锚:bbb
//获得秒值变量
$dateTimeNow = time();
//三个表格分别设置为当前实际的 日期格式、时间格式、日期和时间格式
//首先将单元格的值设置为由PHPExcel_Shared_Date::PHPToExcel方法转换后的excel格式的值,然后用过得到该单元格的样式里面数字样式再设置显示格式
$objActSheet->setCellValue( 'C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow )); $objActSheet->getStyle( 'C9')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
$objActSheet->setCellValue( 'C10', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objActSheet->getStyle( 'C10')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
$objActSheet->setCellValue( 'C10', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objActSheet->getStyle( 'C10')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
//将E4到E13的数字格式设置为EUR
$objPHPExcel->getActiveSheet()->getStyle( 'E4:E13')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
设置列的宽度 锚:bbb
$objActSheet->getColumnDimension( 'B')->setAutoSize(true); //内容自适应
$objActSheet->getColumnDimension( 'A')->setWidth(30); //30宽
设置文件打印的页眉和页脚 锚:bbb
//设置打印时候的页眉页脚(设置完了以后可以通过打印预览来看效果)字符串中的&*好像是一些变量
$objActSheet->getHeaderFooter()->setOddHeader( '&L&G&C&HPlease treat this document as confidential!');
$objActSheet->getHeaderFooter()->setOddFooter( '&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N' );
设置页面文字的方向和页面大小 锚:bbb
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup:: ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4); //A4纸大小为页眉添加图片 office中有效 wps中无效 锚:bbb
$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setHeight(36);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT );
设置单元格的批注 锚:bbb
//给单元格添加批注
$objPHPExcel->getActiveSheet()->getComment( 'E13')->setAuthor('PHPExcel' ); //设置作者
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E13' )->getText()->createTextRun('PHPExcel:'); //添加批注
$objCommentRichText->getFont()->setBold( true); //将现有批注加粗
$objPHPExcel->getActiveSheet()->getComment( 'E13')->getText()->createTextRun("\r\n" ); //添加更多批注
$objPHPExcel->getActiveSheet()->getComment( 'E13')->getText()->createTextRun('Total amount on the current invoice, including VAT.' );
$objPHPExcel->getActiveSheet()->getComment( 'E13')->setWidth('100pt' ); //设置批注显示的宽高 ,在office中有效在wps中无效
$objPHPExcel->getActiveSheet()->getComment( 'E13')->setHeight('100pt' );
$objPHPExcel->getActiveSheet()->getComment( 'E13')->setMarginLeft('150pt' );
$objPHPExcel->getActiveSheet()->getComment( 'E13')->getFillColor()->setRGB('EEEEEE' ); //设置背景色 ,在office中有效在wps中无效
添加文字块 看效果图 office中有效 wps中无效 锚:bbb
//大概翻译 创建一个富文本框 office有效 wps无效
$objRichText = new PHPExcel_RichText();
$objRichText->createText('This invoice is '); //写文字
//添加文字并设置这段文字粗体斜体和文字颜色
$objPayable = $objRichText->createTextRun( 'payable within thirty days after the end of the month');
$objPayable->getFont()->setBold( true);
$objPayable->getFont()->setItalic( true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(', unless specified otherwise on the invoice.');
//将文字写到A18单元格中
$objPHPExcel->getActiveSheet()->getCell( 'A18')->setValue($objRichText);
合并拆分单元格 锚:bbb
$objPHPExcel->getActiveSheet()->mergeCells( 'A28:B28'); // A28:B28合并
$objPHPExcel->getActiveSheet()->unmergeCells( 'A28:B28'); // A28:B28再拆分
单元格密码保护 锚:bbb
// 单元格密码保护不让修改
$objPHPExcel->getActiveSheet()->getProtection()->setSheet( true); // 为了使任何表保护,需设置为真
$objPHPExcel->getActiveSheet()->protectCells( 'A3:E13', 'PHPExcel' ); // 将A3到E13保护 加密密码是 PHPExcel
$objPHPExcel->getActiveSheet()->getStyle( 'B1')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); //去掉保护
设置单元格字体 锚:bbb
//将B1的文字字体设置为Candara,20号的粗体下划线有背景色
$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);
文字对齐方式 锚:bbb
$objPHPExcel->getActiveSheet()->getStyle( 'D11')->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); //垂直方向上中间居中
设置单元格边框 锚:bbb
$styleThinBlackBorderOutline = array(
'borders' => array (
'outline' => array (
//'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式
'color' => array ('argb' => 'FF000000'), //设置border颜色
),
),
);
$objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline);
背景填充颜色 锚:bbb
//设置填充的样式和背景色
$objPHPExcel->getActiveSheet()->getStyle( 'A1:E1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle( 'A1:E1')->getFill()->getStartColor()->setARGB('FF808080');
综合设置样例
$objPHPExcel->getActiveSheet()->getStyle( 'A3:E3')->applyFromArray(
array(
'font'  
Bản phát biểu: tất cả các bản vẽ được in trên trang web là để truyền thêm thông tin và dễ dàng thảo luận, không có nghĩa là website, máy chủ và người cầm đầu đồng ý ý kiến của họ hoặc xác nhận tính xác thực của nội dung của họ. Nội dung các bài báo chỉ có liên quan. Nếu quyền của tác giả gốc bị vi phạm, xin liên lạc với quản trị website.
Ghi chú
-
Không có dữ liệu