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

Hi, i am new to perl. Recently, i have encounter a problem and i need some advise from u guys. Hope u can help me. Anyone know how to export a .csv file into excel using perl ???I cant seem to call out a .csv file into excel using perl. Thks in advance.

Replies are listed 'Best First'.
Re: Exporting .csv file to EXCEL
by jmcnamara (Monsignor) on Jan 23, 2002 at 13:38 UTC

    Excel can read and convert CSV automatically. In fact double-clicking on a .csv file in Windows will generally open it in Excel.

    If you wish to convert the CSV file to an Excel format you can use the Spreadsheet::WriteExcel module. The distro comes with a program called csv2xls.pl which will do the conversion for you.

    Alternatively you could use the Win32::OLE module and office automation. This requires a Windows platform and an installed copy of Excel. This is the most powerful and complete method for interfacing with Excel. See here and here for further information.

    --
    John.

Re: Exporting .csv file to EXCEL
by cacharbe (Curate) on Jan 23, 2002 at 18:04 UTC
    You could eliminate a step if you are exporting to csv, and then opening in Excel by just writing the data out to excel in the first place, especially if you choose to use the Win32::OLE module, which is, as was stated before, the most powerful choice when dealing with Excel on the Win32 platform.

    Either way, here is an example (taken from a tutorial in progress on my scratchpad) of opening a file.

    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # Die on Errors. ######################################## ## ::Warn = 2; throws the errors, but ## ## expects that the programmer deals ## ######################################## my $excelfile = 'somefile.csv'; ################################################################### ## First, we need an excel object to work with, so if there isn't## ## an open one, we create a new one, and we define how the object## ## is going to exit ## ################################################################### my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($excelfile);
    Now you could just add a blank Workbook by replaceing the ->Open($excelfile) line with
    my $Book = $Excel->Workbooks->Add();
    and cycle through your data and enter it directly into excel (an exercise I use leave for the reader, though there is a good example of it in the tutorial mentioned above).

    C-.

      Hi It works. Really appreciate it. Thks for your help!!
Re: Exporting .csv file to EXCEL
by davis (Vicar) on Jan 23, 2002 at 13:27 UTC
    Hi.
    I'm not sure I understand what you want to do. If you just want to open a CSV file, you can open it using Excel - it's one of Excel's recognised file types.
    If you'd like to create a .xls format spreadsheet, you can save the csv file as a .xls file, also from within Excel.

    Assuming that you want to do something more than this, I'd use Text::CSV and Spreadsheet::WriteExcel to convert csv file into .xls files. However, I'd only say this would be worth it if you *specifically* want .xls format files, and you have a lot of files to process - otherwise, I'd just use Excel.<br Hope this helps
    davis
      Before using the Text::CSV module, take a look at some of the warnings given in the Text::CSV node in Perl Monks. This module has its weaknesses!
      HI,thks for all u reply. What i meant was to open the .csv file in excel directly from perl without actually going into excel and open the file manually i.e.( after running the perl program, the excel program will be loaded with the .csv file which is saved in the perl program.) The .csv was saved inside perl because i have used perl to extract some data and i want to load and display this data in EXCEL automatically using perl. Thks once again.
Re: Exporting .csv file to EXCEL
by strat (Canon) on Jan 23, 2002 at 16:57 UTC
    Maybe the following works for you if csv is assosiated with excel...
    system ("start c:\\mydir\\myfile.csv");

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"

Re: Exporting .csv file to EXCEL
by zuqif (Hermit) on Jan 23, 2002 at 18:07 UTC
    Hope I gotcha right here ..
    As has been said before, SuperSearch is your friend!
    Try this by cacharbe
    Tot ziens!
    the 'qif;

    Update:Beaten to it by the man hisself, must be slipping (-: