PHP create and edit Excel spreadsheet method

To use the pure PHP to create or edit an Excel spreadsheet, we will use PHPExcel library that can read and write a lot of spreadsheet formats including xls, xlsx, ods and csv. Before we continue, carefully check the version PHP 5.2 or later and the following PHP extensions installed on your server: php_zip, php_xml and php_gd2.

Create a spreadsheet

Create a spreadsheet with PHP is one example of an application of the most common, used to export data to an Excel spreadsheet. Check the following code, learn how to use PHPExcel create a sample Excel spreadsheet:

?

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

If you want to download a spreadsheet instead of saving it to the server, do the following:

?

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

Edit an existing spreadsheet

Edit a spreadsheet and create a spreadsheet similar in 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');

Ready to print a spreadsheet

To prepare for printing a spreadsheet, we will set the paper orientation, size and margins:

?

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 used together with Laravel

PHPExcel library can also be used Laravel framework. Review the following PHP package (here) and install it through Composer. After installation is complete, you can use the following code to export data from a database into an Excel spreadsheet:

?

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

Guess you like

Origin blog.csdn.net/zhujibcom/article/details/82710517