D
dave
Hello all,
I have been searching the archives for a while and can't seem to find a
solution to my problem. The quick and dirty of it is I am pulling data
from an Oracle database via DBI. I am using the following code to pull
data and write it into an excel spreadsheet:
59 while ( my @row = $sth->fetchrow_array() ) {
60 my $array_ref = \@row;
61 $worksheet1->keep_leading_zeros();
62 $worksheet1->write_row( $row_num, 0, $array_ref );
63 $row_num = $row_num + 1;
64
65 my $line = join("|", map{defined($_) ? $_ : ""} @row);
66 print $line, "\n";
67 push(@DOCIDS, "$row[2]");
68 push(@DATA, $line);
69 }
After executing this I fill up the array @DATA and @DOCIDS and I print
each row (ie line 66) so I know I am fetching the data correctly. But
my Excel doc is not being written. If I were to put a die statement at
line 62 it would say "File not found". What does this mean. This is
really driving me nuts! It should work. Let me know if I need to
supply more info. Below is the entire code from script:
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use DBI;
7 use Spreadsheet::WriteExcel;
8 use Getopt::Std;
9
10 # Database variables
11 # my $ORAINST = "BLAH";
12 my $ORAINST = "BLAH.BLAH.COM";
13 my $DBUSER = "xxxxxx";
14 # my $DBPASSWD = "xxxxxxx";
15 my $DBPASSWD = "xxxxxxxx";
16 my $DBH =
DBI->connect("dbi:Oracle:$ORAINST","$DBUSER","$DBPASSWD") or
17 die "Couldn't open database connection: $!";
18
19 # Options
20 use vars qw/%opt/;
21
22 # Globals
23 my @DATA = ();
24 my $ROW_LIMIT = 65536;
25 my @DOCIDS = ();
26 my $BUFFER_SIZE = 5000000;
27 my $LONG_RAW_TYPE = 113;
28
29
30 my $workbook = Spreadsheet::WriteExcel->new('test.xls');
31 $worksheet1 = $workbook->add_worksheet("test 1");
32
33 # Build initial headings
34 $worksheet1->write(0, 0, 'Supplier Id');
35 $worksheet1->write(0, 1, 'Agreement Id');
36 $worksheet1->write(0, 2, 'Document Id');
37 $worksheet1->write(0, 3, 'Outlet Id');
38 $worksheet1->write(0, 4, 'Supplier Customer Number');
39 $worksheet1->write(0, 5, 'Outlet');
40 $worksheet1->write(0, 6, 'Address 1');
41 $worksheet1->write(0, 7, 'City');
42 $worksheet1->write(0, 8, 'State');
43 $worksheet1->write(0, 9, 'Zip');
44 $worksheet1->write(0, 10, 'Display Name');
45 $worksheet1->write(0, 11, 'Display Type');
46 $worksheet1->write(0, 12, 'Product Name');
47 $worksheet1->write(0, 13, 'UPC');
48 $worksheet1->write(0, 14, 'Wholesaler Item Code');
49 $worksheet1->write(0, 15, 'Order Quantity');
50
51 #&init();
52
53 my $sqlStmt = &sql_statement_setup( $opt{S}, $opt{P} );
54
55 my $sth = $DBH->prepare($sqlStmt) || die "\nPrepare error: $DBI::err
.... $DBI::err str\n";
56 $sth->execute() || die "\nExecute error: $DBI::err ...
$DBI::errstr\n";
57
58 my $row_num = 1;
59 while ( my @row = $sth->fetchrow_array() ) {
60 my $array_ref = \@row;
61 $worksheet1->keep_leading_zeros();
62 $worksheet1->write_row( $row_num, 0, $array_ref );
63 $row_num = $row_num + 1;
64
65 my $line = join("|", map{defined($_) ? $_ : ""} @row);
66 print $line, "\n";
67 push(@DOCIDS, "$row[2]");
68 push(@DATA, $line);
69 }
70
71
72
73
74
75
76
77 # Subroutines
78
79 sub init() {
80 my $opt_string = 'hS:';
81 getopts("$opt_string", \%opt) or usage();
82 if (!defined($opt{S})) { $opt{S} = 1248; }
83 if (!defined($opt{P})) { $opt{P} = 18; }
84 usage() if $opt{h};
85
86 return 0;
87 }
88
89 sub usage() {
90 print <<EOF;
91 usage: $0 -S <supp id> -P <prgrm id>
92
93 -h rints this help message.
94 -S <supplier id> :The supplier id number.
95 -P <program id :The program id number.
96 EOF
97
98 return 1;
99 }
100
101
102 # sql_statement_setup()
103 sub sql_statement_setup() {
104 my $supplier_id = $_[0];
105 my $program_id = $_[1];
106
107 my $sql = <<END;
108 SELECT a.supplier_id,
109 a.agreement_id,
110 a.document_id,
111 a.outlet_id,
112 a.supplier_customer_number,
113 b.outlet_name,
114 c.address_1,
115 c.city,
116 c.state,
117 c.zip,
118 e.display_name,
119 e.display_type,
120 h.product_name,
121 h.upc,
122 (SELECT z.wholesaler_item_code
123 FROM stsdm.product_lu z
124 WHERE z.wholesaler_id = a.supplier_id
125 AND z.p_display_id = f.p_display_id
126 AND z.upc = h.upc
127 AND z.wholesaler_id = a.supplier_id) AS
WHOLESALER_ITEM_CODE,
128 nvl((SELECT z.order_quantity
129 FROM stsdm.product_lu z
130 WHERE z.wholesaler_id = a.supplier_id
131 AND z.p_display_id = f.p_display_id
132 AND z.upc = h.upc),
133 (SELECT max(z.order_quantity)
134 FROM stsdm.product_lu z
135 WHERE z.p_display_id = f.p_display_id
136 AND z.upc = h.upc)) AS ORDER_QUANTITY
137 FROM stsdm.agreement a,
138 sts.outlet b,
139 sts.location c,
140 stsdm.agreement_detail d,
141 stsdm.display e,
142 stsdm.program_display f,
143 stsdm.product_lu h
144 WHERE a.supplier_id = \'$supplier_id\'
145 AND a.program_id = \'$program_id\'
146 AND a.outlet_id = b.outlet_id
147 AND b.loc_id = c.loc_id
148 AND a.agreement_id = d.agreement_id
149 AND d.p_display_id = f.p_display_id
150 AND f.display_id = e.display_id
151 AND f.p_display_id = h.p_display_id
152 ORDER BY a.supplier_id, a.outlet_id, e.display_name
153 END
154
155 return $sql;
156 }
157
I have been searching the archives for a while and can't seem to find a
solution to my problem. The quick and dirty of it is I am pulling data
from an Oracle database via DBI. I am using the following code to pull
data and write it into an excel spreadsheet:
59 while ( my @row = $sth->fetchrow_array() ) {
60 my $array_ref = \@row;
61 $worksheet1->keep_leading_zeros();
62 $worksheet1->write_row( $row_num, 0, $array_ref );
63 $row_num = $row_num + 1;
64
65 my $line = join("|", map{defined($_) ? $_ : ""} @row);
66 print $line, "\n";
67 push(@DOCIDS, "$row[2]");
68 push(@DATA, $line);
69 }
After executing this I fill up the array @DATA and @DOCIDS and I print
each row (ie line 66) so I know I am fetching the data correctly. But
my Excel doc is not being written. If I were to put a die statement at
line 62 it would say "File not found". What does this mean. This is
really driving me nuts! It should work. Let me know if I need to
supply more info. Below is the entire code from script:
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use DBI;
7 use Spreadsheet::WriteExcel;
8 use Getopt::Std;
9
10 # Database variables
11 # my $ORAINST = "BLAH";
12 my $ORAINST = "BLAH.BLAH.COM";
13 my $DBUSER = "xxxxxx";
14 # my $DBPASSWD = "xxxxxxx";
15 my $DBPASSWD = "xxxxxxxx";
16 my $DBH =
DBI->connect("dbi:Oracle:$ORAINST","$DBUSER","$DBPASSWD") or
17 die "Couldn't open database connection: $!";
18
19 # Options
20 use vars qw/%opt/;
21
22 # Globals
23 my @DATA = ();
24 my $ROW_LIMIT = 65536;
25 my @DOCIDS = ();
26 my $BUFFER_SIZE = 5000000;
27 my $LONG_RAW_TYPE = 113;
28
29
30 my $workbook = Spreadsheet::WriteExcel->new('test.xls');
31 $worksheet1 = $workbook->add_worksheet("test 1");
32
33 # Build initial headings
34 $worksheet1->write(0, 0, 'Supplier Id');
35 $worksheet1->write(0, 1, 'Agreement Id');
36 $worksheet1->write(0, 2, 'Document Id');
37 $worksheet1->write(0, 3, 'Outlet Id');
38 $worksheet1->write(0, 4, 'Supplier Customer Number');
39 $worksheet1->write(0, 5, 'Outlet');
40 $worksheet1->write(0, 6, 'Address 1');
41 $worksheet1->write(0, 7, 'City');
42 $worksheet1->write(0, 8, 'State');
43 $worksheet1->write(0, 9, 'Zip');
44 $worksheet1->write(0, 10, 'Display Name');
45 $worksheet1->write(0, 11, 'Display Type');
46 $worksheet1->write(0, 12, 'Product Name');
47 $worksheet1->write(0, 13, 'UPC');
48 $worksheet1->write(0, 14, 'Wholesaler Item Code');
49 $worksheet1->write(0, 15, 'Order Quantity');
50
51 #&init();
52
53 my $sqlStmt = &sql_statement_setup( $opt{S}, $opt{P} );
54
55 my $sth = $DBH->prepare($sqlStmt) || die "\nPrepare error: $DBI::err
.... $DBI::err str\n";
56 $sth->execute() || die "\nExecute error: $DBI::err ...
$DBI::errstr\n";
57
58 my $row_num = 1;
59 while ( my @row = $sth->fetchrow_array() ) {
60 my $array_ref = \@row;
61 $worksheet1->keep_leading_zeros();
62 $worksheet1->write_row( $row_num, 0, $array_ref );
63 $row_num = $row_num + 1;
64
65 my $line = join("|", map{defined($_) ? $_ : ""} @row);
66 print $line, "\n";
67 push(@DOCIDS, "$row[2]");
68 push(@DATA, $line);
69 }
70
71
72
73
74
75
76
77 # Subroutines
78
79 sub init() {
80 my $opt_string = 'hS:';
81 getopts("$opt_string", \%opt) or usage();
82 if (!defined($opt{S})) { $opt{S} = 1248; }
83 if (!defined($opt{P})) { $opt{P} = 18; }
84 usage() if $opt{h};
85
86 return 0;
87 }
88
89 sub usage() {
90 print <<EOF;
91 usage: $0 -S <supp id> -P <prgrm id>
92
93 -h rints this help message.
94 -S <supplier id> :The supplier id number.
95 -P <program id :The program id number.
96 EOF
97
98 return 1;
99 }
100
101
102 # sql_statement_setup()
103 sub sql_statement_setup() {
104 my $supplier_id = $_[0];
105 my $program_id = $_[1];
106
107 my $sql = <<END;
108 SELECT a.supplier_id,
109 a.agreement_id,
110 a.document_id,
111 a.outlet_id,
112 a.supplier_customer_number,
113 b.outlet_name,
114 c.address_1,
115 c.city,
116 c.state,
117 c.zip,
118 e.display_name,
119 e.display_type,
120 h.product_name,
121 h.upc,
122 (SELECT z.wholesaler_item_code
123 FROM stsdm.product_lu z
124 WHERE z.wholesaler_id = a.supplier_id
125 AND z.p_display_id = f.p_display_id
126 AND z.upc = h.upc
127 AND z.wholesaler_id = a.supplier_id) AS
WHOLESALER_ITEM_CODE,
128 nvl((SELECT z.order_quantity
129 FROM stsdm.product_lu z
130 WHERE z.wholesaler_id = a.supplier_id
131 AND z.p_display_id = f.p_display_id
132 AND z.upc = h.upc),
133 (SELECT max(z.order_quantity)
134 FROM stsdm.product_lu z
135 WHERE z.p_display_id = f.p_display_id
136 AND z.upc = h.upc)) AS ORDER_QUANTITY
137 FROM stsdm.agreement a,
138 sts.outlet b,
139 sts.location c,
140 stsdm.agreement_detail d,
141 stsdm.display e,
142 stsdm.program_display f,
143 stsdm.product_lu h
144 WHERE a.supplier_id = \'$supplier_id\'
145 AND a.program_id = \'$program_id\'
146 AND a.outlet_id = b.outlet_id
147 AND b.loc_id = c.loc_id
148 AND a.agreement_id = d.agreement_id
149 AND d.p_display_id = f.p_display_id
150 AND f.display_id = e.display_id
151 AND f.p_display_id = h.p_display_id
152 ORDER BY a.supplier_id, a.outlet_id, e.display_name
153 END
154
155 return $sql;
156 }
157