You can always muck with Excel using Win32::OLE (aka COM
TM) Recently I wrote the following, relatively simple, script to convert an Excel
TM file to TSV. For you I quickly swapped a few things around to convert TSV back to Excel
TM format. It worked for my simple testing. Note that the filename needs to be fully qualified or Excel
TM evilly thinks you mean it's default directory, not your current working directory.
#!perl -w
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 2; # Always warn with verbose error messages
# Open a new excel app, thus not stepping on anyones toes.
my $Excel = Win32::OLE->new('Excel.Application', 'Quit')
or die "Unable to start Excel";
# Set Application Visibility
# 0 = Not Visible 1 = Visible
$Excel->{Visible} = 0;
my $file = 'c:\test.txt'; # I tested with a TSV file.
die "'$file' not found" unless -e $file;
my $new_file = $file;
$new_file =~ s/txt$/xls/i;
if( -e $new_file )
{
my $n = 4;
while( --$n )
{
print STDERR "Deleting '$new_file' in $n seconds \r";
sleep 1;
}
warn "Deleting '$new_file' \n";
unlink $new_file;
}
my $Book = $Excel->Workbooks->Open($file); # Open the book.
# And save it in the prefered format.
$Book->SaveAs( {
FileName => $new_file,
FileFormat => xlNormal, # To save as TSV use xlText
CreateBackup => 0} );
$Book->Close(0);
$Excel->Quit();
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.