• PHPExcel 基本用法详解


      1 1.header
      2 header("Content-Type:application/vnd.ms-excel");
      3 header("Content-Disposition:attachment;filename=sample.xls");
      4 header("Pragma:no-cache");
      5 header("Expires:0");
      6 
      7 2.PHPExcel
      8 http://www.codeplex.com/PHPExcel
      9 http://www.phpexcel.net
     10 开发包Tests目录有详细使用实例  支持中文,注意文件编码   文件保存为utf-8
     11 
     12 写excel
     13 //Include class
     14 require_once('Classes/PHPExcel.php');
     15 require_once('Classes/PHPExcel/Writer/Excel2007.php');
     16 $objPHPExcel = new PHPExcel();
     17  
     18 //Set properties 设置文件属性
     19 $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
     20 $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
     21 $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
     22 $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
     23 $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
     24 $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
     25 $objPHPExcel->getProperties()->setCategory("Test result file");
     26  
     27 //Add some data 添加数据
     28 $objPHPExcel->setActiveSheetIndex(0);
     29 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');//可以指定位置
     30 $objPHPExcel->getActiveSheet()->setCellValue('A2', true);
     31 $objPHPExcel->getActiveSheet()->setCellValue('A3', false);
     32 $objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!');
     33 $objPHPExcel->getActiveSheet()->setCellValue('B3', 2);
     34 $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello');
     35 $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');
     36  
     37 //循环
     38 for($i = 1;$i<200;$i++) {
     39 $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i);
     40 $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, 'Test value');
     41 }
     42  
     43 //日期格式化
     44 $objPHPExcel->getActiveSheet()->setCellValue('D1', time());
     45 $objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
     46  
     47 //Add comment 添加注释
     48 $objPHPExcel->getActiveSheet()->getComment('E11')->setAuthor('PHPExcel');
     49 $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('PHPExcel:');
     50 $objCommentRichText->getFont()->setBold(true);
     51 $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun("
    ");
     52 $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('Total amount on the current invoice, excluding VAT.');
     53  
     54 //Add rich-text string 添加文字 可设置样式
     55 $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18') );
     56 $objRichText->createText('This invoice is ');
     57 $objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
     58 $objPayable->getFont()->setBold(true);
     59 $objPayable->getFont()->setItalic(true);
     60 $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
     61 $objRichText->createText(', unless specified otherwise on the invoice.');
     62  
     63 //Merge cells 合并分离单元格
     64 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
     65 $objPHPExcel->getActiveSheet()->unmergeCells('A18:E22');
     66  
     67 //Protect cells 保护单元格
     68 $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//Needs to be set to true in order to enable any worksheet protection!
     69 $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
     70  
     71 //Set cell number formats 数字格式化
     72 $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
     73 $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
     74  
     75 //Set column widths 设置列宽度
     76 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
     77 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
     78  
     79 //Set fonts 设置字体
     80 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
     81 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
     82 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
     83 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
     84 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
     85  
     86 //Set alignments 设置对齐
     87 $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
     88 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
     89 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
     90 $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setWrapText(true);
     91  
     92 //Set column borders 设置列边框
     93 $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
     94 $objPHPExcel->getActiveSheet()->getStyle('A10')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
     95 $objPHPExcel->getActiveSheet()->getStyle('E10')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
     96 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
     97 $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
     98  
     99 //Set border colors 设置边框颜色
    100 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
    101 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
    102 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
    103 $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');
    104  
    105 //Set fills 设置填充
    106 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    107 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
    108  
    109 //Add a hyperlink to the sheet 添加链接
    110 $objPHPExcel->getActiveSheet()->setCellValue('E26', 'www.phpexcel.net');
    111 $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setUrl('http://www.phpexcel.net');
    112 $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setTooltip('Navigate to website');
    113 $objPHPExcel->getActiveSheet()->getStyle('E26')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    114  
    115 //Add a drawing to the worksheet 添加图片
    116 $objDrawing = new PHPExcel_Worksheet_Drawing();
    117 $objDrawing->setName('Logo');
    118 $objDrawing->setDescription('Logo');
    119 $objDrawing->setPath('./images/officelogo.jpg');
    120 $objDrawing->setHeight(36);
    121 $objDrawing->setCoordinates('B15');
    122 $objDrawing->setOffsetX(110);
    123 $objDrawing->setRotation(25);
    124 $objDrawing->getShadow()->setVisible(true);
    125 $objDrawing->getShadow()->setDirection(45);
    126 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    127  
    128 //Play around with inserting and removing rows and columns
    129 $objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10);
    130 $objPHPExcel->getActiveSheet()->removeRow(6, 10);
    131 $objPHPExcel->getActiveSheet()->insertNewColumnBefore('E', 5);
    132 $objPHPExcel->getActiveSheet()->removeColumn('E', 5);
    133  
    134 //Add conditional formatting
    135 $objConditional1 = new PHPExcel_Style_Conditional();
    136 $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
    137 $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
    138 $objConditional1->setCondition('0');
    139 $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
    140 $objConditional1->getStyle()->getFont()->setBold(true);
    141  
    142 //Set autofilter 自动过滤
    143 $objPHPExcel->getActiveSheet()->setAutoFilter('A1:C9');
    144  
    145 //Hide "Phone" and "fax" column 隐藏列
    146 $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setVisible(false);
    147 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
    148  
    149 //Set document security 设置文档安全
    150 $objPHPExcel->getSecurity()->setLockWindows(true);
    151 $objPHPExcel->getSecurity()->setLockStructure(true);
    152 $objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel");
    153  
    154 //Set sheet security 设置工作表安全
    155 $objPHPExcel->getActiveSheet()->getProtection()->setPassword('PHPExcel');
    156 $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// This should be enabled in order to enable any of the following!
    157 $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
    158 $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
    159 $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
    160  
    161 //Calculated data 计算
    162 echo 'Value of B14 [=COUNT(B2:B12)]: ' . $objPHPExcel->getActiveSheet()->getCell('B14')->getCalculatedValue() . "
    ";
    163  
    164 //Set outline levels
    165 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
    166 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setVisible(false);
    167 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setCollapsed(true);
    168  
    169 //Freeze panes
    170 $objPHPExcel->getActiveSheet()->freezePane('A2');
    171  
    172 //Rows to repeat at top
    173 $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
    174  
    175 //Set data validation 验证输入值
    176 $objValidation = $objPHPExcel->getActiveSheet()->getCell('B3')->getDataValidation();
    177 $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
    178 $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
    179 $objValidation->setAllowBlank(true);
    180 $objValidation->setShowInputMessage(true);
    181 $objValidation->setShowErrorMessage(true);
    182 $objValidation->setErrorTitle('Input error');
    183 $objValidation->setError('Number is not allowed!');
    184 $objValidation->setPromptTitle('Allowed input');
    185 $objValidation->setPrompt('Only numbers between 10 and 20 are allowed.');
    186 $objValidation->setFormula1(10);
    187 $objValidation->setFormula2(20);
    188 $objPHPExcel->getActiveSheet()->getCell('B3')->setDataValidation($objValidation);
    189  
    190 //Create a new worksheet, after the default sheet 创建新的工作标签
    191 $objPHPExcel->createSheet();
    192 $objPHPExcel->setActiveSheetIndex(1);
    193  
    194 //Set header and footer. When no different headers for odd/even are used, odd header is assumed. 页眉页脚
    195 $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&HPlease treat this document as confidential!');
    196 $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
    197  
    198 //Set page orientation and size 方向大小
    199 $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    200 $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
    201  
    202 //Rename sheet 重命名工作表标签
    203 $objPHPExcel->getActiveSheet()->setTitle('Simple');
    204  
    205 //Set active sheet index to the first sheet, so Excel opens this as the first sheet
    206 $objPHPExcel->setActiveSheetIndex(0);
    207  
    208 //Save Excel 2007 file 保存
    209 $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    210 $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    211  
    212 //Save Excel 5 file 保存
    213 require_once('Classes/PHPExcel/Writer/Excel5.php');
    214 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
    215 $objWriter->save(str_replace('.php', '.xls', __FILE__));
    216  
    217 //1.6.2新版保存
    218 require_once('Classes/PHPExcel/IOFactory.php');
    219 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    220 $objWriter->save(str_replace('.php', '.xls', __FILE__));
    221 
    222 读excel
    223 //Include class
    224 require_once('Classes/PHPExcel/Reader/Excel2007.php');
    225 $objReader = new PHPExcel_Reader_Excel2007;
    226 $objPHPExcel = $objReader->load("05featuredemo.xlsx");
    227 
    228 读写csv
    229 require_once("05featuredemo.inc.php");
    230 require_once('Classes/PHPExcel/Writer/CSV.php');
    231 require_once('Classes/PHPExcel/Reader/CSV.php');
    232 require_once('Classes/PHPExcel/Writer/Excel2007.php');
    233 //Write to CSV format 写
    234 $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
    235 $objWriter->setDelimiter(';');
    236 $objWriter->setEnclosure('');
    237 $objWriter->setLineEnding("
    ");
    238 $objWriter->setSheetIndex(0);
    239 $objWriter->save(str_replace('.php', '.csv', __FILE__));
    240 //Read from CSV format 读
    241 $objReader = new PHPExcel_Reader_CSV();
    242 $objReader->setDelimiter(';');
    243 $objReader->setEnclosure('');
    244 $objReader->setLineEnding("
    ");
    245 $objReader->setSheetIndex(0);
    246 $objPHPExcelFromCSV = $objReader->load(str_replace('.php', '.csv', __FILE__));
    247 //Write to Excel2007 format
    248 $objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
    249 $objWriter2007->save(str_replace('.php', '.xlsx', __FILE__));
    250 
    251 写html
    252 require_once("05featuredemo.inc.php");
    253 require_once('Classes/PHPExcel/Writer/HTML.php');
    254 //Write to HTML format
    255 $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
    256 $objWriter->setSheetIndex(0);
    257 $objWriter->save(str_replace('.php', '.htm', __FILE__));
    258 
    259 写pdf
    260 require_once("05featuredemo.inc.php");
    261 require_once('Classes/PHPExcel/IOFactory.php');
    262 //Write to PDF format
    263 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
    264 $objWriter->setSheetIndex(0);
    265 $objWriter->save(str_replace('.php', '.pdf', __FILE__));
    266 //Echo memory peak usage
    267 echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB
    ";
    View Code

    PHPExcel 中文使用手册详解

    https://www.cnblogs.com/heijinli/p/11044211.html

  • 相关阅读:
    模式识别及相关领域的重要期刊列表转载
    Spotify敏捷模式详解三部曲第三篇:工程文化
    Spotify敏捷模式详解三部曲第二篇:研发过程
    Scrum工具Leanoo思维导图
    真心推荐的项目管理工具
    Spotify敏捷模式详解三部曲第一篇:研发团队
    敏捷开发必须要选的scrum工具
    什么是用户故事?用Leangoo怎么管理?
    Scrum工具Leangoo“免费版”与“企业版”对比
    参加scrum中文网ScrumMaster认证培训
  • 原文地址:https://www.cnblogs.com/jiayu123/p/12047817.html
Copyright © 2020-2023  润新知