LoginSignup
1
4

More than 5 years have passed since last update.

PhpSpreadsheet のサンプル

Posted at
xlsx_manipulate.php
<?php
//
//  xlsx_manipulate.php
//
//                  Sep/09/2018
//
// ----------------------------------------------------------------
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;;

// ----------------------------------------------------------------
function xlsx_write_proc ($xlsx_file,$dict_aa)
{
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    $nn = 1;
    foreach ($dict_aa as $key => $value)
        {
        $cell = 'A' . $nn;
        $sheet->setCellValue($cell,$key);
        $cell = 'B' . $nn;
        $sheet->setCellValue($cell,$value['name']);
        $cell = 'C' . $nn;
        $sheet->setCellValue($cell,$value['population']);
        $cell = 'D' . $nn;
        $sheet->setCellValue($cell,$value['date_mod']);
        $nn += 1;
        }

    $writer = new Writer($spreadsheet);
    $writer->save($xlsx_file);
}

// ----------------------------------------------------------------
function xlsx_read_proc ($xlsx_file)
{
    $dict_aa = array ();

    $reader = new Reader();
    $spreadsheet = $reader->load($xlsx_file);

    $sheet = $spreadsheet->getActiveSheet();

    for ($it=1; $it<= 100; $it++)
        {
        $dict_unit = array ();

        $id = $sheet->getCell ('A' . $it) -> getValue ();

        if ($id == "")
            {
            break;
            }

        $dict_unit['name'] = $sheet->getCell ('B' . $it) -> getValue ();
    $dict_unit['population'] = $sheet->getCell ('C' . $it) -> getValue ();
    $dict_unit['date_mod'] = $sheet->getCell ('D' . $it) -> getValue ();

        $dict_aa[$id]= $dict_unit;
        }

    return  $dict_aa;
}

// ----------------------------------------------------------------

書き込みのサンプル

xlsx_create.php
#! /usr/bin/php
<?php
//
//  xlsx_create.php
//
//                  Sep/09/2018
//
// ----------------------------------------------------------------
include "xlsx_manipulate.php";

// ----------------------------------------------------------------
error_reporting(E_ALL);

fputs (STDERR,"*** 開始 ***\n");
date_default_timezone_set('Asia/Tokyo');

$xlsx_file = $argv[1];

$dict_aa = data_prepare_proc ();

xlsx_write_proc ($xlsx_file,$dict_aa);

fputs (STDERR,"*** 終了 ***\n");
// ----------------------------------------------------------------
function data_prepare_proc ()
{
$dict_aa = array ();

$dict_aa = dict_append_proc ($dict_aa,'t2971','奈良',59276,'2002-7-31');
$dict_aa = dict_append_proc ($dict_aa,'t2972','大和高田',47325,'2002-4-12');
$dict_aa = dict_append_proc ($dict_aa,'t2973','大和郡山',28914,'2002-9-01');
$dict_aa = dict_append_proc ($dict_aa,'t2974','天理',46379,'2002-10-29');
$dict_aa = dict_append_proc ($dict_aa,'t2975','橿原',36715,'2002-7-14');
$dict_aa = dict_append_proc ($dict_aa,'t2976','桜井',24387,'2002-6-12');
$dict_aa = dict_append_proc ($dict_aa,'t2977','五條',82149,'2002-4-17');
$dict_aa = dict_append_proc ($dict_aa,'t2978','御所',98561,'2002-3-24');
$dict_aa = dict_append_proc ($dict_aa,'t2979','生駒',79452,'2002-7-8');

return  $dict_aa;

}

// ----------------------------------------------------------------
function dict_append_proc ($dict_aa,$id,$name,$population,$date_mod)
{
    $dict_unit = array ();
    $dict_unit['name'] = $name;
    $dict_unit['population'] = $population;
    $dict_unit['date_mod'] = $date_mod;
    $dict_aa[(string)$id]= $dict_unit;

    return  $dict_aa;
}
?>

読み込みのサンプル

xlsx_read.php
#! /usr/bin/php
<?php
//
//  xlsx_read.php
//
//                  Sep/09/2018
//
// ----------------------------------------------------------------
include "xlsx_manipulate.php";
// ----------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

date_default_timezone_set('Asia/Tokyo');

$file_in = $argv[1];
#
print $file_in . "\n";

$dict_aa = xlsx_read_proc ($file_in);

dict_display_proc ($dict_aa);


fputs (STDERR, "*** 終了 ***\n");

// ----------------------------------------------------------------
function dict_display_proc ($dict_aa)
{
    ksort ($dict_aa);

    foreach ($dict_aa as $key => $value)
        {
        $name = $value["name"];
        $population = $value['population'];
        $date_mod = $value['date_mod'];

        print "$key\t";
        print "$name\t";
        print "$population\t";
        print "$date_mod\n";        
        }
}
?>
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4