in reply to Re: Insert blank rows in a CSV
in thread Insert blank rows in a CSV

Hi
But for example if I want to leave a few rows in blank because i need to put a image there, how can I do that but not from command line but from a program
this is what I am trying to tell you
<---Start of the CSVFILE -----------------------------> <--Here goes a image ---------------------------------> <--Here goes a blank row -----------------------------> <--Here goes the header ------------------------------> 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data
So, Image I have already have it, but I cant tell the excel csv file to add rows
How can i do that?
thanks

Replies are listed 'Best First'.
Re^3: Insert blank rows in a CSV
by runrig (Abbot) on Jan 09, 2008 at 19:48 UTC
    See How to add an image to Excel/CSV. I think you are out of luck doing it in a CSV, but there is an example in that node of getting the job done another way. As for adding rows in a CSV though (in a program not command line), just add the command line switches (-pi) to the #!perl line in the first line of the script, and the script would contain something similar to what is in the -e option in the command line example.
Re^3: Insert blank rows in a CSV
by pc88mxer (Vicar) on Jan 09, 2008 at 20:03 UTC
    Perhaps part of the confusion is that runnig's approach is to make a new copy of the file, adding some lines to the output stream at the appropriate places and then rename the new file to the original file name. So, the original file is not being edited in place, but a wholly new copy of the data is being made into a different file. When the new file is renamed to the orignal file name, it "becomes" the original file and the contents of the original file are automatically deleted (or, if you use the -i command line switch, the old file is renamed to another name so you can go back to it if necessary.)

    One nice property of this approach is that if your script bombs in the middle, you still have the original contents around. So, I hope this explains why you don't have to "reserve" space in your original file for the added content.

    On the other hand, if you want your original file to actually look like:

    <---Start of the CSVFILE -----------------------------> <--Here goes a image ---------------------------------> <--Here goes a blank row -----------------------------> <--Here goes the header ------------------------------> 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data 123456 123 john 9393939 data data
    and you want the script to replace
    <--Here goes a image --------------------------------->
    with something, then that's a different problem, but you can still use the same idea:
    while (<>) { if (m/^<--Here goes a image.../) { print ...whatever... } else { print; } }
    Use the above code with the -i option to use perl's file editing feature. If the rows you want to replace are blank, then just use line numbers as the matching criteria:
    while (<>) { if ($. == 2) { print ...image csv line... } else { print; } }
Re^3: Insert blank rows in a CSV
by starX (Chaplain) on Jan 09, 2008 at 19:11 UTC
    runrig's advice still applies.  print CSV_HANDLE ",,,,,\n"; would print a row with six empty cells to CSV_HANDLE file handle.
      Hi I am kinda new to Perl, Its going to sound dumb but, my CSV_Handle which one is it these is the code:
      # create the sql select statement my $sql = "SELECT * INTO OUTFILE 'c:/proyecto/$filename' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' FROM t3132"; my $sth = $dbh->prepare($sql); $sth->execute (); $sth->finish (); $dbh->disconnect (); my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); #------ Abro el archivo CSV y lo dirijo al primera hoja de trabajo --- +-- my $Book = $Excel->Workbooks->Open("c:\\proyecto\\$filename"); my $Sheet = $Book->Worksheets(1); print $filename ",,,,,\n"; #------ Busco las ultimas celdas usadas en la hoja de trabajo ------ my $LastRow = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row}; my $LastCol = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column}; #------ Imprimo en pantalla los valores ------- print "Ultima Columna:\n"; print "$LastCol\n"; print "Ultimo renglon:\n"; print "$LastRow\n"; #------ Ahora busco las celdas de la hoja de excel ----- #en este caso dejo la primera linea sin contar porque es la de los enc +abezados my ($Start_col, $Num_Cols,$End_col) = ('A', $LastCol, 'A'); ++$End_col while --$Num_Cols; my ($Start_row, $Num_rows) = (4, $LastRow); my $End_row = $Start_row + $Num_rows - 1; my $Range_str = "$Start_col$Start_row:$End_col$End_row"; #------ Imprimo los valores ----------- print $Start_col; print $Start_row; print $End_col; print $End_row; print "Fin de busqueda de los datos"; my $range2 = $Sheet->Range($Range_str); $range2->AutoFormat(7);
      thanks
        Instead of selecting straight into a file, you might want to write the file using Spreadsheet::WriteExcel. That module is capable of inserting images into the spreadsheet. You then wouldn't need to use Win32::OLE.
        Generally, when you want to open a file for writing, you would do something like:
        open FILE_HANDLE, ">filename.csv" or die "Couldn't open filename.csv f +or writing $!";
        and then to print to the file you would specify the file handle as part of the print statement, hence print FILE_HANDLE ",,,,,\n"; In this case, I can see where you're opening the work book with Win32::OLE, but all of your print statements seem to be going to standard output, which won't write anything to the file.

        update: fixed type (thanks blazar)