2009년 7월 15일 수요일

Excel을 xml로 저장하기.

php의 엑셀 파서는 너무 후지다. 무슨 이유에선지 원본 파일의 내용과 파싱한 내용이 다르다.
다른 이름으로 저장을 해봤더니 정상적으로 읽어진다. 하지만 고객들에게 매번 저장을 새로 하라고 할 수는 없는 노릇이다.

perl로 테스트를 해 봤더니 원본도 이상없이 파싱이 된다. 흠 php엑셀 파서를 perl로 변경해야 할까?

#!/usr/bin/perl -w

    use strict;
    use Spreadsheet::ParseExcel;
    use Spreadsheet::ParseExcel::FmtUnicode;
    my $oExcel = new Spreadsheet::ParseExcel;
    my $oFmt = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map => "euc-kr");
    use XML::Excel;
    my $parser   = Spreadsheet::ParseExcel->new();
    my $excel_obj = XML::Excel->new({ParseExcel => $parser});

    my @arr_data;
    my $workbook = $parser->Parse('20090713101853_10002.XLS',$oFmt);

    for my $worksheet ( $workbook->worksheets() ) {

        my ( $row_min, $row_max ) = $worksheet->row_range();
        my ( $col_min, $col_max ) = $worksheet->col_range();

        for my $row ( $row_min .. $row_max ) {
            my @_row;
            for my $col ( $col_min .. $col_max ) {
                my $cell = $worksheet->get_cell( $row, $col );
                next unless $cell;
                push(@_row, $cell->value());

                #print "Row, Col    = ($row, $col)\n";
                #print "Value       = ", $cell->value(),       "\n";
                #print "Unformatted = ", $cell->unformatted(), "\n";
                print "#";
            }

            $arr_data[$row] = \@_row;
        }
    }
    $excel_obj->{column_data} = \@arr_data;
    $excel_obj->print_xml('aa.xml');

댓글 없음:

댓글 쓰기