萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 使用PHPExcel操作Excel用法實例分析

使用PHPExcel操作Excel用法實例分析

 這篇文章主要介紹了使用PHPExcel操作Excel用法,實例分析了使用PHPExcel進行讀寫及生成等常用技巧,非常具有實用價值,需要的朋友可以參考下

   

本文實例分析了使用PHPExcel操作Excel用法。分享給大家供大家參考。具體分析如下:

PHPExcel下載地址:
http://www.codeplex.com/PHPExcel
http://www.phpexcel.net
開發包Tests目錄有詳細使用實例 支持中文,注意文件編碼 文件保存為utf-8

1.header部分:

? 1 2 3 4 header("Content-Type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=sample.xls"); header("Pragma:no-cache"); header("Expires:0");

2.寫excel:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 //Include class require_once('Classes/PHPExcel.php'); require_once('Classes/PHPExcel/Writer/Excel2007.php'); $objPHPExcel = new PHPExcel();   //Set properties 設置文件屬性 $objPHPExcel->getProperties()->setCreator("Maarten Balliauw"); $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw"); $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."); $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); $objPHPExcel->getProperties()->setCategory("Test result file");   //Add some data 添加數據 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');//可以指定位置 $objPHPExcel->getActiveSheet()->setCellValue('A2', true); $objPHPExcel->getActiveSheet()->setCellValue('A3', false); $objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!'); $objPHPExcel->getActiveSheet()->setCellValue('B3', 2); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello'); $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');   //循環 for($i = 1;$i<200;$i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, 'Test value'); }   //日期格式化 $objPHPExcel->getActiveSheet()->setCellValue('D1', time()); $objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);   //Add comment 添加注釋 $objPHPExcel->getActiveSheet()->getComment('E11')->setAuthor('PHPExcel'); $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('PHPExcel:'); $objCommentRichText->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun("rn"); $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('Total amount on the current invoice, excluding VAT.');   //Add rich-text string 添加文字 可設置樣式 $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18') ); $objRichText->createText('This invoice is '); $objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month'); $objPayable->getFont()->setBold(true); $objPayable->getFont()->setItalic(true); $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) ); $objRichText->createText(', unless specified otherwise on the invoice.');
copyright © 萬盛學電腦網 all rights reserved