Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove line, save contents

Status
Not open for further replies.

zincyo

Programmer
Jun 13, 2007
35
US
I wrote a perl file which opens up a CSV file and removes the first row of the file, and the last column of the file.

What I need to somehow do is save the text from the first row of the file (which I then remove), so as to add it to the top of another sheet in the same workbook. Any ideas?

below is the code, which I run by typing "perl thisFIlename.pl file_being_edited.csv > new_output.txt
Essentially, I output this into a text, then have my xls import that text.

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::Warn = 3; # die on errors...

my $csv = Text::CSV->new();
my $columns;
while (<>) {
next if ($. == 1);
$csv->parse($_);
my @columns = ($csv->fields());
my $i = pop(@columns);
print join(' ', @columns), "\n";
}

# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');

# open Excel file
my $Book = $Excel->Workbooks->Open("c:/location/sheet1.xls");

$Excel->Run("macro1");


$Excel->Run("macro2");
 
Code:
my $csv = Text::CSV->new();
my @headings;
my $columns; [red]# what's this? Write-only storage?[/red]
while (<>) {
   $csv->parse($_);
   my @columns = ($csv->fields());
   if ($. == 1) {
      @headings = @columns;
      next;
   }
   my $i = pop(@columns);
   print join('    ', @columns), "\n";
}

# do stuff with saved @headings here

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top