imperl has asked for the wisdom of the Perl Monks concerning the following question:

I tried the Use Spreadsheet::WriteExcel but I get an error saying "Cant locate Spreadsheet/WriteExcel.pm", any idea what this is about? ---------------- Im trying to filter certain words in a text file by pushing all the lines that contain the word into an excel sheet. Now what I would like is for the word (that needed to be searched) to be highlighted in the excel sheet. Say for example, in a document I want to filter out the word "Complex" and push all the lines containing this word to an excel sheet. I want the word "Complex" to be highlighted in the Excel sheet. Any ideas or suggestions would be greatly appreciated. My code is given below.
print "Enter the filename\n"; $filename=<STDIN>; print "Enter the identifier:\n"; $ident=<STDIN>; chomp($ident); open FIN, $filename or warn("$filename $!"); open FOUT, ">result.xls" or warn ("result.xls $i"); while($line= <FIN>) { if($line=~/$ident/) { @flds = split(/\s+/, $line); $number= @flds; for($j=0;$j<=$number;$j++) { print FOUT $flds[$j], "\n"; } print FOUT "*************NEXT ENTRY*************** +**", "\n"; } } print $ident; print "LOG ANALYSIS FOUND IN FILE RESULT.XLS! \n"; close FIN; close FOUT;

Replies are listed 'Best First'.
Re: Color highlighting in Excel
by chester (Hermit) on Sep 16, 2005 at 14:12 UTC
    I have this lying around because I did something similar recently (but this is entirely untested):

    use warnings; use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("filename.xls") or die $! +; my $worksheet = $workbook->add_worksheet(); my $purple = $workbook->set_custom_color(40, 175,200,200); my $format = $workbook->add_format(bold => 1,bg_color => $purple); my $row = 0; foreach my $data (@something) { $worksheet->write_row($row,0,[$data],$format); $row++; }
      Or, using Excel::Template
      <workbook> <worksheet> <loop name="data"> <row> <if name="highlight_this"> <format bgcolor="purple"> <cell><var name="something" /></cell> </format> </if> <if name="highlight_this" is="false"> <cell><var name="something" /></cell> </if> </row> </loop> </worksheet> </workbook>

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Color highlighting in Excel
by Util (Priest) on Sep 16, 2005 at 14:53 UTC

    Suggestions on your original code:

    • $ident needs a newline after you print it.
    • /$ident/ needs to be /$ident/o to prevent the (slow) recompiling of the regex on each loop.
    • You should chomp the filename, just like you do for $ident.
    • If the open fails on either of your files, then the rest of your program cannot run sensibly, so change warn into die.
    • use strict and use warnings.
    • In your FOUT warning, you used $i instead of $!. 'use strict' would catch that!

    The example code below creates a spreadsheet with two rows, "Harry James Potter" and "Amelia Susan Bones", highlighting "James" and "Amelia".

    Working, tested code:

    use strict; use warnings; use Spreadsheet::WriteExcel; my $out_file = 'result.xls'; my $search_string = 'me'; my $search_pattern = quotemeta($search_string); my $workbook = Spreadsheet::WriteExcel->new($out_file) or die "Can't create new workbook '$out_file': $!"; my $worksheet = $workbook->add_worksheet() or die; my $highlight = $workbook->add_format(); $highlight->set_bg_color('yellow'); my $row = 0; while (<DATA>) { chomp; next unless /$search_pattern/o; my $col = 0; my @fields = split; foreach my $field (@fields) { if ( $field =~ /$search_pattern/o ) { $worksheet->write_string($row, $col, $field, $highlight); } else { $worksheet->write_string($row, $col, $field); } $col++; } $row++; } $workbook->close() or warn "Error closing file '$out_file': $!"; __END__ Harry James Potter Albus Percival Wulfric Brian Dumbledore Cornelius Oswald Fudge Amelia Susan Bones Dolores Jane Umbridge Percy Ignatius Weasley

      Thank you all so much. Well, Im not sure how it works but when I say result.xls, there is an excel worksheet created with the output in it. However, I must say that I started it out with a .txt extension and just experimented with a .xls extension. Im quite a novice in Perl, simply because I havent really been dedicating my time for it, but let me try the suggestions and see if it helps. Thanks again
Re: Color highlighting in Excel
by dorward (Curate) on Sep 16, 2005 at 12:40 UTC

    I'm not an expert on the subject, so I might be wrong, but it looks that you aren't outputting to an Excel spreadsheet. It looks like you are outputting to a plain text file with a .xls file extension and new lines delimiting each entry then hoping that whatever application the user has registered to deal with Excel files can interpret that data and convert it to a real Excel format.

    If so, then the solution is almost cetainly a case of using one of the Excel data handling modules on CPAN.

Re: Color highlighting in Excel
by Skeeve (Parson) on Sep 16, 2005 at 12:42 UTC
    First of all you're not creating an excel file but a plain text file containing one word in each line.
    Second: "Highlighting" in Excel is not a perl task. Simply apply a conditional formatting to your cells and you should be done.
    Or did I misunderstand something?

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Color highlighting in Excel
by jmcnamara (Monsignor) on Sep 16, 2005 at 15:43 UTC

    Here is a short example that matches an identifier and highlights it within the line written to an Excel file.

    It used Spreadsheet::WriteExcelXML because this type of in-string highlighting isn't (currently) possible with Spreadsheet::WriteExcel. This means, however, that the end user must have Excel 2002/2003.

    #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcelXML; my $workbook = Spreadsheet::WriteExcelXML->new("file.xls"); my $worksheet = $workbook->add_worksheet(); my $ident = 'days'; my $row = 0; while (<DATA>) { if (s[\b($ident)\b][<B>$1</B>]og) { chomp; $worksheet->write_html_string($row++, 0, $_); } } __DATA__ These are the days and these are the days. What are days for? This is the time. And this is the record of the time. I thank you for the days.

    --
    John.

      The file gets terminated at the second line, since its unable to find Spreadsheet::WriteExcelXML or evern Spreadsheet::WriteExcel. Do I need to add WriteExcel into the repository.

        That error means that the module hasn't been installed.

        There are installation instructions for Spreadsheet::WriteExcel here. The first two methods also apply to Spreadsheet::WriteExcelXML.

        With ActivePerl you can also install the modules via ppm from the ActiveState repositories.

        See also Writing, Installing, and Using Perl Modules.

        --
        John.