中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

個人買域名有什么用徐州seo培訓(xùn)

個人買域名有什么用,徐州seo培訓(xùn),模具鋼東莞網(wǎng)站建設(shè),網(wǎng)站建設(shè)的系統(tǒng)設(shè)計PhpSpreadsheet是一個純PHP編寫的組件庫,它使用現(xiàn)代PHP寫法,代碼質(zhì)量和性能比PHPExcel高不少,完全可以替代PHPExcel(PHPExcel已不再維護(hù))。使用PhpSpreadsheet可以輕松讀取和寫入Excel文檔,支持Excel的所有…

PhpSpreadsheet是一個純PHP編寫的組件庫,它使用現(xiàn)代PHP寫法,代碼質(zhì)量和性能比PHPExcel高不少,完全可以替代PHPExcel(PHPExcel已不再維護(hù))。使用PhpSpreadsheet可以輕松讀取和寫入Excel文檔,支持Excel的所有操作。

1. 初識PhpSpreadsheet

軟件依賴

要使用PhpSpreadsheet需要滿足以下條件:

PHP5.6或更改版本,推薦PHP7
支持php_zip擴(kuò)展
支持php_xml擴(kuò)展
支持php_gd2擴(kuò)展

安裝

現(xiàn)在開始,創(chuàng)建項目目錄/PHPExcel,進(jìn)入項目目錄。

使用composer安裝:

composer require phpoffice/phpspreadsheet

使用

在項目目錄下新建/public目錄,在public目錄下創(chuàng)建示例文件test.php,編輯test.php,用以下代碼。

<?phprequire '../vendor/autoload.php';use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();$sheet->setCellValue('A1', 'Welcome to Helloweba.');$writer = new Xlsx($spreadsheet);$writer->save('hello.xlsx');

運行代碼,你會發(fā)現(xiàn)在目錄下生成一個hello.xlsx文件,打開Excel文件,你會看到Excel中的單元格A1中有“Welcome to Helloweba.”內(nèi)容。當(dāng)然你可以對單元格樣式諸如顏色、背景、寬度、字體等等進(jìn)行設(shè)置,這些會在接下來的幾節(jié)中講到。
PhpSpreadsheet特性

支持讀取.xls,.xlsx,.html,.csv等格式文件,支持寫入導(dǎo)出.xls,.xlsx,.html,.csv,.pdf格式文件。
提供豐富的API,提供單元格樣式設(shè)置、Excel表格屬性設(shè)置、圖表設(shè)置等等諸多功能。使用PhpSpreadsheet完全可以生成一個外觀結(jié)構(gòu)都滿足你的Excel表格文件。
卓越的性能,尤其在PHP7上表現(xiàn)優(yōu)異,比PHPExcel強大很多。

2. 使用PhpSpreadsheet將Excel導(dǎo)入到MySQL數(shù)據(jù)庫

導(dǎo)入Excel

思路:使用PhpSpreadsheet讀取Excel表格中的有用信息,然后組裝成sql語句,最后批量插入到MySQL表中。

  require 'vendor/autoload.php';include('conn.php'); //連接數(shù)據(jù)庫$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');$reader->setReadDataOnly(TRUE);$spreadsheet = $reader->load('students.xlsx'); //載入excel表格$worksheet = $spreadsheet->getActiveSheet();$highestRow = $worksheet->getHighestRow(); // 總行數(shù)$highestColumn = $worksheet->getHighestColumn(); // 總列數(shù)$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5$lines = $highestRow - 2;if ($lines <= 0) {exit('Excel表格中沒有數(shù)據(jù)');}$sql = "INSERT INTO `t_student` (`name`, `chinese`, `maths`, `english`) VALUES ";for ($row = 3; $row <= $highestRow; ++$row) {$name = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); //姓名$chinese = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); //語文$maths = $worksheet->getCellByColumnAndRow(3, $row)->getValue(); //數(shù)學(xué)$english = $worksheet->getCellByColumnAndRow(4, $row)->getValue(); //外語$sql .= "('$name','$chinese','$maths','$english'),";}$sql = rtrim($sql, ","); //去掉最后一個,號try {$db->query($sql);echo 'OK';} catch (Exception $e) {echo $e->getMessage();}

3. 使用PhpSpreadsheet將數(shù)據(jù)導(dǎo)出為Excel文件

一、設(shè)置表頭

首先我們引入自動加載PhpSpreadsheet庫,然后實例化,設(shè)置工作表標(biāo)題名稱為:學(xué)生成績表,接著設(shè)置表頭內(nèi)容。表頭分為兩行,第一行是表格的名稱,第二行數(shù)表格列名稱。最后我們將第一行單元格進(jìn)行合并,并設(shè)置表頭內(nèi)容樣式:字體、對齊方式等。

