方法一:特點(diǎn),簡單,省心, 只適用windows服務(wù)器
以下為引用的內(nèi)容:
02 |
header( "Content-type:application/vnd.ms-excel" ); |
03 |
header( "Content-Disposition:attachment;filename=test_data.xls" ); |
方法二: 引用google code中推薦的小類庫(大體同方法一,比較復(fù)雜點(diǎn))
下載地址: http://code.google.com/p/php-excel/downloads/list
方法三: PHPEXCEL 類庫,功能強(qiáng)大,操作excel很方便,尤其是可以方便的加入圖片,支持jpg gif png格式,支持win Excel2003 ,Win Excel2007.
下載地址:http://www./PHPExcel
下面是總結(jié)的幾個(gè)使用方法
008 |
include 'PHPExcel.php' ; |
010 |
include 'PHPExcel/Writer/Excel2007.php' ; |
020 |
$objExcel = new PHPExcel(); |
030 |
$objProps = $objExcel ->getProperties(); |
031 |
$objProps ->setCreator( "Zeal Li" ); |
032 |
$objProps ->setLastModifiedBy( "Zeal Li" ); |
033 |
$objProps ->setTitle( "Office XLS Test Document" ); |
034 |
$objProps ->setSubject( "Office XLS Test Document, Demo" ); |
035 |
$objProps ->setDescription( "Test document, generated by PHPExcel." ); |
036 |
$objProps ->setKeywords( "office excel PHPExcel" ); |
037 |
$objProps ->setCategory( "Test" ); |
043 |
$objExcel ->setActiveSheetIndex(0); |
046 |
$objActSheet = $objExcel ->getActiveSheet(); |
049 |
$objActSheet ->setTitle( '測試Sheet' ); |
055 |
$objActSheet ->setCellValue( 'A1' , '字符串內(nèi)容' ); |
056 |
$objActSheet ->setCellValue( 'A2' , 26); |
057 |
$objActSheet ->setCellValue( 'A3' , true); |
058 |
$objActSheet ->setCellValue( 'A4' , '=SUM(A2:A2)' ); |
061 |
$objActSheet ->setCellValueExplicit( 'A5' , '847475847857487584' , |
062 |
PHPExcel_Cell_DataType::TYPE_STRING); |
065 |
$objActSheet ->mergeCells( 'B1:C22' ); |
068 |
$objActSheet ->unmergeCells( 'B1:C22' ); |
075 |
$objActSheet ->getColumnDimension( 'B' )->setAutoSize(true); |
076 |
$objActSheet ->getColumnDimension( 'A' )->setWidth(30); |
078 |
$objStyleA5 = $objActSheet ->getStyle( 'A5' ); |
096 |
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); |
099 |
$objFontA5 = $objStyleA5 ->getFont(); |
100 |
$objFontA5 ->setName( 'Courier New' ); |
101 |
$objFontA5 ->setSize(10); |
102 |
$objFontA5 ->setBold(true); |
103 |
$objFontA5 ->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); |
104 |
$objFontA5 ->getColor()->setARGB( 'FF999999' ); |
107 |
$objAlignA5 = $objStyleA5 ->getAlignment(); |
108 |
$objAlignA5 ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); |
109 |
$objAlignA5 ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); |
112 |
$objBorderA5 = $objStyleA5 ->getBorders(); |
113 |
$objBorderA5 ->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); |
114 |
$objBorderA5 ->getTop()->getColor()->setARGB( 'FFFF0000' ); |
115 |
$objBorderA5 ->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); |
116 |
$objBorderA5 ->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); |
117 |
$objBorderA5 ->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); |
120 |
$objFillA5 = $objStyleA5 ->getFill(); |
121 |
$objFillA5 ->setFillType(PHPExcel_Style_Fill::FILL_SOLID); |
122 |
$objFillA5 ->getStartColor()->setARGB( 'FFEEEEEE' ); |
125 |
$objActSheet ->duplicateStyle( $objStyleA5 , 'B1:C22' ); |
130 |
$objDrawing = new PHPExcel_Worksheet_Drawing(); |
131 |
$objDrawing ->setName( 'ZealImg' ); |
132 |
$objDrawing ->setDescription( 'Image inserted by Zeal' ); |
133 |
$objDrawing ->setPath( './zeali.net.logo.gif' ); |
134 |
$objDrawing ->setHeight(36); |
135 |
$objDrawing ->setCoordinates( 'C23' ); |
136 |
$objDrawing ->setOffsetX(10); |
137 |
$objDrawing ->setRotation(15); |
138 |
$objDrawing ->getShadow()->setVisible(true); |
139 |
$objDrawing ->getShadow()->setDirection(36); |
140 |
$objDrawing ->setWorksheet( $objActSheet ); |
144 |
$objExcel ->createSheet(); |
145 |
$objExcel ->getSheet(1)->setTitle( '測試2' ); |
148 |
$objExcel ->getSheet(1)->getProtection()->setSheet(true); |
149 |
$objExcel ->getSheet(1)->protectCells( 'A1:C22' , 'PHPExcel' ); |
155 |
$outputFileName = "output.xls" ; |
160 |
header( "Content-Type: application/force-download" ); |
161 |
header( "Content-Type: application/octet-stream" ); |
162 |
header( "Content-Type: application/download" ); |
163 |
header( 'Content-Disposition:inline;filename="' . $outputFileName . '"' ); |
164 |
header( "Content-Transfer-Encoding: binary" ); |
165 |
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); |
166 |
header( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" ); |
167 |
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); |
168 |
header( "Pragma: no-cache" ); |
169 |
$objWriter ->save( 'php://output' ); |