PHPの作成と編集Excelスプレッドシート法

純粋使用するにはPHPを Excelスプレッドシートを作成または編集するために、我々は、XLS、XLSX、ODSやCSVなどのスプレッドシート形式の多くを読み書きできるPHPExcelライブラリを使用します。php_zip、php_xmlとphp_gd2:我々は継続する前に、慎重バージョンのPHP 5.2以降およびサーバーにインストールされている以下のPHP拡張を確認してください。

スプレッドシートを作成します。

PHPは、Excelスプレッドシートにデータをエクスポートするために使用される最も一般的なのアプリケーションの一例であるとのスプレッドシートを作成します。次のコードをチェックし、サンプルのExcelスプレッドシートを作成PHPExcelを使用する方法を学びます:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

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

あなたはそれをサーバーに保存するのではなく、スプレッドシートをダウンロードしたい場合は、次の手順を実行します。

1

2

3

4

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で同様のスプレッドシートを作成します。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

スプレッドシートを印刷する準備ができました

スプレッドシートを印刷するための準備をするために、我々は、用紙の向き、大きさや余白を設定します:

1

2

3

4

5

6

$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パッケージを確認します(ここでは)と作曲を通してそれをインストールします。インストールが完了したら、Excelのスプレッドシートにデータベースからデータをエクスポートするには、次のコードを使用することができます。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

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

おすすめ

転載: blog.csdn.net/zhujibcom/article/details/82710517