php에서 엑셀파일 읽어들이기
한 2년전에(-0-) 엑셀파일(csv가 아닌-0-ㅋ) 을 업로드 해서 파싱해야하는 일이 있어서
이것저것 찾다가 perl로 가능하다고 해서 여기저기 주워들은 것으로 만든것입니다.ㅋ
일단 관련 모듈을 설치해야하는데..(perl은 당근 있겠죠?ㅋㅋ)
#!/usr/bin/perl
use CPAN;
CPAN::Shell->install("Spreadsheet::ParseExcel");
CPAN::Shell->install("Unicode::Map");
[/code]
위 코드를 파일에 저장한 후 실행권한주고 실행 시키면 이것저것 물어보는데(처음설치하는거라면..)
모르시면 그냥 엔터만 누르세요ㅋㅋ
그 후에 엑셀파일을 진짜 읽어들이는 코드를 만드러야겠죠ㅋ
[code]
#!/usr/bin/perl
use strict;
my %opts;
my %opts;
#
# Command line options processing
#
sub init()
{
use Getopt::Std;
my $opt_string = 'gSs:L:hfC:c:m:';
getopts( "$opt_string", \%opts ) or usage();
usage() if $opts{h};
if(!$ARGV[0]){
usage();
die();
}
}
# Command line options processing
#
sub init()
{
use Getopt::Std;
my $opt_string = 'gSs:L:hfC:c:m:';
getopts( "$opt_string", \%opts ) or usage();
usage() if $opts{h};
if(!$ARGV[0]){
usage();
die();
}
}
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
my $oExcel = new Spreadsheet::ParseExcel;
my $unicode_map = "EUC-KR";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map => $unicode_map);
my $oBook = $oExcel->Parse($ARGV[0], $oFmtJ);
use Spreadsheet::ParseExcel::FmtUnicode;
my $oExcel = new Spreadsheet::ParseExcel;
my $unicode_map = "EUC-KR";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map => $unicode_map);
my $oBook = $oExcel->Parse($ARGV[0], $oFmtJ);
my($iR, $iC, $oWkS, $oWkC);
#print "=========================================\n";
#print "FILE :", $oBook->{File} , "<br>";
#print $oBook->{SheetCount} , "\a\a\b\b\a\a";
#print "AUTHOR:", $oBook->{Author} , "\n";
#print "=========================================\n";
#print "FILE :", $oBook->{File} , "<br>";
#print $oBook->{SheetCount} , "\a\a\b\b\a\a";
#print "AUTHOR:", $oBook->{Author} , "\n";
my $table = [];
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) {
$oWkS = $oBook->{Worksheet}[$iSheet];
for(my $iR = $oWkS->{MinRow} ;
defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
# print "ROW HEIGHT:", $oWkS->{RowHeight}[$iR], "\n";
for(my $iC = $oWkS->{MinCol} ;
defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) {
$oWkC = $oWkS->{Cells}[$iR][$iC];
print $oWkC->Value, "[#행구분#]" if ($oWkC);
print " ", "[#행구분#]" if (!$oWkC);
}
print "[#열구분#]";
}
}
sub usage()
{
print STDERR << "EOF";
$oWkS = $oBook->{Worksheet}[$iSheet];
for(my $iR = $oWkS->{MinRow} ;
defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
# print "ROW HEIGHT:", $oWkS->{RowHeight}[$iR], "\n";
for(my $iC = $oWkS->{MinCol} ;
defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) {
$oWkC = $oWkS->{Cells}[$iR][$iC];
print $oWkC->Value, "[#행구분#]" if ($oWkC);
print " ", "[#행구분#]" if (!$oWkC);
}
print "[#열구분#]";
}
}
sub usage()
{
print STDERR << "EOF";
unexcel is an Excel to HTML converter.
Usage: $0 excel_file_name [-hgS] [-c codepage] [-m maximum_columns] [-s sheet number] [-L header level] [-C gridline color]
-h Prints out (this) help screen.
-m [maximum_columns]
Maximum number of columns. In some cases, the
Spreadsheet::ParseExcel library has difficulty
determining the right number of columns. This
limits the processing, supressing the output
of extra columns.
-c [codepage]
Sets the codepage, e.g. CP932, CP1251, ...
Just like Unicode::Map. The default is CP1250,
which is for Central Europe.
-s [number]
Process only sheet specified by [number]
-S Suppress sheet headers.
-L [header level]
Set header level (1 for H1, 2 for H2) for sheet headers.
-g Suppress gridlines.
-C [gridline color]
Sets the gridline color. Default is D0D0D0.
-m [maximum_columns]
Maximum number of columns. In some cases, the
Spreadsheet::ParseExcel library has difficulty
determining the right number of columns. This
limits the processing, supressing the output
of extra columns.
-c [codepage]
Sets the codepage, e.g. CP932, CP1251, ...
Just like Unicode::Map. The default is CP1250,
which is for Central Europe.
-s [number]
Process only sheet specified by [number]
-S Suppress sheet headers.
-L [header level]
Set header level (1 for H1, 2 for H2) for sheet headers.
-g Suppress gridlines.
-C [gridline color]
Sets the gridline color. Default is D0D0D0.
This script depends on Spreadsheet::ParseExcel and Unicode::Map
EOF
exit;
}
[/code]EOF
exit;
}
위와 같은 코드를 사용하시면 됩니다.(구분자는 [#열구분#], [#행구분#] 이 두개를 고쳐서 쓰시면 됩니다.)
저 같은 경우는 excelReader.pl 파일에 저장한 후 실행 권한을 주고
웹상에서 php로
[code]
$content = exec("/usr/bin/perl ./excelReader.pl " . $this->FILEPATH);
[/code]
이런 식으로 씁니다.ㅋ
$contents는 따로 파싱해야겠죠?ㅋ
프로그래밍/PHP
2008/03/17 00:06

댓글을 달아 주세요