Sponsored Content
Top Forums Shell Programming and Scripting Perl syntax for formatting columns and text Post 302892891 by sagar_1986 on Saturday 15th of March 2014 11:05:45 AM
Old 03-15-2014
Dear jim mcnamara,

I have just started to use perl script,pl find my code as below,every CSV created here contains text in first column as mentioned before but as soon as i open this excel sheet in windows all zeros which are prefixed to numbers are getting removed.Hence i want to convert this first column as text.
Thanks in advance.

Code:
#!/usr/bin/perl -w

use strict;
use warnings;
use Spreadsheet::Read;
use Spreadsheet::WriteExcel;
#Reading data from csv
my $workbook1 = ReadData("Overview.csv");
my @data1 = Spreadsheet::Read::rows($workbook1->[1]);

my $workbook2 = ReadData("1.csv");
my @data2 = Spreadsheet::Read::rows($workbook2->[1]);

my $workbook3 = ReadData("2.csv");
my @data3 = Spreadsheet::Read::rows($workbook3->[1]);

my $workbook4 = ReadData("3.csv");
my @data4 = Spreadsheet::Read::rows($workbook4->[1]);

my $workbook5 = ReadData("4.csv");
my @data5 = Spreadsheet::Read::rows($workbook5->[1]);

my $workbook6 = ReadData("5.csv");
my @data6 = Spreadsheet::Read::rows($workbook6->[1]);

#Creating new Excel workbook

my $workbook7 = Spreadsheet::WriteExcel->new('sagar.xls');

my $format1 = $workbook7->add_format();
## $format1->set_bold();
  $format1->set_color('black');
   $format1->set_align('center');
   $format1->set_font("Calibri");
   $format1->set_bg_color('orange');

   my $format2 = $workbook7->add_format();
    $format2->set_bold();
     $format2->set_color('green');
      $format2->set_align('right');
       $format2->set_bg_color('red');

       # Create a format for the column headings
       my $header = $workbook7->add_format();
       $header->set_bold();
       $header->set_font("Courier New");
       $header->set_align('center');
       $header->set_align('vcenter');
       $header->set_color('white');
       $header->set_bg_color('black');



       # Adding worksheet to workbook

    my $worksheet1 = $workbook7->add_worksheet("1");
    my $worksheet2 = $workbook7->add_worksheet("2");
    my $worksheet3 = $workbook7->add_worksheet("3");
    my $worksheet4 = $workbook7->add_worksheet("4");
    my $worksheet5 = $workbook7->add_worksheet("5");
    my $worksheet6 = $workbook7->add_worksheet("6");

###################################

$worksheet2->set_column(1, 1, 10);
$worksheet2->set_column(2, 2, 10);
$worksheet2->set_column(3, 3, 15);
$worksheet2->set_column(4, 4, 55);
$worksheet2->set_column(5, 5, 24);

$worksheet3->set_column(1, 1, 10);
$worksheet3->set_column(2, 2, 10);
$worksheet3->set_column(3, 3, 15);
$worksheet3->set_column(4, 4, 55);
$worksheet3->set_column(5, 5, 24);

$worksheet4->set_column(1, 1, 10);
$worksheet4->set_column(2, 2, 10);
$worksheet4->set_column(3, 3, 15);
$worksheet4->set_column(4, 4, 55);
$worksheet4->set_column(5, 5, 24);

$worksheet5->set_column(1, 1, 10);
$worksheet5->set_column(2, 2, 10);
$worksheet5->set_column(3, 3, 15);
$worksheet5->set_column(4, 4, 55);
$worksheet5->set_column(5, 5, 24);

$worksheet6->set_column(1, 1, 10);
$worksheet6->set_column(2, 2, 10);
$worksheet6->set_column(3, 3, 15);
$worksheet6->set_column(4, 4, 80);
$worksheet6->set_column(5, 5, 24);





       # Write the data to worksheet

       $worksheet1->write( 'B2',[\@data1],$format1);
       $worksheet2->write( 'B2',[\@data2],$format1);
       $worksheet3->write( 'B2',[\@data3],$format1);
       $worksheet4->write( 'B2',[\@data4],$format1);
       $worksheet5->write( 'B2',[\@data5],$format1);
       $worksheet6->write( 'B2',[\@data6],$format1);

##############

$worksheet2->write( 'B1','ID',$header);
$worksheet2->write( 'C1','ACTION',$header);
$worksheet2->write( 'D1','ACTION_DATE',$header);
$worksheet2->write( 'E1','MSG',$header);
$worksheet2->write( 'F1','DT',$header);

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Text formatting to 132 columns

I want to take a file that is generated in 132 columns from a unix based report generator and transfer it to a windows environment and preserve the column length. Any ideas how I can do this so that whatever windows based text editor that reads the document will know it should be in 132 columns?... (16 Replies)
Discussion started by: jmossman
16 Replies

2. Shell Programming and Scripting

Formatting info into columns

Hi This question has kind of been asked before but I couldn't get any of the solutions to work. I need to format fields being cut from a file into columns so it looks like a table. e.g Full name Address Hiredate Joe Smith London 11.01.01 Bill King ... (8 Replies)
Discussion started by: straight_edge
8 Replies

3. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies

4. Shell Programming and Scripting

Text formatting in Perl.

Hiho, I've got a variable $sth = `du -sh /home/$whoami`; where $whoami is actually logged user. I want to have only the size of this directory eg. 2,7G. I know its lame, but anyway.. how to do it? (2 Replies)
Discussion started by: fenox
2 Replies

