Class ‘PHPExcel_Writer_csv’ not found

PHP Excelでexcelからcsvへの変換しようと以下のように書くと

set_include_path(get_include_path() . PATH_SEPARATOR . "vendor/phpoffice/phpexcel/Classes/");
include "PHPExcel.php";
include "PHPExcel/IOFactory.php";

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $objReader->load('csv/edinetlist.xls');

$writer = PHPExcel_IOFactory::createWriter($excel, 'csv');
$writer->save('csv/edinetlist.xls');

なに!?
Class ‘PHPExcel_Writer_csv’ not found in /home/vagrant/equity/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php on line 141

githubを見ると、


The Writer names are case-sensitive
so you’d need to specify the Writer name correctly as ‘CSV’ rather than ‘csv’
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘CSV’);

なるほど、以下で上手くいきます。

$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');

もう一回やってみよう

いろいろ触っていたら、csvは複数のsheetsをサポートしていないとアラートが出たので、手動でsheet2,sheet3を削除してcsvで保存。

ini_set('memory_limit', '512M');

$file = "csv/test.csv";
$file = fopen($file, "r");
echo "<table>";
if($file){
  $i= 0;
  while ($line = fgetcsv($file)) {
  	if($i < 10){
  		echo "<tr><td>".$line[0]."</td>";
  		echo "<td>".$line[1]."</td>";
  		echo "<td>".$line[4]."</td>";
  		echo "<td>".$line[5]."</td>";
  		echo "<td>".$line[9]."</td>";
  		echo "<td>".$line[10]."</td>";
  		echo "<td>".$line[11]."</td>";
  		echo "<td>".$line[12]."</td></tr>";
  	}
    $i++; 
  }
}
echo "</table>";
fclose($file);

あれ? なに。

composerでphpexcelをインストールします。
sudo vi /etc/php.iniで以下に変更。
‘memory_limit’, ‘512M’
‘set_time_limit’, ‘480’

phpexcelでシートを削除して期待するcsvに保存できるかやってみます。

set_include_path(get_include_path() . PATH_SEPARATOR . "vendor/phpoffice/phpexcel/Classes/");
include "PHPExcel.php";
include "PHPExcel/IOFactory.php";

$path = "csv/account.xls";
$excel = PHPExcel_IOFactory::load($path);

$excel->removeSheetByIndex(2);

$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->save('csv/out.csv');

駄目なようなので、別の方法を探します。

Allowed memory size of 134217728 bytes exhausted

5万行くらいあるcsv(上場企業の決算全データ)を処理したいのですが、

走らせたところ
192.168.33.1:50210 [500]: / – Allowed memory size of 134217728 bytes exhausted (tried to allocate 8388608 bytes) in /home/vagrant/equity/index.php on line 4

下記を追加して再度リクエスト

ini_set('memory_limit', '512M');
ini_set('memory_limit', '512M');

$pass = "csv/account.csv";
$data = file($pass, FILE_IGNORE_NEW_LINES);

unset($data[0]);

function cut($item){
    return explode(',', $item);
}
$data = array_map("cut", $data);
foreach ($data as $value){
    $code[]= $value[0];
}

var_dump($code);

ああああああああ、もう駄目だ
嫌になってきた、今日は終了

echo mb_detect_encoding($data); で判定すると、”UTF-8″と表示されるんだが、何故??
UTF-8,UTF-7,ASCII,EUC-JP,eucJP-win,SJIS,SJIS-win,JIS,ISO-2022-JP,Unicode全部あかんやん。
ちくしょー

ログインフォーム

<?php
header("Content-type: text/html; charset=utf-8");

define("ID", "vagrant");
define("PASSWORD", "asdf");

$post_id = @$_POST&#91;"id"&#93;;
$post_password = @$_POST&#91;"password"&#93;;
$val1 = @$_POST&#91;"val1"&#93;;
$val2 = @$_POST&#91;"val2"&#93;;
$val3 = @$_POST&#91;"val3"&#93;;

$mass = $val1 + $val2;

if($post_id == ID && $post_password == PASSWORD && $mass == $val3){
	echo "<p>ログイン成功</p>";
} elseif(!@$_POST){
	login_form();
} else {
	if($mass == $val3){
		echo "<p>ログイン失敗 IDまたはPASSWORDが間違っています。</p>";
	} else {
		echo "<p>ログイン失敗 計算の答えが間違っています。</p>";
	}
	login_form();
}

function login_form(){

	$rand1 = rand(0,9);
	$rand2 = rand(0,9);

	echo <<<LOGINFORM
	<!DOCTYPE html>
	<html lang="ja">
	<body>
	<form action="" method="post" id="login">
		<input type="text" name="id" placeholder="id">
		<input type="password" name="password" placeholder="password">
		<input type="text" name="val1" style="display:none;" value="{$rand1}">
		<input type="text" name="val2" style="display:none;" value="{$rand2}">
		<input type="text" name="val3" placeholder="{$rand1}+{$rand2}=の答えを入力">
		<input type="submit" value="ログイン">
	</form>
	</body>
	</html>
LOGINFORM;
}
?>

PDO create tableで4000個テーブルを作る時間

銘柄リストから、それぞれのテーブルを作っていきます。sqlにはif not existsを入れます。

$time_start = microtime(true);
$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 

$sql = "select code from lists";
$stmt = $dbh->query($sql);
while($result = $stmt->fetch(PDO::FETCH_ASSOC)){
		$code[] = $result["code"];
}


foreach($code as $value){
	$sql = "create table if not exists equity.code".$value."(
		id int unsigned auto_increment primary key,
		date int,
		close int
	)ENGINE = MYISAM;";
	$stmt = $dbh->query($sql);
}

$time = microtime(true) - $time_start;
echo "{$time}秒";

mysql側 入ってます。

経過時間:35秒

問題は次ですが、
アップデートがなかった場合:11.73秒

localで走らせるので、そこまで大きな問題ではなさそうですね。
一からもう一度やってみましたが、大体同じ時間です。

試しにgoogle financeから1個データを入れてみます。

これは。。。かなりやる気なくなってきた。

PDOでcreate table

$time_start = microtime(true);
$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 

$code= '2338';

$sql = "create table equity.code".$code."(
	id int unsigned auto_increment primary key,
	date int,
	close int
)ENGINE = MYISAM;";
$stmt = $dbh->query($sql);

$time = microtime(true) - $time_start;
echo "{$time}秒";

mysql 重複を除いた値の一覧抽出

以下で取得可能です。
SELECT DISTINCT colname FROM tablename;

mysql> select distinct sector from lists;
+————————–+
| sector |
+————————–+
| 水産・農林業 |
| 卸売業 |
| 建設業 |
| 非鉄金属 |
| 鉱業 |
| 機械 |
| サービス業 |
| 金属製品 |
| 情報・通信 |
| 食料品 |
| 医薬品 |
| 不動産業 |
| 陸運業 |
| その他金融業 |
| 小売業 |
| その他製品 |
| 繊維製品 |
| 電気機器 |
| ガラス・土石製品 |
| 証券業 |
| 輸送用機器 |
| REIT銘柄一覧 |
| 石油・石炭製品 |
| 化学 |
| パルプ・紙 |
| 精密機器 |
| ゴム製品 |
| 鉄鋼 |
| 銀行業 |
| 保険業 |
| 倉庫・運輸関連業 |
| 海運業 |
| 空運業 |
| 電気・ガス業 |
+————————–+
34 rows in set (0.05 sec)

早く気づけばよかった。