excel转txt工具
? ? 有個任務需要讀好多eccel,實際讀起來并不方便,變手工把ecxcel轉換成文本文檔,心累。。。。于是閑暇寫了個ecxcel轉txt的小工具。主要是用的Spreadsheet::XLSX。
use strict;
use warnings;
use strict;
use Spreadsheet::XLSX;
die "Usage :perl $0 <xlsx> <resultDIR>\n" unless @ARGV==2; ####需要輸入需要轉換的ecxel文件 以及生成結果文件的路徑
my $file=shift @ARGV;
my $dir=shift @ARGV;
my $excel=Spreadsheet::XLSX->new($file);
foreach my $sheet(@{$excel->{Worksheet}}){
? ? ? ? ? my $name=$sheet->{Name};###sheet名字
? ? ? ? ?open OUT,">$dir/$name.txt";##默認結果是一個sheet轉換成一個文本文檔,名字為sheet名字 ,可自行調整
? ? ? ? ?$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];
? ? ? ? ? ? ? ? ? ? ? ? ? ? if($cell){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print OUT"$cell->{Val}\t";
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? print OUT"\n";
? ? ? }
}
寫完之后,發現每次只能轉換一個文件,于是就把它做成了一個包,名字為excel2txt.pm,隨便用~
主程序perl.pl
use strict;
use warnings;
use package::excel2txt;
my $dir="/home/escel2txt";
my @sample=("test");
foreach my $sample(@sample){
? ? ? ? ? my $file="$dir/$sample.xlsx";
? ? ? ? ? my $reportdir="/home/escel2txt";
? ? ? ? ?package::excel2txt::change($file,$reportdir);##在report下批量生成轉換過后的文檔
}
包:excel2txt.pm
package package::excel2txt;
use strict;
use warnings;
use strict;?
use Spreadsheet::XLSX;
sub change{
my $file=shift @_;
my $dir=shift @_;
my $excel=Spreadsheet::XLSX->new($file);
? foreach my $sheet(@{$excel->{Worksheet}}){
? ? ? ? ? my $name=$sheet->{Name};###sheet名字
? ? ? ? ?open OUT,">$dir/$name.txt";##默認結果是一個sheet轉換成一個文本文檔,名字為sheet名字 ,可自行調整
? ? ? ? ?$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];
? ? ? ? ? ? ? ? ? ? ? ? ? ? if($cell){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print OUT"$cell->{Val}\t";
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?print OUT"\n";
? ? ? ? ? }
? ? ?}
}
1
轉載于:https://www.cnblogs.com/gui-/p/6889608.html
總結
以上是生活随笔為你收集整理的excel转txt工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么在linux的cmd中运行c项目,如
- 下一篇: JAVA程序员--壁纸