5. UNIX for Dummies Questions & Answers

Formatting a line of data into columns

Hi all, I'm a little stuck with a data file I've been collecting data in. The file contains one field of data running continuously down the file and I can't work out how to format the data into three columns. This is a mock up of the file: Each r# is a random number and varies in length, this... (3 Replies)
Discussion started by: nistleloy
3 Replies

6. Shell Programming and Scripting

Formatting output in columns

I have a file which contains data in below format: nbkv28s MgmtReporting -> TradingDesk 1 nbkv28s RMBS -> Credits 178 nbkv28s RMBS -> PassThrough 96 nbkv28s RMBS -> Prepayment 111 nbkv28s RMBS -> RMBSHome 370 nbkv28s RMBS -> TradingStrategy 98 nbkvnze RMBS -> RMBSHome 85 nbkvugn GSF... (3 Replies)
Discussion started by: mohsin.quazi
3 Replies

7. Shell Programming and Scripting

formatting columns

Hi I have a file that is several gigs in size and basically I want to change the format of it. Obviously I cannot go through it manually. The file looks like this: 897 2 901 2 905 2 909 2 913 2 917 2 921 2 925 2 929 2 933 2 937 2 941 2 945 2 949 0 (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

Aligning columns in a text file using Perl

Hi All, I am new to perl and was trying to write a simple program which will generate a text file as output.. now the output which i am getting is something like this.. ================================================================================================== Col1 ... (8 Replies)
Discussion started by: smarty86
8 Replies

9. Shell Programming and Scripting

Formatting columns in file

I have a script that creates a file with three column output. The columns look like this: Policy Name Name Volume VLS-EDWARDS-ARCHIVED_FILE-WINDOWS 10 12 XLZ-BASE-CYGWIN-ARCHIVED-FILE-LINUX 2 21 ZLX-GOLD-FILES-JAPANFILERS1-LINUX 20 27 ZLX-GOLD-FILES-JAPANFILERS2-LINUX13 29... (4 Replies)
Discussion started by: newbie2010
4 Replies

10. Shell Programming and Scripting

Bash formatting data into columns

Hi guys, I'm trying to create a table of aggregated data using just bash commands. My data is in three columns, for example: 2014-01-01 testA 64 2014-01-01 testB 27 2014-02-01 testA 31 2014-02-02 testB 29 2014-02-02 testC 12 And the result I am looking for is: ... (4 Replies)
Discussion started by: mccmjc
4 Replies
Spreadsheet::WriteExcel::Chart::Column(3pm)		User Contributed Perl Documentation	       Spreadsheet::WriteExcel::Chart::Column(3pm)

NAME
Column - A writer class for Excel Column charts. SYNOPSIS
To create a simple Excel file with a Column chart using Spreadsheet::WriteExcel: #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' ); my $worksheet = $workbook->add_worksheet(); my $chart = $workbook->add_chart( type => 'column' ); # Configure the chart. $chart->add_series( categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$B$2:$B$7', ); # Add the worksheet data the chart refers to. my $data = [ [ 'Category', 2, 3, 4, 5, 6, 7 ], [ 'Value', 1, 4, 5, 2, 1, 5 ], ]; $worksheet->write( 'A1', $data ); __END__ DESCRIPTION
This module implements Column charts for Spreadsheet::WriteExcel. The chart object is created via the Workbook "add_chart()" method: my $chart = $workbook->add_chart( type => 'column' ); Once the object is created it can be configured via the following methods that are common to all chart classes: $chart->add_series(); $chart->set_x_axis(); $chart->set_y_axis(); $chart->set_title(); These methods are explained in detail in Spreadsheet::WriteExcel::Chart. Class specific methods or settings, if any, are explained below. Column Chart Methods There aren't currently any column chart specific methods. See the TODO section of Spreadsheet::WriteExcel::Chart. EXAMPLE
Here is a complete example that demonstrates most of the available features when creating a chart. #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new( 'chart_column.xls' ); my $worksheet = $workbook->add_worksheet(); my $bold = $workbook->add_format( bold => 1 ); # Add the worksheet data that the charts will refer to. my $headings = [ 'Number', 'Sample 1', 'Sample 2' ]; my $data = [ [ 2, 3, 4, 5, 6, 7 ], [ 1, 4, 5, 2, 1, 5 ], [ 3, 6, 7, 5, 4, 3 ], ]; $worksheet->write( 'A1', $headings, $bold ); $worksheet->write( 'A2', $data ); # Create a new chart object. In this case an embedded chart. my $chart = $workbook->add_chart( type => 'column', embedded => 1 ); # Configure the first series. (Sample 1) $chart->add_series( name => 'Sample 1', categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$B$2:$B$7', ); # Configure the second series. (Sample 2) $chart->add_series( name => 'Sample 2', categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$C$2:$C$7', ); # Add a chart title and some axis labels. $chart->set_title ( name => 'Results of sample analysis' ); $chart->set_x_axis( name => 'Test number' ); $chart->set_y_axis( name => 'Sample length (cm)' ); # Insert the chart into the worksheet (with an offset). $worksheet->insert_chart( 'D2', $chart, 25, 10 ); __END__ AUTHOR
John McNamara jmcnamara@cpan.org COPYRIGHT
Copyright MM-MMX, John McNamara. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.10.1 2010-02-02 Spreadsheet::WriteExcel::Chart::Column(3pm)
All times are GMT -4. The time now is 04:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy