其實這些插件不只能夠用composer下載,還能夠直接從github上面下載,還能夠直接從官網下載php
前二者在通常狀況下是差很少的,好比thinkphp,html
前二者的區別是composer是有依賴管理,git
github上面下載下來的確定是能運行的,也算是有依賴管理github
二者在通常狀況下是差很少的,好比thinkphp,sql
二者的區別是composer是有依賴管理,thinkphp
github上面下載下來的確定是能運行的,也算是有依賴管理數據庫
直接是在項目的目錄下面引入插件(在vendor的上一級目錄下)windows
PHPExcel是一個PHP類庫,用來幫助咱們簡單、高效實現從Excel讀取Excel的數據和導出數據到Excel。也是咱們平常開發中,常常會遇到的使用場景。好比有個客戶信息表,要批量導出發給同事,咱們就能夠用PHPExcel來快速實現。一樣,若是咱們要利用短信羣發接口去羣發信息,PHPExcel能夠快速導入客戶信息,避免人工錄入信息的麻煩。api
PHPExcel使用教程:數組
首先下載PHPExcel
到https://github.com/PHPOffice/PHPExcel下載PHPExcel,若是不懂得使用git,能夠到這https://codeload.github.com/PHPOffice/PHPExcel/zip/1.8下載壓縮包,懂得的自行用git下載。
下載好文件,解壓能夠獲得以下文件:
PHPExcel
好了,如今咱們就能夠用PHPExcel愉快的讀取和製做表格了!
PHPExcel DEMO1:利用PHPExcel讀取excel信息:
咱們在根目錄建立一個名爲read.php的文件來讀取文檔,建立一個test.xlsx的文件,裏面寫的信息以下:
而後在read.php寫如下代碼:
include ‘./Classes/PHPExcel/IOFactory.php’;
$inputFileName = ‘./test.xls’;
date_default_timezone_set(‘PRC’);
// 讀取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die(‘加載文件發生錯誤:」‘.pathinfo($inputFileName,PATHINFO_BASENAME).'」: ‘.$e->getMessage());
}// 肯定要讀取的sheet,什麼是sheet,看excel的右下角,真的不懂去百度吧
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();// 獲取一行的數據
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray(‘A’ . $row . ‘:’ . $highestColumn . $row, NULL, TRUE, FALSE);
//這裏獲得的rowData都是一行的數據,獲得數據後自行處理,咱們這裏只打出來看看效果
var_dump($rowData);
echo 「
」;}
獲得的數據:
PHPexcel讀取文件
PHPExcel DEMO2:利用PHPExcel導出信息到excel:
<?php /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('PRC'); /** 引入PHPExcel */ require_once dirname(__FILE__) . './Classes/PHPExcel.php'; // 建立Excel文件對象 $objPHPExcel = new PHPExcel(); // 設置文檔信息,這個文檔信息windows系統能夠右鍵文件屬性查看 $objPHPExcel->getProperties()->setCreator("做者簡慶旺") ->setLastModifiedBy("最後更改者") ->setTitle("文檔標題") ->setSubject("文檔主題") ->setDescription("文檔的描述信息") ->setKeywords("設置文檔關鍵詞") ->setCategory("設置文檔的分類"); //根據excel座標,添加數據 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '你好') ->setCellValue('B2', '世界') ->setCellValue('C1', '你好') ->setCellValue('D2', '世界'); // 混雜各類符號, 編碼爲UTF-8 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A4', 'Miscellaneous glyphs') ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); $objPHPExcel->getActiveSheet()->setCellValue('A8',"你好世界"); $objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1); $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true); $value = "-ValueA\n-Value B\n-Value C"; $objPHPExcel->getActiveSheet()->setCellValue('A10', $value); $objPHPExcel->getActiveSheet()->getRowDimension(10)->setRowHeight(-1); $objPHPExcel->getActiveSheet()->getStyle('A10')->getAlignment()->setWrapText(true); $objPHPExcel->getActiveSheet()->getStyle('A10')->setQuotePrefix(true); // 重命名工做sheet $objPHPExcel->getActiveSheet()->setTitle('第一個sheet'); // 設置第一個sheet爲工做的sheet $objPHPExcel->setActiveSheetIndex(0); // 保存Excel 2007格式文件,保存路徑爲當前路徑,名字爲export.xlsx $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save( 'export.xlsx'); // 保存Excel 95格式文件,,保存路徑爲當前路徑, $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('export.xls');
好了,執行後咱們能夠獲得export.xls和export.xlsx。
參考:利用PHPExcel讀取Excel的數據和導出數據到Excel - 簡慶旺 - 博客園
https://www.cnblogs.com/jianqingwang/p/6830892.html
下面是總結的幾個使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的 建立一個excel $objPHPExcel = new PHPExcel(); 保存excel—2007格式 $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式 $objWriter->save("xxx.xlsx"); 直接輸出到瀏覽器 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Pragma: public"); header("Expires: 0″); header("Cache-Control:must-revalidate, post-check=0, pre-check=0″); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename="resume.xls"'); header("Content-Transfer-Encoding:binary"); $objWriter->save('php://output'); ——————————————————————————————————————– 設置excel的屬性: 建立人 $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"); ——————————————————————————————————————– 設置當前的sheet $objPHPExcel->setActiveSheetIndex(0); 設置sheet的name $objPHPExcel->getActiveSheet()->setTitle('Simple'); 設置單元格的值 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String'); $objPHPExcel->getActiveSheet()->setCellValue('A2', 12); $objPHPExcel->getActiveSheet()->setCellValue('A3', true); $objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)'); $objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)'); 合併單元格 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22'); 分離單元格 $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); 保護cell $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection! $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel'); 設置格式 // Set cell number formats echo date('H:i:s') . " Set cell number formats\n"; $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ); 設置寬width // Set column widths $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12); 設置font $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true); 設置align $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); //垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 設置column的border $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 設置border的color $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300'); 設置填充顏色 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080'); 加圖片 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath('./images/officelogo.jpg'); $objDrawing->setHeight(36); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Paid'); $objDrawing->setDescription('Paid'); $objDrawing->setPath('./images/paid.png'); $objDrawing->setCoordinates('B15'); $objDrawing->setOffsetX(110); $objDrawing->setRotation(25); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(45); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //處理中文輸出問題 須要將字符串轉化爲UTF-8編碼,才能正常輸出,不然中文字符將輸出爲空白,以下處理: $str = iconv('gb2312', 'utf-8', $str); 或者你能夠寫一個函數專門處理中文字符串: function convertUTF8($str) { if(empty($str)) return ''; return iconv('gb2312', 'utf-8', $str); } //從數據庫輸出數據處理方式 從數據庫讀取數據如: $db = new Mysql($dbconfig); $sql = "SELECT * FROM 表名"; $row = $db->GetAll($sql); // $row 爲二維數組 $count = count($row); for ($i = 2; $i <= $count+1; $i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, convertUTF8($row[$i-2][1])); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, convertUTF8($row[$i-2][2])); $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, convertUTF8($row[$i-2][3])); $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, convertUTF8($row[$i-2][4])); $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, convertUTF8(date("Y-m-d", $row[$i-2][5]))); $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, convertUTF8($row[$i-2][6])); $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, convertUTF8($row[$i-2][7])); $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, convertUTF8($row[$i-2][8])); } 在默認sheet後,建立一個worksheet echo date('H:i:s') . " Create new Worksheet object\n"; $objPHPExcel->createSheet(); $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5'); $objWriter-save('php://output');
參考:PhpExcel中文幫助手冊|PhpExcel使用方法 - FreeSpider - 博客園https://www.cnblogs.com/freespider/p/3284828.html