+ Reply to Thread
Results 1 to 6 of 6

saving multiple sheets into one csv file

Hybrid View

  1. #1
    archimedes
    Guest

    saving multiple sheets into one csv file

    I have multiple sheets each with over 60000 lines. I want to save it into one
    csv file. Is there any quick code around it.

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    I think you can only have 1 sheet in a CSV file

  3. #3
    archimedes
    Guest

    Re: saving multiple sheets into one csv file

    thanks, but i ama ware of that. Im currently saving each sheet as a csv file
    and then opening them as text file and appending the file information. Im
    interested if there is a quicker way around it


    "Mallycat" wrote:

    >
    > I think you can only have 1 sheet in a CSV file
    >
    >
    > --
    > Mallycat
    > ------------------------------------------------------------------------
    > Mallycat's Profile: http://www.excelforum.com/member.php...o&userid=35514
    > View this thread: http://www.excelforum.com/showthread...hreadid=553139
    >
    >


  4. #4
    Registered User
    Join Date
    05-10-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    2

    Post Re: saving multiple sheets into one csv file

    use below code. it will do the job.

    usage
    csvtoxls.pl list list.xls

    Where list is a text list of files needed to added to a xls.



    #!/tool/aticad/1.0/bin/perl
    #use strict;
    use warnings;
    
    use Spreadsheet::WriteExcel;
    use Text::CSV::Simple;
    
    
    
    my $infile_list = shift;
    usage()  unless defined $infile_list && -f $infile_list;
    
    open LIST ,"<", $infile_list or die  ;
    
    my @file_list = <LIST>;
    
    my $infile = "" ;
    
    my $outfile = shift  or die ;
    #my $subject = shift || 'worksheet';
    
    sub usage {
        print "csv2xls infile-list outfile] \n";
        exit;
    }
        
    my $workbook = Spreadsheet::WriteExcel->new($outfile);
    my $bold = $workbook->add_format();
    $bold->set_bold(1);
    
    
    foreach $infile (@file_list) {
    
    my $parser = Text::CSV::Simple->new;
    chomp ($infile) ;
    
    
    my @data = $parser->read_file($infile);
    
    
    my $subject = $infile ;
    my $headers = shift @data;
    
    
    import_data($workbook, $subject, $headers, \@data);
    
    
    }
    
    # Add a worksheet
    sub import_data {
        my $workbook  = shift;
        my $base_name = shift;
        my $colums    = shift;
        my $data      = shift;
        my $limit     = shift || 50_000;
        my $start_row = shift || 1;
        my $worksheet = $workbook->add_worksheet($base_name);
        $worksheet->add_write_handler(qr[\w], \&store_string_widths);
        my $w = 1;
        $worksheet->write('A' . $start_row, $colums, ,$bold);
        my $i = $start_row;
        my $qty = 0;
        for my $row (@$data) {
            $qty++;
            if ($i > $limit) {
                 $i = $start_row;
                 $w++;
                 $worksheet = $workbook->add_worksheet("$base_name - $w");
                 $worksheet->write('A1', $colums,$bold);
            }
            $worksheet->write($i++, 0, $row);
        }
        autofit_columns($worksheet);
        warn "Convereted $qty rows.";
        return $worksheet;
    }
    
    
    ######################################################################
    ######################################################################
    #
    # Functions used for Autofit.
    #
    
    ######################################################################
    #########
    #
    # Adjust the column widths to fit the longest string in the column.
    #
    sub autofit_columns {
    
        my $worksheet = shift;
        my $col       = 0;
    
        for my $width (@{$worksheet->{__col_widths}}) {
    
            $worksheet->set_column($col, $col, $width) if $width;
            $col++;
        }
    }
    
    
    ######################################################################
    #########
    #
    # The following function is a callback that was added via add_write_ha
    #ndler()
    # above. It modifies the write() function so that it stores the maximu
    #m
    # unwrapped width of a string in a column.
    #
    sub store_string_widths {
    
        my $worksheet = shift;
        my $col       = $_[1];
        my $token     = $_[2];
    
        # Ignore some tokens that we aren't interested in.
        return if not defined $token;       # Ignore undefs.
        return if $token eq '';             # Ignore blank cells.
        return if ref $token eq 'ARRAY';    # Ignore array refs.
        return if $token =~ /^=/;           # Ignore formula
    
        # Ignore numbers
        #return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+
    #+))?$/;
    
        # Ignore various internal and external hyperlinks. In a real scena
    #+rio
        # you may wish to track the length of the optional strings used wi
    #+th
        # urls.
        return if $token =~ m{^[fh]tt?ps?://};
        return if $token =~ m{^mailto:};
        return if $token =~ m{^(?:in|ex)ternal:};
    
    
        # We store the string width as data in the Worksheet object. We us
    #+e
        # a double underscore key name to avoid conflicts with future name
    #+s.
        #
        my $old_width    = $worksheet->{__col_widths}->[$col];
        my $string_width = string_width($token);
    
        if (not defined $old_width or $string_width > $old_width) {
            # You may wish to set a minimum column width as follows.
            #return undef if $string_width < 10;
    
            $worksheet->{__col_widths}->[$col] = $string_width;
        }
    
    
        # Return control to write();
        return undef;
    }
    
    
    ######################################################################
    #+#########
    #
    # Very simple conversion between string length and string width for Ar
    #+ial 10.
    # See below for a more sophisticated method.
    #
    sub string_width {
    
        return length $_[0];
    }

    jay

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: saving multiple sheets into one csv file

    Thanks for the effort jay. It may not help the OP since its been 5 yrs+ since the topic was posted, but it will surely help others who search for this kind of issues.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Registered User
    Join Date
    05-10-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: saving multiple sheets into one csv file

    That was the idea. I faced this issue today. Couldn't find exact solution so wrote a script for my self and shared.


    Jay

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1