ThinkPHP中PHPExcel的使用

当然是在你需要的地方写代码。我只写样例,你看懂了就可以灵活的使用。

  1.           vendor("PHPExcel.PHPExcel");//如果这里提示类不存在,肯定是你文件夹名字不对。
  2.           $objPHPExcel = new \PHPExcel();//这里要注意‘\’ 要有这个。因为版本是3.1.2了。
  3.           $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);//设置保存版本格式

  4. //接下来就是写数据到表格里面去
  5. $list =你从数据库查出来的的数据
  6.  foreach ($list as $key => $value) {
  7.                   $i=$key+1;//表格是从1开始的
  8.                   $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,  $value['name']);//这里是设置A1单元格的内容
  9.                   $objPHPExcel->getActiveSheet()->setCellValue('B'.$i,  $value['id']);////这里是设置B1单元格的内容
  10.                   //以此类推,可以设置C D E F G看你需要了。
  11.                   
  12. }

  13. //接下来当然是下载这个表格了,在浏览器输出就好了
  14.           header("Pragma: public");
  15.           header("Expires: 0");
  16.           header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  17.           header("Content-Type:application/force-download");
  18.           header("Content-Type:application/vnd.ms-execl");
  19.           header("Content-Type:application/octet-stream");
  20.           header("Content-Type:application/download");;
  21.           header('Content-Disposition:attachment;filename=文件名称.xls');
  22.           header("Content-Transfer-Encoding:binary");
  23.           $objWriter->save('文件名称.xls');
  24.           $objWriter->save('php://output');

  25. 到这里你就已经完成了。如果有哪里不懂可以加我QQ164418960。或者留言
  26.             
复制代码

第三 : 上面只是完成了流程而已。但是不可能这样简单而已。我们可以设置很多东西。看自己需要来设置了。

  1. 设置excel的属性:
  2. 创建人
  3. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  4. 最后修改人
  5. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  6. 标题
  7. $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
  8. 题目
  9. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
  10. 描述
  11. $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  12. 关键字
  13. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  14. 种类
  15. $objPHPExcel->getProperties()->setCategory("Test result file");
复制代码
    1. 设置当前的sheet
    2. $objPHPExcel->setActiveSheetIndex(0);
    3. 设置sheetname
    4. $objPHPExcel->getActiveSheet()->setTitle('Simple');
    5. 设置单元格的值
    6. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String');
    7. $objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
    8. $objPHPExcel->getActiveSheet()->setCellValue('A3', true);
    9. $objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)');
    10. $objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)');
    11. 合并单元格
    12. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
    13. 分离单元格
    14. $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28');

    15. 保护cell
    16. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
    17. $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
    18. 设置格式
    19. // Set cell number formats
    20. echo date('H:i:s') . " Set cell number formats\n";
    21. $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
    22. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
    23. 设置宽width
    24. // Set column widths
    25. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
    26. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
    27. 设置font
    28. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
    29. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
    30. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
    31. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
    32. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
    33. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
    34. $objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true);
    35. $objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);
    36. 设置align
    37. $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    38. $objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    39. $objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    40. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
    41. //垂直居中
    42. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    43. 设置columnborder
    44. $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    45. $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    46. $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    47. $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    48. $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    49. 设置bordercolor
    50. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
    51. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
    52. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
    53. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
    54. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
    55. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');
    56. 设置填充颜色
    57. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    58. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
    59. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    60. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080');
    61. 加图片
    62. $objDrawing = new PHPExcel_Worksheet_Drawing();
    63. $objDrawing->setName('Logo');
    64. $objDrawing->setDescription('Logo');
    65. $objDrawing->setPath('./images/officelogo.jpg');
    66. $objDrawing->setHeight(36);
    67. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    68. $objDrawing = new PHPExcel_Worksheet_Drawing();
    69. $objDrawing->setName('Paid');
    70. $objDrawing->setDescription('Paid');
    71. $objDrawing->setPath('./images/paid.png');
    72. $objDrawing->setCoordinates('B15');
    73. $objDrawing->setOffsetX(110);
    74. $objDrawing->setRotation(25);
    75. $objDrawing->getShadow()->setVisible(true);
    76. $objDrawing->getShadow()->setDirection(45);
    77. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    78. //处理中文输出问题
    79. 需要将字符串转化为UTF-8编码,才能正常输出,否则中文字符将输出为空白,如下处理:
    80.  $str  = iconv('gb2312', 'utf-8', $str);
    81. 或者你可以写一个函数专门处理中文字符串:
    82. function convertUTF8($str)
    83. {
    84.    if(empty($str)) return '';
    85.    return  iconv('gb2312', 'utf-8', $str);
    86. }

posted on 2016-09-13 17:24  Mr.风的影子  阅读(1393)  评论(0编辑  收藏  举报

导航