require 'vendor/autoload.php';use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;include('conn.php'); //連接數(shù)據(jù)庫$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
//設(shè)置工作表標(biāo)題名稱
$worksheet->setTitle('學(xué)生成績表');//表頭
//設(shè)置單元格內(nèi)容
$worksheet->setCellValueByColumnAndRow(1, 1, '學(xué)生成績表');
$worksheet->setCellValueByColumnAndRow(1, 2, '姓名');
$worksheet->setCellValueByColumnAndRow(2, 2, '語文');
$worksheet->setCellValueByColumnAndRow(3, 2, '數(shù)學(xué)');
$worksheet->setCellValueByColumnAndRow(4, 2, '外語');
$worksheet->setCellValueByColumnAndRow(5, 2, '總分');//合并單元格
$worksheet->mergeCells('A1:E1');$styleArray = ['font' => ['bold' => true],'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,],
];
//設(shè)置單元格樣式
$worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);$worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);

二、讀取數(shù)據(jù)

我們連接數(shù)據(jù)庫后,直接讀取學(xué)生成績表t_student,然后for循環(huán),設(shè)置每個單元格對應(yīng)的內(nèi)容,計算總成績。注意的是表格中的數(shù)據(jù)是從第3行開始,因為第1,2行是表頭占用了。

然后,我們設(shè)置整個表格樣式,給表格加上邊框,并且居中對齊。

  $sql = "SELECT id,name,chinese,maths,english FROM `t_student`";$stmt = $db->query($sql);$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);$len = count($rows);$j = 0;for ($i=0; $i < $len; $i++) {$j = $i + 3; //從表格第3行開始$worksheet->setCellValueByColumnAndRow(1, $j, $rows[$i]['name']);$worksheet->setCellValueByColumnAndRow(2, $j, $rows[$i]['chinese']);$worksheet->setCellValueByColumnAndRow(3, $j, $rows[$i]['maths']);$worksheet->setCellValueByColumnAndRow(4, $j, $rows[$i]['english']);$worksheet->setCellValueByColumnAndRow(5, $j, $rows[$i]['chinese'] + $rows[$i]['maths'] + $rows[$i]['english']);}$styleArrayBody = ['borders' => ['allBorders' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,'color' => ['argb' => '666666'],],],'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,],];$total_rows = $len + 2;//添加所有邊框/居中$worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);

三、下載保存

強制瀏覽器下載數(shù)據(jù)并保存為Excel文件

$filename = '成績表.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

如想要保存為.xls文件格式的話,可以改下header代碼:

$filename = '成績表.xlsx';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'xls');
$writer->save('php://output');

4. 詳解PhpSpreadsheet設(shè)置單元格

PhpSpreadsheet提供了豐富的API接口,可以設(shè)置諸多單元格以及文檔屬性,包括樣式、圖片、日期、函數(shù)等等諸多應(yīng)用,總之你想要什么樣的Excel表格,PhpSpreadsheet都能做到。
引入了正確的文件并實例化:

use PhpOffice\PhpSpreadsheet\Spreadsheet;$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

字體

第1行代碼將A7至B7兩單元格設(shè)置為粗體字,Arial字體,10號字;第2行代碼將B1單元格設(shè)置為粗體字。

$spreadsheet->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true)->setName('Arial')->setSize(10);;
$spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);

將文字顏色設(shè)置為紅色

$spreadsheet->getActiveSheet()->getStyle('A4')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);

圖片

可以將圖片加載到Excel中

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath('./images/officelogo.jpg');
$drawing->setHeight(36);

列寬

將A列寬度設(shè)置為30(字符):

$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(30);

如果需要自動計算列寬,可以這樣:

$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); 

設(shè)置默認(rèn)列寬為12:

$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);

行高

設(shè)置第10行行高為100pt:

$spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100);

設(shè)置默認(rèn)行高:

$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); 

對齊

將A1單元格設(shè)置為水平居中對齊:

$styleArray = ['alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,],
];
$worksheet->getStyle('A1')->applyFromArray($styleArray); 

合并

將A18到E22合并為一個單元格:

$spreadsheet->getActiveSheet()->mergeCells('A18:E22');

拆分

將合并后的單元格拆分:

$spreadsheet->getActiveSheet()->unmergeCells('A18:E22');

邊框

將B2至G8的區(qū)域添加紅色邊框:

$styleArray = ['borders' => ['outline' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,'color' => ['argb' => 'FFFF0000'],],],
];
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);

工作表標(biāo)題

設(shè)置當(dāng)前工作表標(biāo)題:

