本篇文章給大家分享的是有關(guān)如何使用PHP創(chuàng)建和編輯Excel表格,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。

創(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)建電子表格類(lèi)似:
// 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');php的框架:1、Laravel,Laravel是一款免費(fèi)并且開(kāi)源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專(zhuān)業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開(kāi)源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。
以上就是如何使用PHP創(chuàng)建和編輯Excel表格,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:如何使用PHP創(chuàng)建和編輯Excel表格-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.yijiale78.com/article2/dgshoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站制作、搜索引擎優(yōu)化、網(wǎng)站維護(hù)、服務(wù)器托管、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容