PHP中創(chuàng)建和編輯Excel表格的方法
要使用純PHP創(chuàng)建或編輯Excel電子表格,我們將使用PHPExcel庫(kù),它可以讀寫許多電子表格格式,包括xls,xlsx,ods和csv。在我們繼續(xù)之前,仔細(xì)檢查您的服務(wù)器上是否有PHP 5.2或更高版本以及安裝了以下PHP擴(kuò)展:php_zip,php_xml和php_gd2。
創(chuàng)建電子表格
創(chuàng)建電子表格是PHP應(yīng)用程序中最常見(jiàn)的用例之一,用于將數(shù)據(jù)導(dǎo)出到Excel電子表格。查看以下代碼,了解如何使用PHPExcel創(chuàng)建示例Excel電子表格:
// Include PHPExcel library and create its object
require('PHPExcel.php');
$phpExcel = new PHPExcel;
// Set default font to Arial
$phpExcel->getDefaultStyle()->getFont()->setName('Arial');
// Set default font size to 12
$phpExcel->getDefaultStyle()->getFont()->setSize(12);
// Set spreadsheet properties – title, creator and description
$phpExcel ->getProperties()->setTitle("Product list");
$phpExcel ->getProperties()->setCreator("Voja Janjic");
$phpExcel ->getProperties()->setDescription("PHP Excel spreadsheet testing.");
// Create the PHPExcel spreadsheet writer object
// We will create xlsx file (Excel 2007 and above)
$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
// When creating the writer object, the first sheet is also created
// We will get the already created sheet
$sheet = $phpExcel ->getActiveSheet();
// Set sheet title
$sheet->setTitle('My product list');
// Create spreadsheet header
$sheet ->getCell('A1')->setValue('Product');
$sheet ->getCell('B1')->setValue('Quanity');
$sheet ->getCell('C1')->setValue('Price');
// Make the header text bold and larger
$sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14);
// Insert product data
// Autosize the columns
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
// Save the spreadsheet
$writer->save('products.xlsx');
如果要下載電子表格而不是將其保存到服務(wù)器,請(qǐng)執(zhí)行以下操作:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
編輯現(xiàn)有電子表格
在PHP中編輯電子表格與創(chuàng)建電子表格類似:
// Include PHPExcel library and create its object
require('PHPExcel.php');
// Load an existing spreadsheet
$phpExcel = PHPExcel_IOFactory::load('products.xlsx');
// Get the first sheet
$sheet = $phpExcel ->getActiveSheet();
// Remove 2 rows starting from the row 2
$sheet ->removeRow(2,2);
// Insert one new row before row 2
$sheet->insertNewRowBefore(2, 1);
// Create the PHPExcel spreadsheet writer object
// We will create xlsx file (Excel 2007 and above)
$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
// Save the spreadsheet
$writer->save('products.xlsx');
準(zhǔn)備電子表格進(jìn)行打印
要準(zhǔn)備電子表格進(jìn)行打印,我們將設(shè)置紙張方向,尺寸和邊距:
$sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); $sheet -> getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); $sheet->getPageMargins()->setTop(1); $sheet ->getPageMargins()->setRight(0.75); $sheet ->getPageMargins()->setLeft(0.75); $sheet ->getPageMargins()->setBottom(1);
將PHPExcel與Laravel一起使用
PHPExcel庫(kù)也可以在Laravel框架中使用。查看以下PHP包(此處)并通過(guò)Composer安裝它。完成安裝步驟后,您可以使用以下代碼將數(shù)據(jù)從數(shù)據(jù)庫(kù)導(dǎo)出到Excel電子表格中:
Excel::create('Products', function($excel) {
// Set the title
$excel->setTitle('Product list');
// Set the creator
$excel->setCreator('Voja Janjic');
// Set description
$excel->setDescription('PHP Excel spreadsheet testing');
$excel->sheet('Products', function($sheet) {
// Get data from the database
$products = Product::all();
// Generate header row
$sheet->row(1, array(
'ID',
'Product',
'Price',
'Quantity',
));
// Generate data rows
$i = 2;
foreach($products as $product) {
$sheet->row($i, array(
$product->product_id,
$product->product_name,
$product->price,
$variety->quantity,
));
$i++;
}
});
})->export('xlsx');
- PHPExcel實(shí)現(xiàn)表格導(dǎo)出功能示例【帶有多個(gè)工作sheet】
- Thinkphp5+PHPExcel實(shí)現(xiàn)批量上傳表格數(shù)據(jù)功能
- 使用PHPExcel實(shí)現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- php上傳excel表格并獲取數(shù)據(jù)
- php通過(guò)PHPExcel導(dǎo)入Excel表格到MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
- thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
- php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼
- PHP處理excel cvs表格的方法實(shí)例介紹
- PHP5.6讀寫excel表格文件操作示例
相關(guān)文章
Yii使用DeleteAll連表刪除出現(xiàn)報(bào)錯(cuò)問(wèn)題的解決方法
這篇文章主要介紹了Yii使用DeleteAll連表刪除出現(xiàn)報(bào)錯(cuò)問(wèn)題的解決方法,分析了相關(guān)的SQL語(yǔ)句及出現(xiàn)錯(cuò)誤的原因與改正方法,需要的朋友可以參考下2016-07-07
給WordPress的編輯后臺(tái)添加提示框的代碼實(shí)例分享
這篇文章主要介紹了給WordPress的編輯后臺(tái)添加提示框的代碼實(shí)例分享,即制作一個(gè)鼠標(biāo)指向后顯示詳細(xì)信息的效果,需要的朋友可以參考下2015-12-12
Thinkphp 框架擴(kuò)展之類庫(kù)擴(kuò)展操作詳解
這篇文章主要介紹了Thinkphp 框架擴(kuò)展之類庫(kù)擴(kuò)展操作,結(jié)合實(shí)例形式分析了Thinkphp 類庫(kù)擴(kuò)展的相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例
這篇文章主要介紹了PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例,本文講解SplFileInfo用來(lái)獲取文件詳細(xì)信息、SplFileObject遍歷、查找指定行、寫入csv文件等內(nèi)容,需要的朋友可以參考下2015-05-05
解決Laravel5.2 Auth認(rèn)證退出失效的問(wèn)題
今天在做登陸認(rèn)證時(shí)遇到了個(gè)問(wèn)題,以為是我的解決方法,希望對(duì)大家有所幫助吧2019-10-10
ThinkPHP水印功能實(shí)現(xiàn)修復(fù)PNG透明水印并增加JPEG圖片質(zhì)量可調(diào)整
這篇文章主要介紹了ThinkPHP水印功能實(shí)現(xiàn)修復(fù)PNG透明水印并增加JPEG圖片質(zhì)量可調(diào)整,包含了對(duì)多層水印設(shè)置代碼的修改,修改的部分在注釋里有較為詳細(xì)的說(shuō)明,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
PHP下用Swoole實(shí)現(xiàn)Actor并發(fā)模型的方法
這篇文章主要介紹了PHP下用Swoole實(shí)現(xiàn)Actor并發(fā)模型的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

