001
003 | 即定义一个PHPEXCEL对象,并设置EXCEL对象内显示内容</p> |
011 | require_once dirname( __FILE__ ) .
'PHPExcel.php' ; |
013 | $objPHPExcel = new PHPExcel(); |
015 | $objPHPExcel ->getProperties()->setCreator( "Sam.c" ) |
016 | ->setLastModifiedBy( "Sam.c Test" ) |
017 | ->setTitle( "Microsoft Office Excel Document" ) |
019 | ->setDescription( "Test" ) |
020 | ->setKeywords( "Test" ) |
021 | ->setCategory( "Test result file" ); |
024 | $objPHPExcel ->setActiveSheetIndex(0); |
026 | $objPHPExcel ->getActiveSheet()->setTitle(iconv( 'gbk' , 'utf-8' , 'phpexcel测试' )); |
028 | $objPHPExcel ->getDefaultStyle()->getFont()->setName(iconv( 'gbk' , 'utf-8' , '宋体' )); |
029 | $objPHPExcel ->getDefaultStyle()->getFont()->setSize(10); |
037 | if ( $m_exportType == "excel" ){ |
038 | $objWriter
=PHPExcel_IOFactory::createWriter( $objPHPExcel , 'Excel5' ); |
040 | header( "Pragma: public" ); |
041 | header( "Expires: 0" ); |
042 | header( "Cache-Control:must-revalidate,post-check=0,pre-check=0" ); |
043 | header( "Content-Type:application/force-download" ); |
044 | header( "Content-Type: application/vnd.ms-excel;" ); |
045 | header( "Content-Type:application/octet-stream" ); |
046 | header( "Content-Type:application/download" ); |
047 | header( "Content-Disposition:attachment;filename=" . $filename ); |
048 | header( "Content-Transfer-Encoding:binary" ); |
049 | $objWriter ->save( "php://output" ); |
052 | if ( $m_exportType == "pdf" ){ |
053 | $objWriter
=PHPExcel_IOFactory::createWriter( $objPHPExcel , 'PDF' ); |
054 | $objWriter ->setSheetIndex(0); |
055 | header( "Pragma: public" ); |
056 | header( "Expires: 0" ); |
057 | header( "Cache-Control:must-revalidate,post-check=0,pre-check=0" ); |
058 | header( "Content-Type:application/force-download" ); |
059 | header( "Content-Type: application/pdf" ); |
060 | header( "Content-Type:application/octet-stream" ); |
061 | header( "Content-Type:application/download" ); |
062 | header( "Content-Disposition:attachment;filename=" . $m_strOutputPdfFileName ); |
063 | header( "Content-Transfer-Encoding:binary" ); |
064 | $objWriter ->save( "php://output" ); |
073 | $objPHPExcel ->getActiveSheet()->getColumnDimension( 'A' )->setWidth(15); |
080 | $objPHPExcel ->getActiveSheet()->getRowDimension( '6' )->setRowHeight(30); |
087 | $objPHPExcel ->getActiveSheet()->mergeCells( 'A1:P1' ); |
090 | <p>设置A1单元格加粗,居中:<br /> |
103 | 'horizontal'
=>PHPExcel_Style_Alignment::HORIZONTAL_CENTER, |
107 | $objPHPExcel ->getActiveSheet()->getStyle( 'A1' )->applyFromArray( $styleArray1 ); |
109 | $objPHPExcel ->getActiveSheet()->getStyle( 'B1' )->getFont()->setBold(true); |
117 | $objPHPExcel ->getActiveSheet()->setCellValue( 'A1' , 'Hello Baby' ); |
120 | <p>设置单元格样式(水平/垂直居中):<br /> |
124 | $objPHPExcel ->getActiveSheet()->getStyle( 'A1' )->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); |
125 | $objPHPExcel ->getActiveSheet()->getStyle( 'A1' )->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); |
128 | <p>设置单元格样式(黑色字体):<br /> |
132 | $objPHPExcel ->getActiveSheet()->getStyle( 'H5' )->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色 |
139 | $objPHPExcel ->getActiveSheet()->getStyle( 'H5' )->getFill()->getStartColor()->setARGB( '00ff99cc' ); // 将背景设置为浅粉色 |
142 | <p>设置单元格格式(数字格式):<br /> |
146 | $objPHPExcel ->getActiveSheet()->getStyle( 'F' . $iLineNumber )->getNumberFormat()->setFormatCode( '0.000' ); |
154 | $objDrawing = new PHPExcel_Worksheet_Drawing(); |
155 | $objDrawing ->setName( 'Logo' ); |
156 | $objDrawing ->setDescription( 'Logo' ); |
157 | $objDrawing ->setPath( 'test.jpg' ); |
158 | $objDrawing ->setWidth(400); |
159 | $objDrawing ->setHeight(123); |
160 | $objDrawing ->setCoordinates( 'J1' ); |
161 | $objDrawing ->setWorksheet( $objPHPExcel ->getActiveSheet()); |
169 | $objPHPExcel ->getActiveSheet()->setCellValue( 'H8' ,iconv( 'gbk' , 'utf-8' , '燕南天' )); |
176 | $styleThinBlackBorderOutline = array ( |
179 | 'style'
=>PHPExcel_Style_Border::BORDER_THIN, //设置border样式 |
180 | //'style' =>PHPExcel_Style_Border::BORDER_THICK,另一种样式 |
181 | 'color'
=> array ( 'argb' => 'FF000000' ), //设置border颜色 |
185 | $objPHPExcel ->getActiveSheet()->getStyle( 'A4:E10' )->applyFromArray( $styleThinBlackBorderOutline ); |
188 | $objExcel ->createSheet(); |
189 | $objActSheet
= $objExcel ->getSheet( $s ); |
190 | $objActSheet ->setTitle( '表' . $GSheet ); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理