$spreadsheet->getActiveSheet()->setTitle('Hello'); 

日期時間

設(shè)置日期格式:

$spreadsheet->getActiveSheet()->setCellValue('D1', '2018-06-15');$spreadsheet->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2);

換行

使用\n進(jìn)行單元格內(nèi)換行,相當(dāng)于(ALT+“Enter”):

$spreadsheet->getActiveSheet()->getCell('A4')->setValue("hello\nworld");
$spreadsheet->getActiveSheet()->getStyle('A4')->getAlignment()->setWrapText(true)

超鏈接

將單元格設(shè)置為超鏈接形式:

$spreadsheet->getActiveSheet()->setCellValue('E6', 'www.helloweba.net');
$spreadsheet->getActiveSheet()->getCell('E6')->getHyperlink()->setUrl('https://www.helloweba.net');

使用函數(shù)

使用SUM計算B5到C5之間單元格的總和。其他函數(shù)同理:最大數(shù)(MAX),最小數(shù)(MIN),平均值(AVERAGE):

$spreadsheet->getActiveSheet()->setCellValue('B7', '=SUM(B5:C5)');

設(shè)置文檔屬性

可以設(shè)置Excel文檔屬性:

  $spreadsheet->getProperties()->setCreator("Helloweba")    //作者->setLastModifiedBy("Yuegg") //最后修改者->setTitle("Office 2007 XLSX Test Document")  //標(biāo)題->setSubject("Office 2007 XLSX Test Document") //副標(biāo)題->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  //描述->setKeywords("office 2007 openxml php") //關(guān)鍵字->setCategory("Test result file"); //分類

此外,除了提供豐富的Excel文件處理接口外,PhpSpreadshee還提供了CSV,PDF,HTML以及XML等文件處理接口。

http://www.risenshineclean.com/news/26853.html

相關(guān)文章:

  • 做網(wǎng)站需要多大的內(nèi)存品牌推廣公司
  • 學(xué)生做網(wǎng)站賺錢網(wǎng)站推廣的常用方法有哪些?
  • 厚街鎮(zhèn)網(wǎng)站仿做seo關(guān)鍵詞排名優(yōu)化制作
  • 泉州網(wǎng)站建設(shè)推廣賺錢的軟件排行
  • 名詞解釋 網(wǎng)站內(nèi)容軟文推廣文章案例
  • 國內(nèi)vps做網(wǎng)站要備案嗎太原seo排名公司
  • 淘寶網(wǎng)站的論壇做的怎么樣寧波seo托管公司
  • 國家企業(yè)信息公示官網(wǎng)入口seo推廣系統(tǒng)排名榜
  • 上海城鄉(xiāng)住房建設(shè)部網(wǎng)站首頁近期的重大新聞
  • 網(wǎng)站建設(shè)有利于優(yōu)化seo教程技術(shù)
  • 鹽城公司做網(wǎng)站自己建站的網(wǎng)站
  • 泗水做網(wǎng)站普通話的順口溜6句
  • 做網(wǎng)站開發(fā)需要的英語水平推廣賺錢軟件
  • 蘭州網(wǎng)站建站搜索引擎優(yōu)化論文3000字
  • 軟件網(wǎng)站開發(fā)實訓(xùn)報告東莞網(wǎng)站排名提升
  • 做圍棋死活題的網(wǎng)站百度關(guān)鍵詞怎么優(yōu)化
  • cms網(wǎng)站開發(fā)網(wǎng)站模板樂事薯片軟文推廣
  • 河北省住房和城鄉(xiāng)建設(shè)廳信用網(wǎng)站知乎營銷推廣
  • wordpress pluto安康地seo
  • 義烏獨立站百度網(wǎng)盤在線登錄
  • 西安做網(wǎng)站的網(wǎng)絡(luò)公司優(yōu)化方案模板
  • 做網(wǎng)站沒有成本的方法網(wǎng)站推廣引流最快方法
  • 網(wǎng)站策劃預(yù)算怎么做南陽seo
  • php充值網(wǎng)站源碼小程序開發(fā)框架
  • 樹莓派上怎么做網(wǎng)站項目推廣渠道有哪些
  • 高端文化網(wǎng)站谷歌地球
  • 如何建設(shè)網(wǎng)頁游戲網(wǎng)站百度指數(shù)上多少就算熱詞
  • 廣州專業(yè)的網(wǎng)站建設(shè)公司哪家好2022年列入傳銷組織最新騙法
  • 昆明營銷型網(wǎng)站建設(shè)華夏思源培訓(xùn)機構(gòu)官網(wǎng)
  • 北碚網(wǎng)站建設(shè)公司在線教育