0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

xampp上のHPでexcelファイル(xlmx)を読み込む方法(Linux)

Posted at

0.環境

 Chromebook上のLinuxでやっています。
 ChromebookにXAMPP環境を構築するには、Qiita上でググってみてください。

1.Composerのインストール

 まずは「Composer」コマンドが使えるようにします。

sudo apt-get install php

からの

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer.phar
sudo ln -s /usr/local/bin/composer.phar /usr/bin/composer.phar
alias composer="php /usr/bin/composer.phar"
alias sudo='sudo '

一番下のコマンドに関しては、

sudoコマンドでaliasを使えるようにする
https://qiita.com/homoluctus/items/ba1a6d03df85e65fc85a

を参照のこと

composer -V

でバージョンが見れればOK

2.色々ダウンロード

sudo apt-get install php-xml
sudo apt-get install php-gd
sudo apt-get install php-mbstring
sudo apt-get install php-zip

3.ここで参考にしたいのは

aalfiann/install_composer_xampp_ubuntu.md
https://gist.github.com/aalfiann/5ec4fe415ae4c6b8ddd79bffdcd33070
 目を通して、必要に応じて実行してください。

4.spreadsheetのインストール

 ご自身のwebにcomposerとかいう名前でmkdirしておき、その中にcdします。
そこで、

sudo composer require phpoffice/phpspreadsheet

を実行すると、
composer.jsonやcomposer.lockやvendorが生成されているかと思います。

5.テスト

こんな感じのHTMLと、xlmxを配置し、中身が見えたらOK!

index.php
<!DOCTYPE html>
<html>
<head>
	<title>Excelファイルの内容を表示</title>
	<style>
		table, th, td {
		  border: 1px solid black;
		  border-collapse: collapse;
		}
		th, td {
		  padding: 5px;
		  text-align: left;
		}
	</style>
</head>
<body>

<?php
require 'composer/vendor/autoload.php'; // ComposerでインストールしたPhpSpreadsheetを読み込む

use PhpOffice\PhpSpreadsheet\IOFactory;

// Excelファイルを読み込み、データを配列に格納
$spreadsheet = IOFactory::load('example.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$data = $worksheet->toArray();

// HTMLテーブルを作成して、Excelファイルの内容を表示
echo '<table>';
foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $cell) {
        echo '<td>' . $cell . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

</body>
</html>

6.参考資料

phpSpreadSheet の環境構築とよく参考にするサイト
https://qiita.com/hamburg-love/items/e008ba55770a0820f380

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?