PHPExcel中文帮助手册|PHPExcel使用方法

  1 下面是总结的几个使用方法
  2 include 'PHPExcel.php';
  3 include 'PHPExcel/Writer/Excel2007.php';
  4 //或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的
  5 创建一个excel
  6 $objPHPExcel = new PHPExcel();
  7 保存excel—2007格式
  8 $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  9 //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式
 10 $objWriter->save("xxx.xlsx");
 11 直接输出到浏览器
 12 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
 13 header("Pragma: public");
 14 header("Expires: 0″);
 15 header("Cache-Control:must-revalidate, post-check=0, pre-check=0″);
 16 header("Content-Type:application/force-download");
 17 header("Content-Type:application/vnd.ms-execl");
 18 header("Content-Type:application/octet-stream");
 19 header("Content-Type:application/download");;
 20 header('Content-Disposition:attachment;filename="resume.xls"');
 21 header("Content-Transfer-Encoding:binary");
 22 $objWriter->save('php://output');
 23 ——————————————————————————————————————–
 24 设置excel的属性:
 25 创建人
 26 $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
 27 最后修改人
 28 $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
 29 标题
 30 $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
 31 题目
 32 $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
 33 描述
 34 $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
 35 关键字
 36 $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
 37 种类
 38 $objPHPExcel->getProperties()->setCategory("Test result file");
 39 ——————————————————————————————————————–
 40 设置当前的sheet
 41 $objPHPExcel->setActiveSheetIndex(0);
 42 设置sheet的name
 43 $objPHPExcel->getActiveSheet()->setTitle('Simple');
 44 设置单元格的值
 45 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String');
 46 $objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
 47 $objPHPExcel->getActiveSheet()->setCellValue('A3', true);
 48 $objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)');
 49 $objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)');
 50 合并单元格
 51 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
 52 分离单元格
 53 $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28');
 54 
 55 保护cell
 56 $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
 57 $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
 58 设置格式
 59 // Set cell number formats
 60 echo date('H:i:s') . " Set cell number formats\n";
 61 $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
 62 $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
 63 设置宽width
 64 // Set column widths
 65 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
 66 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
 67 设置font
 68 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
 69 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
 70 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
 71 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
 72 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
 73 $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
 74 $objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true);
 75 $objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);
 76 设置align
 77 $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
 78 $objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
 79 $objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
 80 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
 81 //垂直居中
 82 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
 83 设置column的border
 84 $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
 85 $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
 86 $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
 87 $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
 88 $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
 89 设置border的color
 90 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
 91 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
 92 $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
 93 $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
 94 $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
 95 $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');
 96 设置填充颜色
 97 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
 98 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
 99 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
100 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080');
101 加图片
102 $objDrawing = new PHPExcel_Worksheet_Drawing();
103 $objDrawing->setName('Logo');
104 $objDrawing->setDescription('Logo');
105 $objDrawing->setPath('./images/officelogo.jpg');
106 $objDrawing->setHeight(36);
107 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
108 $objDrawing = new PHPExcel_Worksheet_Drawing();
109 $objDrawing->setName('Paid');
110 $objDrawing->setDescription('Paid');
111 $objDrawing->setPath('./images/paid.png');
112 $objDrawing->setCoordinates('B15');
113 $objDrawing->setOffsetX(110);
114 $objDrawing->setRotation(25);
115 $objDrawing->getShadow()->setVisible(true);
116 $objDrawing->getShadow()->setDirection(45);
117 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
118 //处理中文输出问题
119 需要将字符串转化为UTF-8编码,才能正常输出,否则中文字符将输出为空白,如下处理:
120  $str  = iconv('gb2312', 'utf-8', $str);
121 或者你可以写一个函数专门处理中文字符串:
122 function convertUTF8($str)
123 {
124    if(empty($str)) return '';
125    return  iconv('gb2312', 'utf-8', $str);
126 }
127 //从数据库输出数据处理方式
128 从数据库读取数据如:
129 $db = new Mysql($dbconfig);
130 $sql = "SELECT * FROM  表名";
131 $row = $db->GetAll($sql);  // $row 为二维数组
132 $count = count($row);
133 for ($i = 2; $i <= $count+1; $i++) {
134  $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, convertUTF8($row[$i-2][1]));
135  $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, convertUTF8($row[$i-2][2]));
136  $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, convertUTF8($row[$i-2][3]));
137  $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, convertUTF8($row[$i-2][4]));
138  $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, convertUTF8(date("Y-m-d", $row[$i-2][5])));
139  $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, convertUTF8($row[$i-2][6]));
140  $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, convertUTF8($row[$i-2][7]));
141  $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, convertUTF8($row[$i-2][8]));
142 }
143  
144 在默认sheet后,创建一个worksheet
145 echo date('H:i:s') . " Create new Worksheet object\n";
146 $objPHPExcel->createSheet();
147 $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
148 $objWriter-save('php://output');

 转自:http://www.cnblogs.com/zxf100/p/6963445.html

posted @ 2017-11-26 17:24  huohuohiahia  阅读(717)  评论(0编辑  收藏  举报