Skip to content

Instantly share code, notes, and snippets.

@xie186
Last active November 20, 2021 17:58
Show Gist options
  • Save xie186/8937c182105a7db6a0d19f162992950b to your computer and use it in GitHub Desktop.
Save xie186/8937c182105a7db6a0d19f162992950b to your computer and use it in GitHub Desktop.
Generate excel files based on tab-delimited file
#!/usr/bin/perl -w
use strict;
##################################################
#This script is to convert tab-delimited file to #
#Excel sheets. #
##################################################
use Excel::Writer::XLSX;
my ($tab, $out) = @ARGV;
die usage() unless @ARGV == 2;
sub usage{
my $die =<<DIE;
perl *.pl <Input file: tab-delemited file> <output file in XLSX file>
DIE
}
my $workbook = Excel::Writer::XLSX->new($out);
my $worksheet = $workbook->add_worksheet();
my $row = 0;
open TAB, $tab or die "$!";
while(<TAB>){
chomp;
my @ele = split(/\t/);
&write_xlsx($worksheet, $row, @ele);
++$row;
}
sub write_xlsx{
my ($worksheet, $tem_row, @ele) = @_;
for(my $i = 0; $i < @ele; ++$i){
$worksheet->write( $tem_row, $i, $ele[$i]);
}
}
#!/usr/bin/perl -w
use strict;
############################################################
#This script is to convert multiple tab-delimited files to #
#Excel sheets. xie186@purdue.edu #
############################################################
use Excel::Writer::XLSX;
my ($tab, $sheetname, $out) = @ARGV;
die usage() unless @ARGV == 3;
sub usage{
my $die =<<DIE;
perl *.pl <Input file: tab-delemited file [file1,file2...]> <SheetName[Sheet1,Sheet2...]> <output file in XLSX file>
DIE
}
my $workbook = Excel::Writer::XLSX->new($out);
my @tab = split(/,/, $tab);
my @sheetname = split(/,/, $sheetname);
#my $worksheet = $workbook->add_worksheet();
for(my $i = 0; $i < @tab; ++$i){
my $row = 0;
my $worksheet = $workbook->add_worksheet($sheetname[$i]);
open TAB, $tab[$i] or die "$!";
while(<TAB>){
chomp;
my @ele = split(/\t/);
&write_xlsx($worksheet, $row, @ele);
++$row;
}
}
sub write_xlsx{
my ($worksheet, $tem_row, @ele) = @_;
for(my $i = 0; $i < @ele; ++$i){
$worksheet->write( $tem_row, $i, $ele[$i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment