博主在使用ThinkPHP開發(fā)網(wǎng)站的時候框都,經(jīng)常會遇到用戶要求導(dǎo)出Excel文件搬素,經(jīng)過在網(wǎng)上尋找后發(fā)現(xiàn),PHPExcel是目前PHP端解決Excel導(dǎo)出的最好方案魏保,這里總結(jié)了一些常用的使用邏輯熬尺,可以根據(jù)需要來使用,歡迎大家來補充完善谓罗。
.h文件
#import@classUIImage;typedefvoid(^CompleateBlock)(NSString*text);@interfaceRecogizeCardManager:NSObject
文/wythetan(簡書作者)
原文鏈接:http://www.reibang.com/p/ac4c4536ca3e
著作權(quán)歸作者所有粱哼,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”妥衣。
加載及初始化
//PHP原生加載PHPExcel包
include 'PHPExcel.php';
include 'PHPExcel/RichText.php'; //用于輸出.xls/.pdf的//ThinkPHP加載PHPExcel包,放置在Vendor文件夾中皂吮,通過vendor函數(shù)引入第三方類庫
Vendor('PHPExcel');//創(chuàng)建一個excel
$objPHPExcel = new PHPExcel();//保存excel
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("xxx.xlsx");//輸出XLS到瀏覽器
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header("Pragma: public");
header("Expires: 0″);
header("Cache-Control:must-revalidate, post-check=0, pre-check=0″);
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="resume.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');//輸出PDF到瀏覽器
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'TCPDF';
$rendererLibraryPath = './Lib/tcpdf/';
PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setSheetIndex(0);
header("Pragma: public");
header("Expires: 0″);
header("Cache-Control:must-revalidate, post-check=0, pre-check=0″);
header("Content-Type:application/force-download");
header("Content-Type:application/pdf");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="resume.pdf"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');常用屬性
//創(chuàng)建人
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");//最后修改人
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");//標(biāo)題
$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.");//關(guān)鍵字
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");//種類
$objPHPExcel->getProperties()->setCategory("Test result file");其他常用設(shè)置
//設(shè)置默認(rèn)字體和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName('宋體');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);//設(shè)置當(dāng)前的sheet
$objPHPExcel->setActiveSheetIndex(0);//設(shè)置sheet的name
$objPHPExcel->getActiveSheet()->setTitle('Simple');//設(shè)置單元格的值
$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');//保護cell
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');設(shè)置單元格格式
// Set cell number formats
echo date('H:i:s') . " Set cell number formatsn";
$objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );//設(shè)置寬width
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);//設(shè)置高度
$objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);//設(shè)置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('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);//設(shè)置align
$objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);//垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//設(shè)置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);//設(shè)置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');//設(shè)置填充顏色
$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->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('./images/officelogo.jpg');
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Paid');
$objDrawing->setDescription('Paid');
$objDrawing->setPath('./images/paid.png');
$objDrawing->setCoordinates('B15');
$objDrawing->setOffsetX(110);
$objDrawing->setRotation(25);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());//超鏈接
$objPHPExcel->getActiveSheet()->getCell('J9')->getHyperlink()->setUrl('http://www.g.cn/');//在默認(rèn)sheet后,創(chuàng)建一個worksheet
echo date('H:i:s') . " Create new Worksheet object\n";
$objPHPExcel->createSheet();
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter-save('php://output');//單元格內(nèi)換行
注意:1.換行的字符串Hello\nWorld外面必須是雙引號 2.必須要加上最后一句
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A4', "Hello\nWorld");
$objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setWrapText(true);//設(shè)置單元格為文本格式
//設(shè)置A3單元格為文本
$objPHPExcel->getActiveSheet()->getStyle('A3')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);//設(shè)置整行或整列的style
//E 列為文本
$objPHPExcel->getActiveSheet()->getStyle('E')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);//第三行為文本
$objPHPExcel->getActiveSheet()->getStyle('3')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);