博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
perl读取excel
阅读量:4964 次
发布时间:2019-06-12

本文共 3124 字,大约阅读时间需要 10 分钟。

因为工作当中遇到要处理大数据的excel的玩意,最多的有几十万行。用perl的方式试试,看看效果如何。

ppm install OLE::Storage_Lite #如果不安装这个,后面两个安装不了 ppm install Spreadsheet::ParseExcelppm install Spreadsheet::WriteExcel

查看是否安装成功

perldoc Spreadsheet::ParseExcel #如果打印出文档则表示安装成功

为保证编码正确

ppm install Unicode::Map
use strict; use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::FmtUnicode; #字符编码 my $parser = Spreadsheet::ParseExcel->new(); my $formatter = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map=>"CP936");#设置字符编码#my $workbook = $parser->parse('Book.xls'); my $workbook = $parser->parse('E:\webdev\project\perl\a.xls', $formatter);#按所设置的字符编码解析my $log = "demo.log"; if ( !defined $workbook ) {     die $parser->error(), ".\n"; }open(FILE,">$log");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 ) {         for my $col ( $col_min .. $col_max ) {              my $cell = $worksheet->get_cell( $row, $col );             next unless $cell;             print $cell->value()," ";            print(FILE $cell->value()."\t");        }         print "\n";        print(FILE "\n");    } }close(FILE);
View Code

记录下,明天去公司试试效果

use strict;  use warnings;  use Spreadsheet::XLSX;  use Encode;  use Getopt::std;use File::Basename;use LWP::Simple;  use URI::Escape;#my $converter = Text::Iconv -> new ("utf-8", "windows-1251");  #my $excel = Spreadsheet::XLSX -> new ('test.xlsx', $converter);  eval {    my $l = "D:\\log.log";        #读取excel文件    my $excel_path = $ARGV[0];    my $dir = dirname($excel_path);        #设置log文件路径    my $log = $dir."/".$ARGV[1].".log";    open(FILE,">$l");    print(FILE "http://localhost/kwm/source/index.php?s=Admin/Index/extract/log/".$ARGV[1]);    close(FILE);    #print 'http://localhost/kwm/source/index.php?s=Admin/Index/extract/log/'.$ARGV[1];            #print $log;die;    #print(FILE "praam $excel_path\n");    my $excel = Spreadsheet::XLSX->new($excel_path);    #print(FILE "excel read over\n");            my $sheet = $excel->Worksheet(0);    open(FILE,">$log");    printf("Sheet: %s/n", $sheet->{Name});     $sheet -> {MaxRow} ||= $sheet -> {MinRow};                    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {          $sheet -> {MaxCol} ||= $sheet -> {MinCol};          foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {              my $cell = $sheet -> {Cells} [$row] [$col];              my $a = "";            if ($cell) {                  $a = $cell->{Val};                  #$a = encode("gbk", decode("utf8", $a));                  #printf("( %s , %s ) => %s/n", $row, $col, $a);                  #printf("$a ");              }                         print(FILE $a."\t");        }          #print "/n"; ## one row over          print(FILE "\n");    }      close(FILE);    my $content = get('http://localhost/kwm/source/index.php?s=Admin/Task/extract/log/'.$ARGV[1]."/id/".$ARGV[2]."/uid/".$ARGV[3]);     print encode("gbk", $content);    die;};if($@) {    #print "error";}
View Code

 

转载于:https://www.cnblogs.com/yimiao/p/3807032.html

你可能感兴趣的文章
自定义ClassLoader
查看>>
用python发邮件实例
查看>>
Python基础-包
查看>>
oss文件删除策略
查看>>
HDU 1058 Humble Numbers(dp)
查看>>
LeetCode 251. Flatten 2D Vector
查看>>
LeetCode Shortest Unsorted Continuous Subarray
查看>>
(转载)Java多线程入门理解
查看>>
用JS打开新窗口,防止被浏览器阻止的方法
查看>>
自我介绍
查看>>
数据库实例练习
查看>>
mybatis中sql片段
查看>>
Ubuntu下安装Android Studio全过程(2015.01.27)
查看>>
ABAP术语-Company Code
查看>>
学习笔记:弧度和角度转换的定义以及转换方法
查看>>
shell中的数值运算
查看>>
23种设计模式之六(工厂模式)
查看>>
HashSet、TreeSet和LinkedHashSet分别基于HashMap、TreeMap和LinkedHashMap
查看>>
maven 添加json-lib包 or自定义jar包
查看>>
linux之ssh服务
查看>>