Reading Excel Files with Perl

Sometimes you need to read data from Excel files with Perl. There are several Perl Modules (Spreadsheet::ParseExcel, Spreadsheet::ParseExcel::Simple) that make that job quite easy. Spreadsheet::ParseExcel::Simple is an lightweight abstraction of Spreadsheet::ParseExcel.

Sample Code:

#!/usr/bin/perl -w

use Spreadsheet::ParseExcel;

my $parser = new Spreadsheet::ParseExcel();
my $xls = $parser->Parse("PerlExcelTest.xls")
  or die "Can't read File!";

# Loop over all tables in Excel file
for(my $i = 0; $i < $xls->{SheetCount}; $i++) {
  $sheet = $xls->{Worksheet}[$i];

  print "\nSheet: " . $sheet->{Name} . "\n";

  # has the current sheet cells?
  next unless (exists ($sheet->{MaxRow}) and (exists ($sheet->{MaxCol})));

  # loop over the row of the current sheet
  foreach my $row ($sheet->{MinRow} .. $sheet->{MaxRow}) {
    
    # loop over the cells in the current row
    foreach my $column ($sheet->{MinCol} .. $sheet->{MaxCol}) {
      next unless (defined $sheet->{Cells}[$row][$column]);

      print $sheet->{Cells}[$row][$column]->Value . ' | ';
    }
    print "\n";
  }
}

During my tests I came across several issues:

  • If you create Excel files with OpenOffice numeric values show up as string GENERAL. There is a patch for that problem. Please note also that you could use $cell->Val instead of $cell->Value.
  • I also have problems to extract date values from the excel file. In that case I am not able to determine the type of a date cell. Normally I should see Date but I turns out to be Numeric.

Creating Images with Mono / C#

I just wrote some sample code on how to create GIF images in C#:

using System;
using System.Drawing;
using System.Drawing.Imaging;


public class Tester {
  static void Main() {
    Bitmap bmp = new Bitmap(79,121);
    Graphics graph = Graphics.FromImage(bmp);
    Font font = new Font("Times", 6, FontStyle.Italic);

    for(int i=0; i<10000; i++) {
      graph.Clear(Color.White);
      graph.DrawString(i + " - Test", font, SystemBrushes.WindowText, 1, 1); 
      bmp.Save(i + "foo.gif", ImageFormat.Gif);
    }
  }
}

To compile that code you have to make an reference:

$ mcs -r:System.Drawing pic01.cs

To create 10.000 images takes about 12 seconds on my notebook. If you prefer PNG it takes a little bit longer ~ 26s.

Creating Windows EXE-Files under Linux

Sometimes a cross-compile environment can be very handy. Sam Lantinga form the libsdl.org project made a shell script (build-cross.sh) that makes it very easy to setup an GCC cross-compile environment for MinGW.

I used the follwing changes for the script:

  • GCC_VERSION=3.4.2-20040916-1
  • BINUTILS=binutils-2.15.91-20040904-1
  • MINGW=mingw-runtime-3.3
  • W32API=w32api-2.5

The EXE-files are a little bit large in size, but when you strip them the size is reduced drastically.

OC4J JSP Profiling with JProbe

I have done some Profiling with JProbe - hey I didn't know that you can download a Freeware Edition from Quest.

To get it working with Oracle OC4J 10g (Version 10.1.3) I used the following configuration:

  • Application Server Type: Other Server
  • Server Directory: toplevel directory where you installed OC4J (<install -dir>)
  • Server Class: oracle.oc4j.loader.boot.BootStrap
  • Class Path: <install -dir>/j2ee/home/oc4j.jar
  • Java Executable: /usr/java/j2sdk1.4.2_09/bin/java
  • Java Options: -DORACLE_HOME=<install -dir>
  • Working Directory: <install -dir>
  • Server Class Arguments: -config <install -dir>/j2ee/home/config/server.xml

I hope that helps to get you started with profiling. It is always a cool thing to see where the performance bottlenecks are buried inside of some piece of software. Sometimes some "improvements" make it worse when seeking to make it better :-D.

Having Fun with Bugs

Ups :-)
It is a little bit embarrassing, but software bugs can also happen to me. Bugs in web applications can lead to something like that showing up in search engine results:

error message in search engine

That error message was caused by an flaw on how I handled the
input from the HTTP Accept-Language line, if it was missing my code run into the above error.

So, that example proves one thing - it is very hard to review and audit your own code for flaws and security holes.