一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP中創建和編輯Excel表格的方法

PHP中創建和編輯Excel表格的方法

2019-09-06 14:05二哈少爺 PHP教程

本文通過實例代碼給大家介紹了如何在php中使用PHPExcel庫實現創建和編輯Excel表格的方法,希望大家能夠喜歡

要使用純PHP創建或編輯Excel電子表格,我們將使用PHPExcel庫,它可以讀寫許多電子表格格式,包括xls,xlsx,ods和csv。在我們繼續之前,仔細檢查您的服務器上是否有PHP 5.2或更高版本以及安裝了以下PHP擴展:php_zip,php_xml和php_gd2。

創建電子表格

創建電子表格是PHP應用程序中最常見的用例之一,用于將數據導出到Excel電子表格。查看以下代碼,了解如何使用PHPExcel創建示例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');

如果要下載電子表格而不是將其保存到服務器,請執行以下操作:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');

編輯現有電子表格

在PHP中編輯電子表格與創建電子表格類似:

// 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');

準備電子表格進行打印

要準備電子表格進行打印,我們將設置紙張方向,尺寸和邊距:

$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庫也可以在Laravel框架中使用。查看以下PHP包(此處)并通過Composer安裝它。完成安裝步驟后,您可以使用以下代碼將數據從數據庫導出到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');

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎永久在线精品国产馆v视影院 | 波多野结衣在线中文字幕 | 天天草人人草 | 狠狠久久久久综合网 | 国产精品一区二区国产 | 欧美乱理伦另类视频 | 成人动漫影院 | 国产一区二区视频在线播放 | www.日日操 | 成人啪啪漫画羞羞漫画www网站 | 日本特黄一级午夜剧场毛片 | 欧美一级鲁丝片免费看 | 国产盗摄女厕美女嘘嘘 | 99精品国产高清一区二区三区香蕉 | 日本乱中文字幕系列在线观看 | 国产婷婷综合丁香亚洲欧洲 | 国产日本韩国不卡在线视频 | 强漂亮白丝女教师小说 | 青青青青在线视频 | 日本一卡=卡三卡免费 | 白丝校花掀起短裙呻吟小说 | 99久久国产综合精品女小说 | 插入肥臀| 国产亚洲精品aaa大片 | 欧美日韩亚毛片免费观看 | 日本艳鉧动漫1~6完整版在 | vod国产成人精品视频 | 天天插综合网 | 亚洲精品一区制服丝袜 | 亚洲六月丁香六月婷婷色伊人 | 国产真实一区二区三区 | 日本高清在线观看天码888 | 4455四色永久免费 | 色女的乖男人 | 成人免费视频一区二区三区 | 精品网站一区二区三区网站 | 男人躁女人p | 国产成人精品午夜视频' | 嫩草视频在线观看视频播放 | 国产在线综合网 | 毛片免费的 |