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

**** Thanks everyone! I ended up setting up a check to figure out the excel version and set a max row limit based on the version (since 03 and 07 have different limits). I then set a counter that keeps track of the rows and alerts the user when the max has been reached, saves the document, and closes out. **** Using Win32 OLE in Perl to write excel rows from data I'm retieving from another data source (ReqPro). I write the data as it is retrieved line by line (so there is no way to know how much data there will be). Anyway, I just ran into an issue where I get a generic error when trying to write a row that something was not defined. After an hour or so I finally realized it wasn't the line of code, but rather the spreadsheet had no more rows to write to because it was at 6x,xxx lines. Any idea how to gracefully catch this? How others handle this? I'm hoping though that there is some WIN32 API check I can run in the code before writing a row to check and see I can write a row (or a catch I can include after trying to write a row if it fails)... A couple thoughts if I can't find a good solution: 1) Count the rows each iteration and when around 60,000 rows are reached throw a dialogue message (tk), stop looping, and then on the last row put something like "Reached the max number of rows the spreadsheet can have, however more data does exist...Narrow you query and run again." 2) If rows equal something like 60,000 then insert a new sheet after the current sheet and continue on (and on, and on, and on, until done...)

Replies are listed 'Best First'.
Re: Error when reach end excel sheet limit
by marto (Cardinal) on Jan 17, 2011 at 15:22 UTC

    What is ReqPro? Why not simply keep count of how many rows you've written so far, and create a new workbooksheet and resume writing to that when you get close to the limit? Are you sure storing so much information within Excel is wise? What are you going to do with it once it's stored there?

    Update: s/book/sheet/

Re: Error when reach end excel sheet limit
by chrestomanci (Priest) on Jan 17, 2011 at 15:49 UTC

    From your description of the problem, it sounds like you are using Excel as an output format to render the results of some sort of database query.

    As Ratazong said, there are almost certainly ways to keep track of how many rows your are writing into your spreadsheet, and if you are exceeding the row limit (2**16 I think), you can issue a warning or start a new sheet.

    However, I would question if it is usefull to the end user to get that many results. If the Excell sheet is just a list of all matching records, then most of the time when the end user gets a huge spreadsheet with thousands of results, they will not bother reading past the first page or so, instead, they will refine their query and run it again.

    Considering that, I would consider having a query limit, so that if a query returns lots of results, the user gets the first 500 or so, and then a message like Your query returned 631,978 records, showing the first 500. You would need to provide a way for users to show all results if they really needed them, but I suspect that most users would not.

Re: Error when reach end excel sheet limit
by locked_user sundialsvc4 (Abbot) on Jan 17, 2011 at 15:35 UTC

    The limit is 65,535 but I would stop well short of that.

    Remember that Excel also provides for “external data ranges” that can come from almost any source and can be of almost any size...   You can prepare information for this use with your Perl script, in a wide variety of formats, and the resulting presentation is very nice.

Re: Error when reach end excel sheet limit
by Ratazong (Monsignor) on Jan 17, 2011 at 15:29 UTC

    For writing to the Excel-sheet you have probably some code like

    $sheet->Range("$column$line")->{'Value'} = $myValue;
    In that case it would be easy to check if your variable $line is > 60.000 ... no need for counting

    HTH, Rata