yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame^] | 1 | package Spreadsheet::WriteExcel::Examples; |
| 2 | |
| 3 | ############################################################################### |
| 4 | # |
| 5 | # Examples - Spreadsheet::WriteExcel examples. |
| 6 | # |
| 7 | # A documentation only module showing the examples that are |
| 8 | # included in the Spreadsheet::WriteExcel distribution. This |
| 9 | # file was generated automatically via the gen_examples_pod.pl |
| 10 | # program that is also included in the examples directory. |
| 11 | # |
| 12 | # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org |
| 13 | # |
| 14 | # Documentation after __END__ |
| 15 | # |
| 16 | |
| 17 | use strict; |
| 18 | use vars qw($VERSION); |
| 19 | $VERSION = '2.37'; |
| 20 | |
| 21 | 1; |
| 22 | |
| 23 | __END__ |
| 24 | |
| 25 | =pod |
| 26 | |
| 27 | =head1 NAME |
| 28 | |
| 29 | Examples - Spreadsheet::WriteExcel example programs. |
| 30 | |
| 31 | =head1 DESCRIPTION |
| 32 | |
| 33 | This is a documentation only module showing the examples that are |
| 34 | included in the L<Spreadsheet::WriteExcel> distribution. |
| 35 | |
| 36 | This file was auto-generated via the gen_examples_pod.pl |
| 37 | program that is also included in the examples directory. |
| 38 | |
| 39 | =head1 Example programs |
| 40 | |
| 41 | The following is a list of the 85 example programs that are included in the Spreadsheet::WriteExcel distribution. |
| 42 | |
| 43 | =over |
| 44 | |
| 45 | =item * L<Example: a_simple.pl> A get started example with some basic features. |
| 46 | |
| 47 | =item * L<Example: demo.pl> A demo of some of the available features. |
| 48 | |
| 49 | =item * L<Example: regions.pl> A simple example of multiple worksheets. |
| 50 | |
| 51 | =item * L<Example: stats.pl> Basic formulas and functions. |
| 52 | |
| 53 | =item * L<Example: formats.pl> All the available formatting on several worksheets. |
| 54 | |
| 55 | =item * L<Example: bug_report.pl> A template for submitting bug reports. |
| 56 | |
| 57 | =item * L<Example: autofilter.pl> Examples of worksheet autofilters. |
| 58 | |
| 59 | =item * L<Example: autofit.pl> Simulate Excel's autofit for column widths. |
| 60 | |
| 61 | =item * L<Example: bigfile.pl> Write past the 7MB limit with OLE::Storage_Lite. |
| 62 | |
| 63 | =item * L<Example: cgi.pl> A simple CGI program. |
| 64 | |
| 65 | =item * L<Example: chart_area.pl> A demo of area style charts. |
| 66 | |
| 67 | =item * L<Example: chart_bar.pl> A demo of bar (vertical histogram) style charts. |
| 68 | |
| 69 | =item * L<Example: chart_column.pl> A demo of column (histogram) style charts. |
| 70 | |
| 71 | =item * L<Example: chart_line.pl> A demo of line style charts. |
| 72 | |
| 73 | =item * L<Example: chart_pie.pl> A demo of pie style charts. |
| 74 | |
| 75 | =item * L<Example: chart_scatter.pl> A demo of scatter style charts. |
| 76 | |
| 77 | =item * L<Example: chart_stock.pl> A demo of stock style charts. |
| 78 | |
| 79 | =item * L<Example: chess.pl> An example of reusing formatting via properties. |
| 80 | |
| 81 | =item * L<Example: colors.pl> A demo of the colour palette and named colours. |
| 82 | |
| 83 | =item * L<Example: comments1.pl> Add comments to worksheet cells. |
| 84 | |
| 85 | =item * L<Example: comments2.pl> Add comments with advanced options. |
| 86 | |
| 87 | =item * L<Example: copyformat.pl> Example of copying a cell format. |
| 88 | |
| 89 | =item * L<Example: data_validate.pl> An example of data validation and dropdown lists. |
| 90 | |
| 91 | =item * L<Example: date_time.pl> Write dates and times with write_date_time(). |
| 92 | |
| 93 | =item * L<Example: defined_name.pl> Example of how to create defined names. |
| 94 | |
| 95 | =item * L<Example: diag_border.pl> A simple example of diagonal cell borders. |
| 96 | |
| 97 | =item * L<Example: easter_egg.pl> Expose the Excel97 flight simulator. |
| 98 | |
| 99 | =item * L<Example: filehandle.pl> Examples of working with filehandles. |
| 100 | |
| 101 | =item * L<Example: formula_result.pl> Formulas with user specified results. |
| 102 | |
| 103 | =item * L<Example: headers.pl> Examples of worksheet headers and footers. |
| 104 | |
| 105 | =item * L<Example: hide_sheet.pl> Simple example of hiding a worksheet. |
| 106 | |
| 107 | =item * L<Example: hyperlink1.pl> Shows how to create web hyperlinks. |
| 108 | |
| 109 | =item * L<Example: hyperlink2.pl> Examples of internal and external hyperlinks. |
| 110 | |
| 111 | =item * L<Example: images.pl> Adding images to worksheets. |
| 112 | |
| 113 | =item * L<Example: indent.pl> An example of cell indentation. |
| 114 | |
| 115 | =item * L<Example: merge1.pl> A simple example of cell merging. |
| 116 | |
| 117 | =item * L<Example: merge2.pl> A simple example of cell merging with formatting. |
| 118 | |
| 119 | =item * L<Example: merge3.pl> Add hyperlinks to merged cells. |
| 120 | |
| 121 | =item * L<Example: merge4.pl> An advanced example of merging with formatting. |
| 122 | |
| 123 | =item * L<Example: merge5.pl> An advanced example of merging with formatting. |
| 124 | |
| 125 | =item * L<Example: merge6.pl> An example of merging with Unicode strings. |
| 126 | |
| 127 | =item * L<Example: mod_perl1.pl> A simple mod_perl 1 program. |
| 128 | |
| 129 | =item * L<Example: mod_perl2.pl> A simple mod_perl 2 program. |
| 130 | |
| 131 | =item * L<Example: outline.pl> An example of outlines and grouping. |
| 132 | |
| 133 | =item * L<Example: outline_collapsed.pl> An example of collapsed outlines. |
| 134 | |
| 135 | =item * L<Example: panes.pl> An examples of how to create panes. |
| 136 | |
| 137 | =item * L<Example: properties.pl> Add document properties to a workbook. |
| 138 | |
| 139 | =item * L<Example: protection.pl> Example of cell locking and formula hiding. |
| 140 | |
| 141 | =item * L<Example: repeat.pl> Example of writing repeated formulas. |
| 142 | |
| 143 | =item * L<Example: right_to_left.pl> Change default sheet direction to right to left. |
| 144 | |
| 145 | =item * L<Example: row_wrap.pl> How to wrap data from one worksheet onto another. |
| 146 | |
| 147 | =item * L<Example: sales.pl> An example of a simple sales spreadsheet. |
| 148 | |
| 149 | =item * L<Example: sendmail.pl> Send an Excel email attachment using Mail::Sender. |
| 150 | |
| 151 | =item * L<Example: stats_ext.pl> Same as stats.pl with external references. |
| 152 | |
| 153 | =item * L<Example: stocks.pl> Demonstrates conditional formatting. |
| 154 | |
| 155 | =item * L<Example: tab_colors.pl> Example of how to set worksheet tab colours. |
| 156 | |
| 157 | =item * L<Example: textwrap.pl> Demonstrates text wrapping options. |
| 158 | |
| 159 | =item * L<Example: win32ole.pl> A sample Win32::OLE example for comparison. |
| 160 | |
| 161 | =item * L<Example: write_arrays.pl> Example of writing 1D or 2D arrays of data. |
| 162 | |
| 163 | =item * L<Example: write_handler1.pl> Example of extending the write() method. Step 1. |
| 164 | |
| 165 | =item * L<Example: write_handler2.pl> Example of extending the write() method. Step 2. |
| 166 | |
| 167 | =item * L<Example: write_handler3.pl> Example of extending the write() method. Step 3. |
| 168 | |
| 169 | =item * L<Example: write_handler4.pl> Example of extending the write() method. Step 4. |
| 170 | |
| 171 | =item * L<Example: write_to_scalar.pl> Example of writing an Excel file to a Perl scalar. |
| 172 | |
| 173 | =item * L<Example: unicode_utf16.pl> Simple example of using Unicode UTF16 strings. |
| 174 | |
| 175 | =item * L<Example: unicode_utf16_japan.pl> Write Japanese Unicode strings using UTF-16. |
| 176 | |
| 177 | =item * L<Example: unicode_cyrillic.pl> Write Russian Cyrillic strings using UTF-8. |
| 178 | |
| 179 | =item * L<Example: unicode_list.pl> List the chars in a Unicode font. |
| 180 | |
| 181 | =item * L<Example: unicode_2022_jp.pl> Japanese: ISO-2022-JP to utf8 in perl 5.8. |
| 182 | |
| 183 | =item * L<Example: unicode_8859_11.pl> Thai: ISO-8859_11 to utf8 in perl 5.8. |
| 184 | |
| 185 | =item * L<Example: unicode_8859_7.pl> Greek: ISO-8859_7 to utf8 in perl 5.8. |
| 186 | |
| 187 | =item * L<Example: unicode_big5.pl> Chinese: BIG5 to utf8 in perl 5.8. |
| 188 | |
| 189 | =item * L<Example: unicode_cp1251.pl> Russian: CP1251 to utf8 in perl 5.8. |
| 190 | |
| 191 | =item * L<Example: unicode_cp1256.pl> Arabic: CP1256 to utf8 in perl 5.8. |
| 192 | |
| 193 | =item * L<Example: unicode_koi8r.pl> Russian: KOI8-R to utf8 in perl 5.8. |
| 194 | |
| 195 | =item * L<Example: unicode_polish_utf8.pl> Polish : UTF8 to utf8 in perl 5.8. |
| 196 | |
| 197 | =item * L<Example: unicode_shift_jis.pl> Japanese: Shift JIS to utf8 in perl 5.8. |
| 198 | |
| 199 | =item * L<Example: csv2xls.pl> Program to convert a CSV file to an Excel file. |
| 200 | |
| 201 | =item * L<Example: tab2xls.pl> Program to convert a tab separated file to xls. |
| 202 | |
| 203 | =item * L<Example: datecalc1.pl> Convert Unix/Perl time to Excel time. |
| 204 | |
| 205 | =item * L<Example: datecalc2.pl> Calculate an Excel date using Date::Calc. |
| 206 | |
| 207 | =item * L<Example: lecxe.pl> Convert Excel to WriteExcel using Win32::OLE. |
| 208 | |
| 209 | =item * L<Example: convertA1.pl> Helper functions for dealing with A1 notation. |
| 210 | |
| 211 | =item * L<Example: function_locale.pl> Add non-English function names to Formula.pm. |
| 212 | |
| 213 | =item * L<Example: writeA1.pl> Example of how to extend the module. |
| 214 | |
| 215 | =back |
| 216 | |
| 217 | =head2 Example: a_simple.pl |
| 218 | |
| 219 | |
| 220 | |
| 221 | A simple example of how to use the Spreadsheet::WriteExcel module to write |
| 222 | some text and numbers to an Excel binary file. |
| 223 | |
| 224 | |
| 225 | |
| 226 | =begin html |
| 227 | |
| 228 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/a_simple.jpg" width="640" height="420" alt="Output from a_simple.pl" /></center></p> |
| 229 | |
| 230 | =end html |
| 231 | |
| 232 | Source code for this example: |
| 233 | |
| 234 | #!/usr/bin/perl -w |
| 235 | |
| 236 | ############################################################################### |
| 237 | # |
| 238 | # A simple example of how to use the Spreadsheet::WriteExcel module to write |
| 239 | # some text and numbers to an Excel binary file. |
| 240 | # |
| 241 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 242 | # |
| 243 | |
| 244 | use strict; |
| 245 | use Spreadsheet::WriteExcel; |
| 246 | |
| 247 | # Create a new workbook called simple.xls and add a worksheet |
| 248 | my $workbook = Spreadsheet::WriteExcel->new('a_simple.xls'); |
| 249 | my $worksheet = $workbook->add_worksheet(); |
| 250 | |
| 251 | # The general syntax is write($row, $column, $token). Note that row and |
| 252 | # column are zero indexed |
| 253 | # |
| 254 | |
| 255 | # Write some text |
| 256 | $worksheet->write(0, 0, "Hi Excel!"); |
| 257 | |
| 258 | |
| 259 | # Write some numbers |
| 260 | $worksheet->write(2, 0, 3); # Writes 3 |
| 261 | $worksheet->write(3, 0, 3.00000); # Writes 3 |
| 262 | $worksheet->write(4, 0, 3.00001); # Writes 3.00001 |
| 263 | $worksheet->write(5, 0, 3.14159); # TeX revision no.? |
| 264 | |
| 265 | |
| 266 | # Write some formulas |
| 267 | $worksheet->write(7, 0, '=A3 + A6'); |
| 268 | $worksheet->write(8, 0, '=IF(A5>3,"Yes", "No")'); |
| 269 | |
| 270 | |
| 271 | # Write a hyperlink |
| 272 | $worksheet->write(10, 0, 'http://www.perl.com/'); |
| 273 | |
| 274 | __END__ |
| 275 | |
| 276 | |
| 277 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/a_simple.pl> |
| 278 | |
| 279 | =head2 Example: demo.pl |
| 280 | |
| 281 | |
| 282 | |
| 283 | A simple demo of some of the features of Spreadsheet::WriteExcel. |
| 284 | |
| 285 | This program is used to create the project screenshot for Freshmeat: |
| 286 | L<http://freshmeat.net/projects/writeexcel/> |
| 287 | |
| 288 | |
| 289 | |
| 290 | =begin html |
| 291 | |
| 292 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/demo.jpg" width="640" height="420" alt="Output from demo.pl" /></center></p> |
| 293 | |
| 294 | =end html |
| 295 | |
| 296 | Source code for this example: |
| 297 | |
| 298 | #!/usr/bin/perl -w |
| 299 | |
| 300 | ####################################################################### |
| 301 | # |
| 302 | # A simple demo of some of the features of Spreadsheet::WriteExcel. |
| 303 | # |
| 304 | # This program is used to create the project screenshot for Freshmeat: |
| 305 | # L<http://freshmeat.net/projects/writeexcel/> |
| 306 | # |
| 307 | # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org |
| 308 | # |
| 309 | |
| 310 | use strict; |
| 311 | use Spreadsheet::WriteExcel; |
| 312 | |
| 313 | my $workbook = Spreadsheet::WriteExcel->new("demo.xls"); |
| 314 | my $worksheet = $workbook->add_worksheet('Demo'); |
| 315 | my $worksheet2 = $workbook->add_worksheet('Another sheet'); |
| 316 | my $worksheet3 = $workbook->add_worksheet('And another'); |
| 317 | |
| 318 | my $bold = $workbook->add_format(bold => 1); |
| 319 | |
| 320 | |
| 321 | ####################################################################### |
| 322 | # |
| 323 | # Write a general heading |
| 324 | # |
| 325 | $worksheet->set_column('A:A', 36, $bold); |
| 326 | $worksheet->set_column('B:B', 20 ); |
| 327 | $worksheet->set_row (0, 40 ); |
| 328 | |
| 329 | my $heading = $workbook->add_format( |
| 330 | bold => 1, |
| 331 | color => 'blue', |
| 332 | size => 16, |
| 333 | merge => 1, |
| 334 | align => 'vcenter', |
| 335 | ); |
| 336 | |
| 337 | my @headings = ('Features of Spreadsheet::WriteExcel', ''); |
| 338 | $worksheet->write_row('A1', \@headings, $heading); |
| 339 | |
| 340 | |
| 341 | ####################################################################### |
| 342 | # |
| 343 | # Some text examples |
| 344 | # |
| 345 | my $text_format = $workbook->add_format( |
| 346 | bold => 1, |
| 347 | italic => 1, |
| 348 | color => 'red', |
| 349 | size => 18, |
| 350 | font =>'Lucida Calligraphy' |
| 351 | ); |
| 352 | |
| 353 | # A phrase in Cyrillic |
| 354 | my $unicode = pack "H*", "042d0442043e002004440440043004370430002004". |
| 355 | "3d043000200440044304410441043a043e043c0021"; |
| 356 | |
| 357 | |
| 358 | $worksheet->write('A2', "Text"); |
| 359 | $worksheet->write('B2', "Hello Excel"); |
| 360 | $worksheet->write('A3', "Formatted text"); |
| 361 | $worksheet->write('B3', "Hello Excel", $text_format); |
| 362 | $worksheet->write('A4', "Unicode text"); |
| 363 | $worksheet->write_utf16be_string('B4', $unicode); |
| 364 | |
| 365 | ####################################################################### |
| 366 | # |
| 367 | # Some numeric examples |
| 368 | # |
| 369 | my $num1_format = $workbook->add_format(num_format => '$#,##0.00'); |
| 370 | my $num2_format = $workbook->add_format(num_format => ' d mmmm yyy'); |
| 371 | |
| 372 | |
| 373 | $worksheet->write('A5', "Numbers"); |
| 374 | $worksheet->write('B5', 1234.56); |
| 375 | $worksheet->write('A6', "Formatted numbers"); |
| 376 | $worksheet->write('B6', 1234.56, $num1_format); |
| 377 | $worksheet->write('A7', "Formatted numbers"); |
| 378 | $worksheet->write('B7', 37257, $num2_format); |
| 379 | |
| 380 | |
| 381 | ####################################################################### |
| 382 | # |
| 383 | # Formulae |
| 384 | # |
| 385 | $worksheet->set_selection('B8'); |
| 386 | $worksheet->write('A8', 'Formulas and functions, "=SIN(PI()/4)"'); |
| 387 | $worksheet->write('B8', '=SIN(PI()/4)'); |
| 388 | |
| 389 | |
| 390 | ####################################################################### |
| 391 | # |
| 392 | # Hyperlinks |
| 393 | # |
| 394 | $worksheet->write('A9', "Hyperlinks"); |
| 395 | $worksheet->write('B9', 'http://www.perl.com/' ); |
| 396 | |
| 397 | |
| 398 | ####################################################################### |
| 399 | # |
| 400 | # Images |
| 401 | # |
| 402 | $worksheet->write('A10', "Images"); |
| 403 | $worksheet->insert_image('B10', 'republic.png', 16, 8); |
| 404 | |
| 405 | |
| 406 | ####################################################################### |
| 407 | # |
| 408 | # Misc |
| 409 | # |
| 410 | $worksheet->write('A18', "Page/printer setup"); |
| 411 | $worksheet->write('A19', "Multiple worksheets"); |
| 412 | |
| 413 | __END__ |
| 414 | |
| 415 | |
| 416 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/demo.pl> |
| 417 | |
| 418 | =head2 Example: regions.pl |
| 419 | |
| 420 | |
| 421 | |
| 422 | An example of how to use the Spreadsheet:WriteExcel module to write a basic |
| 423 | Excel workbook with multiple worksheets. |
| 424 | |
| 425 | |
| 426 | |
| 427 | =begin html |
| 428 | |
| 429 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/regions.jpg" width="640" height="420" alt="Output from regions.pl" /></center></p> |
| 430 | |
| 431 | =end html |
| 432 | |
| 433 | Source code for this example: |
| 434 | |
| 435 | #!/usr/bin/perl -w |
| 436 | |
| 437 | ############################################################################### |
| 438 | # |
| 439 | # An example of how to use the Spreadsheet:WriteExcel module to write a basic |
| 440 | # Excel workbook with multiple worksheets. |
| 441 | # |
| 442 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 443 | # |
| 444 | |
| 445 | use strict; |
| 446 | use Spreadsheet::WriteExcel; |
| 447 | |
| 448 | # Create a new Excel workbook |
| 449 | my $workbook = Spreadsheet::WriteExcel->new("regions.xls"); |
| 450 | |
| 451 | # Add some worksheets |
| 452 | my $north = $workbook->add_worksheet("North"); |
| 453 | my $south = $workbook->add_worksheet("South"); |
| 454 | my $east = $workbook->add_worksheet("East"); |
| 455 | my $west = $workbook->add_worksheet("West"); |
| 456 | |
| 457 | # Add a Format |
| 458 | my $format = $workbook->add_format(); |
| 459 | $format->set_bold(); |
| 460 | $format->set_color('blue'); |
| 461 | |
| 462 | # Add a caption to each worksheet |
| 463 | foreach my $worksheet ($workbook->sheets()) { |
| 464 | $worksheet->write(0, 0, "Sales", $format); |
| 465 | } |
| 466 | |
| 467 | # Write some data |
| 468 | $north->write(0, 1, 200000); |
| 469 | $south->write(0, 1, 100000); |
| 470 | $east->write (0, 1, 150000); |
| 471 | $west->write (0, 1, 100000); |
| 472 | |
| 473 | # Set the active worksheet |
| 474 | $south->activate(); |
| 475 | |
| 476 | # Set the width of the first column |
| 477 | $south->set_column(0, 0, 20); |
| 478 | |
| 479 | # Set the active cell |
| 480 | $south->set_selection(0, 1); |
| 481 | |
| 482 | |
| 483 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/regions.pl> |
| 484 | |
| 485 | =head2 Example: stats.pl |
| 486 | |
| 487 | |
| 488 | |
| 489 | A simple example of how to use functions with the Spreadsheet::WriteExcel |
| 490 | module. |
| 491 | |
| 492 | |
| 493 | |
| 494 | =begin html |
| 495 | |
| 496 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stats.jpg" width="640" height="420" alt="Output from stats.pl" /></center></p> |
| 497 | |
| 498 | =end html |
| 499 | |
| 500 | Source code for this example: |
| 501 | |
| 502 | #!/usr/bin/perl -w |
| 503 | |
| 504 | ############################################################################### |
| 505 | # |
| 506 | # A simple example of how to use functions with the Spreadsheet::WriteExcel |
| 507 | # module. |
| 508 | # |
| 509 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 510 | # |
| 511 | |
| 512 | use strict; |
| 513 | use Spreadsheet::WriteExcel; |
| 514 | |
| 515 | # Create a new workbook and add a worksheet |
| 516 | my $workbook = Spreadsheet::WriteExcel->new("stats.xls"); |
| 517 | my $worksheet = $workbook->add_worksheet('Test data'); |
| 518 | |
| 519 | # Set the column width for columns 1 |
| 520 | $worksheet->set_column(0, 0, 20); |
| 521 | |
| 522 | |
| 523 | # Create a format for the headings |
| 524 | my $format = $workbook->add_format(); |
| 525 | $format->set_bold(); |
| 526 | |
| 527 | |
| 528 | # Write the sample data |
| 529 | $worksheet->write(0, 0, 'Sample', $format); |
| 530 | $worksheet->write(0, 1, 1); |
| 531 | $worksheet->write(0, 2, 2); |
| 532 | $worksheet->write(0, 3, 3); |
| 533 | $worksheet->write(0, 4, 4); |
| 534 | $worksheet->write(0, 5, 5); |
| 535 | $worksheet->write(0, 6, 6); |
| 536 | $worksheet->write(0, 7, 7); |
| 537 | $worksheet->write(0, 8, 8); |
| 538 | |
| 539 | $worksheet->write(1, 0, 'Length', $format); |
| 540 | $worksheet->write(1, 1, 25.4); |
| 541 | $worksheet->write(1, 2, 25.4); |
| 542 | $worksheet->write(1, 3, 24.8); |
| 543 | $worksheet->write(1, 4, 25.0); |
| 544 | $worksheet->write(1, 5, 25.3); |
| 545 | $worksheet->write(1, 6, 24.9); |
| 546 | $worksheet->write(1, 7, 25.2); |
| 547 | $worksheet->write(1, 8, 24.8); |
| 548 | |
| 549 | # Write some statistical functions |
| 550 | $worksheet->write(4, 0, 'Count', $format); |
| 551 | $worksheet->write(4, 1, '=COUNT(B1:I1)'); |
| 552 | |
| 553 | $worksheet->write(5, 0, 'Sum', $format); |
| 554 | $worksheet->write(5, 1, '=SUM(B2:I2)'); |
| 555 | |
| 556 | $worksheet->write(6, 0, 'Average', $format); |
| 557 | $worksheet->write(6, 1, '=AVERAGE(B2:I2)'); |
| 558 | |
| 559 | $worksheet->write(7, 0, 'Min', $format); |
| 560 | $worksheet->write(7, 1, '=MIN(B2:I2)'); |
| 561 | |
| 562 | $worksheet->write(8, 0, 'Max', $format); |
| 563 | $worksheet->write(8, 1, '=MAX(B2:I2)'); |
| 564 | |
| 565 | $worksheet->write(9, 0, 'Standard Deviation', $format); |
| 566 | $worksheet->write(9, 1, '=STDEV(B2:I2)'); |
| 567 | |
| 568 | $worksheet->write(10, 0, 'Kurtosis', $format); |
| 569 | $worksheet->write(10, 1, '=KURT(B2:I2)'); |
| 570 | |
| 571 | __END__ |
| 572 | |
| 573 | |
| 574 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stats.pl> |
| 575 | |
| 576 | =head2 Example: formats.pl |
| 577 | |
| 578 | |
| 579 | |
| 580 | Examples of formatting using the Spreadsheet::WriteExcel module. |
| 581 | |
| 582 | This program demonstrates almost all possible formatting options. It is worth |
| 583 | running this program and viewing the output Excel file if you are interested |
| 584 | in the various formatting possibilities. |
| 585 | |
| 586 | |
| 587 | |
| 588 | =begin html |
| 589 | |
| 590 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/formats.jpg" width="640" height="420" alt="Output from formats.pl" /></center></p> |
| 591 | |
| 592 | =end html |
| 593 | |
| 594 | Source code for this example: |
| 595 | |
| 596 | #!/usr/bin/perl -w |
| 597 | |
| 598 | ############################################################################### |
| 599 | # |
| 600 | # Examples of formatting using the Spreadsheet::WriteExcel module. |
| 601 | # |
| 602 | # This program demonstrates almost all possible formatting options. It is worth |
| 603 | # running this program and viewing the output Excel file if you are interested |
| 604 | # in the various formatting possibilities. |
| 605 | # |
| 606 | # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org |
| 607 | # |
| 608 | |
| 609 | use strict; |
| 610 | use Spreadsheet::WriteExcel; |
| 611 | |
| 612 | my $workbook = Spreadsheet::WriteExcel->new('formats.xls'); |
| 613 | |
| 614 | # Some common formats |
| 615 | my $center = $workbook->add_format(align => 'center'); |
| 616 | my $heading = $workbook->add_format(align => 'center', bold => 1); |
| 617 | |
| 618 | # The named colors |
| 619 | my %colors = ( |
| 620 | 0x08, 'black', |
| 621 | 0x0C, 'blue', |
| 622 | 0x10, 'brown', |
| 623 | 0x0F, 'cyan', |
| 624 | 0x17, 'gray', |
| 625 | 0x11, 'green', |
| 626 | 0x0B, 'lime', |
| 627 | 0x0E, 'magenta', |
| 628 | 0x12, 'navy', |
| 629 | 0x35, 'orange', |
| 630 | 0x21, 'pink', |
| 631 | 0x14, 'purple', |
| 632 | 0x0A, 'red', |
| 633 | 0x16, 'silver', |
| 634 | 0x09, 'white', |
| 635 | 0x0D, 'yellow', |
| 636 | ); |
| 637 | |
| 638 | # Call these subroutines to demonstrate different formatting options |
| 639 | intro(); |
| 640 | fonts(); |
| 641 | named_colors(); |
| 642 | standard_colors(); |
| 643 | numeric_formats(); |
| 644 | borders(); |
| 645 | patterns(); |
| 646 | alignment(); |
| 647 | misc(); |
| 648 | |
| 649 | # Note: this is required |
| 650 | $workbook->close(); |
| 651 | |
| 652 | |
| 653 | ###################################################################### |
| 654 | # |
| 655 | # Intro. |
| 656 | # |
| 657 | sub intro { |
| 658 | |
| 659 | my $worksheet = $workbook->add_worksheet('Introduction'); |
| 660 | |
| 661 | $worksheet->set_column(0, 0, 60); |
| 662 | |
| 663 | my $format = $workbook->add_format(); |
| 664 | $format->set_bold(); |
| 665 | $format->set_size(14); |
| 666 | $format->set_color('blue'); |
| 667 | $format->set_align('center'); |
| 668 | |
| 669 | my $format2 = $workbook->add_format(); |
| 670 | $format2->set_bold(); |
| 671 | $format2->set_color('blue'); |
| 672 | |
| 673 | $worksheet->write(2, 0, 'This workbook demonstrates some of', $format); |
| 674 | $worksheet->write(3, 0, 'the formatting options provided by', $format); |
| 675 | $worksheet->write(4, 0, 'the Spreadsheet::WriteExcel module.', $format); |
| 676 | |
| 677 | $worksheet->write('A7', 'Sections:', $format2); |
| 678 | $worksheet->write('A8', "internal:Fonts!A1", 'Fonts' ); |
| 679 | $worksheet->write('A9', "internal:'Named colors'!A1", 'Named colors' ); |
| 680 | $worksheet->write('A10', "internal:'Standard colors'!A1", 'Standard colors'); |
| 681 | $worksheet->write('A11', "internal:'Numeric formats'!A1", 'Numeric formats'); |
| 682 | $worksheet->write('A12', "internal:Borders!A1", 'Borders' ); |
| 683 | $worksheet->write('A13', "internal:Patterns!A1", 'Patterns' ); |
| 684 | $worksheet->write('A14', "internal:Alignment!A1", 'Alignment' ); |
| 685 | $worksheet->write('A15', "internal:Miscellaneous!A1", 'Miscellaneous' ); |
| 686 | |
| 687 | } |
| 688 | |
| 689 | |
| 690 | ###################################################################### |
| 691 | # |
| 692 | # Demonstrate the named colors. |
| 693 | # |
| 694 | sub named_colors { |
| 695 | |
| 696 | my $worksheet = $workbook->add_worksheet('Named colors'); |
| 697 | |
| 698 | $worksheet->set_column(0, 3, 15); |
| 699 | |
| 700 | $worksheet->write(0, 0, "Index", $heading); |
| 701 | $worksheet->write(0, 1, "Index", $heading); |
| 702 | $worksheet->write(0, 2, "Name", $heading); |
| 703 | $worksheet->write(0, 3, "Color", $heading); |
| 704 | |
| 705 | my $i = 1; |
| 706 | |
| 707 | while (my($index, $color) = each %colors) { |
| 708 | my $format = $workbook->add_format( |
| 709 | bg_color => $color, |
| 710 | pattern => 1, |
| 711 | border => 1 |
| 712 | ); |
| 713 | |
| 714 | $worksheet->write($i+1, 0, $index, $center); |
| 715 | $worksheet->write($i+1, 1, sprintf("0x%02X", $index), $center); |
| 716 | $worksheet->write($i+1, 2, $color, $center); |
| 717 | $worksheet->write($i+1, 3, '', $format); |
| 718 | $i++; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
| 723 | ###################################################################### |
| 724 | # |
| 725 | # Demonstrate the standard Excel colors in the range 8..63. |
| 726 | # |
| 727 | sub standard_colors { |
| 728 | |
| 729 | my $worksheet = $workbook->add_worksheet('Standard colors'); |
| 730 | |
| 731 | $worksheet->set_column(0, 3, 15); |
| 732 | |
| 733 | $worksheet->write(0, 0, "Index", $heading); |
| 734 | $worksheet->write(0, 1, "Index", $heading); |
| 735 | $worksheet->write(0, 2, "Color", $heading); |
| 736 | $worksheet->write(0, 3, "Name", $heading); |
| 737 | |
| 738 | for my $i (8..63) { |
| 739 | my $format = $workbook->add_format( |
| 740 | bg_color => $i, |
| 741 | pattern => 1, |
| 742 | border => 1 |
| 743 | ); |
| 744 | |
| 745 | $worksheet->write(($i -7), 0, $i, $center); |
| 746 | $worksheet->write(($i -7), 1, sprintf("0x%02X", $i), $center); |
| 747 | $worksheet->write(($i -7), 2, '', $format); |
| 748 | |
| 749 | # Add the color names |
| 750 | if (exists $colors{$i}) { |
| 751 | $worksheet->write(($i -7), 3, $colors{$i}, $center); |
| 752 | |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | |
| 758 | ###################################################################### |
| 759 | # |
| 760 | # Demonstrate the standard numeric formats. |
| 761 | # |
| 762 | sub numeric_formats { |
| 763 | |
| 764 | my $worksheet = $workbook->add_worksheet('Numeric formats'); |
| 765 | |
| 766 | $worksheet->set_column(0, 4, 15); |
| 767 | $worksheet->set_column(5, 5, 45); |
| 768 | |
| 769 | $worksheet->write(0, 0, "Index", $heading); |
| 770 | $worksheet->write(0, 1, "Index", $heading); |
| 771 | $worksheet->write(0, 2, "Unformatted", $heading); |
| 772 | $worksheet->write(0, 3, "Formatted", $heading); |
| 773 | $worksheet->write(0, 4, "Negative", $heading); |
| 774 | $worksheet->write(0, 5, "Format", $heading); |
| 775 | |
| 776 | my @formats; |
| 777 | push @formats, [ 0x00, 1234.567, 0, 'General' ]; |
| 778 | push @formats, [ 0x01, 1234.567, 0, '0' ]; |
| 779 | push @formats, [ 0x02, 1234.567, 0, '0.00' ]; |
| 780 | push @formats, [ 0x03, 1234.567, 0, '#,##0' ]; |
| 781 | push @formats, [ 0x04, 1234.567, 0, '#,##0.00' ]; |
| 782 | push @formats, [ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ]; |
| 783 | push @formats, [ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ]; |
| 784 | push @formats, [ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ]; |
| 785 | push @formats, [ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ]; |
| 786 | push @formats, [ 0x09, 0.567, 0, '0%' ]; |
| 787 | push @formats, [ 0x0a, 0.567, 0, '0.00%' ]; |
| 788 | push @formats, [ 0x0b, 1234.567, 0, '0.00E+00' ]; |
| 789 | push @formats, [ 0x0c, 0.75, 0, '# ?/?' ]; |
| 790 | push @formats, [ 0x0d, 0.3125, 0, '# ??/??' ]; |
| 791 | push @formats, [ 0x0e, 36892.521, 0, 'm/d/yy' ]; |
| 792 | push @formats, [ 0x0f, 36892.521, 0, 'd-mmm-yy' ]; |
| 793 | push @formats, [ 0x10, 36892.521, 0, 'd-mmm' ]; |
| 794 | push @formats, [ 0x11, 36892.521, 0, 'mmm-yy' ]; |
| 795 | push @formats, [ 0x12, 36892.521, 0, 'h:mm AM/PM' ]; |
| 796 | push @formats, [ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ]; |
| 797 | push @formats, [ 0x14, 36892.521, 0, 'h:mm' ]; |
| 798 | push @formats, [ 0x15, 36892.521, 0, 'h:mm:ss' ]; |
| 799 | push @formats, [ 0x16, 36892.521, 0, 'm/d/yy h:mm' ]; |
| 800 | push @formats, [ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ]; |
| 801 | push @formats, [ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ]; |
| 802 | push @formats, [ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ]; |
| 803 | push @formats, [ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ]; |
| 804 | push @formats, [ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ]; |
| 805 | push @formats, [ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ]; |
| 806 | push @formats, [ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ]; |
| 807 | push @formats, [ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ]; |
| 808 | push @formats, [ 0x2d, 36892.521, 0, 'mm:ss' ]; |
| 809 | push @formats, [ 0x2e, 3.0153, 0, '[h]:mm:ss' ]; |
| 810 | push @formats, [ 0x2f, 36892.521, 0, 'mm:ss.0' ]; |
| 811 | push @formats, [ 0x30, 1234.567, 0, '##0.0E+0' ]; |
| 812 | push @formats, [ 0x31, 1234.567, 0, '@' ]; |
| 813 | |
| 814 | my $i; |
| 815 | foreach my $format (@formats){ |
| 816 | my $style = $workbook->add_format(); |
| 817 | $style->set_num_format($format->[0]); |
| 818 | |
| 819 | $i++; |
| 820 | $worksheet->write($i, 0, $format->[0], $center); |
| 821 | $worksheet->write($i, 1, sprintf("0x%02X", $format->[0]), $center); |
| 822 | $worksheet->write($i, 2, $format->[1], $center); |
| 823 | $worksheet->write($i, 3, $format->[1], $style); |
| 824 | |
| 825 | if ($format->[2]) { |
| 826 | $worksheet->write($i, 4, $format->[2], $style); |
| 827 | } |
| 828 | |
| 829 | $worksheet->write_string($i, 5, $format->[3]); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | |
| 834 | ###################################################################### |
| 835 | # |
| 836 | # Demonstrate the font options. |
| 837 | # |
| 838 | sub fonts { |
| 839 | |
| 840 | my $worksheet = $workbook->add_worksheet('Fonts'); |
| 841 | |
| 842 | $worksheet->set_column(0, 0, 30); |
| 843 | $worksheet->set_column(1, 1, 10); |
| 844 | |
| 845 | $worksheet->write(0, 0, "Font name", $heading); |
| 846 | $worksheet->write(0, 1, "Font size", $heading); |
| 847 | |
| 848 | my @fonts; |
| 849 | push @fonts, [ 10, 'Arial' ]; |
| 850 | push @fonts, [ 12, 'Arial' ]; |
| 851 | push @fonts, [ 14, 'Arial' ]; |
| 852 | push @fonts, [ 12, 'Arial Black' ]; |
| 853 | push @fonts, [ 12, 'Arial Narrow' ]; |
| 854 | push @fonts, [ 12, 'Century Schoolbook' ]; |
| 855 | push @fonts, [ 12, 'Courier' ]; |
| 856 | push @fonts, [ 12, 'Courier New' ]; |
| 857 | push @fonts, [ 12, 'Garamond' ]; |
| 858 | push @fonts, [ 12, 'Impact' ]; |
| 859 | push @fonts, [ 12, 'Lucida Handwriting'] ; |
| 860 | push @fonts, [ 12, 'Times New Roman' ]; |
| 861 | push @fonts, [ 12, 'Symbol' ]; |
| 862 | push @fonts, [ 12, 'Wingdings' ]; |
| 863 | push @fonts, [ 12, 'A font that doesn\'t exist' ]; |
| 864 | |
| 865 | my $i; |
| 866 | foreach my $font (@fonts){ |
| 867 | my $format = $workbook->add_format(); |
| 868 | |
| 869 | $format->set_size($font->[0]); |
| 870 | $format->set_font($font->[1]); |
| 871 | |
| 872 | $i++; |
| 873 | $worksheet->write($i, 0, $font->[1], $format); |
| 874 | $worksheet->write($i, 1, $font->[0], $format); |
| 875 | } |
| 876 | |
| 877 | } |
| 878 | |
| 879 | |
| 880 | ###################################################################### |
| 881 | # |
| 882 | # Demonstrate the standard Excel border styles. |
| 883 | # |
| 884 | sub borders { |
| 885 | |
| 886 | my $worksheet = $workbook->add_worksheet('Borders'); |
| 887 | |
| 888 | $worksheet->set_column(0, 4, 10); |
| 889 | $worksheet->set_column(5, 5, 40); |
| 890 | |
| 891 | $worksheet->write(0, 0, "Index", $heading); |
| 892 | $worksheet->write(0, 1, "Index", $heading); |
| 893 | $worksheet->write(0, 3, "Style", $heading); |
| 894 | $worksheet->write(0, 5, "The style is highlighted in red for ", $heading); |
| 895 | $worksheet->write(1, 5, "emphasis, the default color is black.", $heading); |
| 896 | |
| 897 | for my $i (0..13){ |
| 898 | my $format = $workbook->add_format(); |
| 899 | $format->set_border($i); |
| 900 | $format->set_border_color('red'); |
| 901 | $format->set_align('center'); |
| 902 | |
| 903 | $worksheet->write((2*($i+1)), 0, $i, $center); |
| 904 | $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center); |
| 905 | |
| 906 | $worksheet->write((2*($i+1)), 3, "Border", $format); |
| 907 | } |
| 908 | |
| 909 | $worksheet->write(30, 0, "Diag type", $heading); |
| 910 | $worksheet->write(30, 1, "Index", $heading); |
| 911 | $worksheet->write(30, 3, "Style", $heading); |
| 912 | $worksheet->write(30, 5, "Diagonal Boder styles", $heading); |
| 913 | |
| 914 | for my $i (1..3){ |
| 915 | my $format = $workbook->add_format(); |
| 916 | $format->set_diag_type($i); |
| 917 | $format->set_diag_border(1); |
| 918 | $format->set_diag_color('red'); |
| 919 | $format->set_align('center'); |
| 920 | |
| 921 | $worksheet->write((2*($i+15)), 0, $i, $center); |
| 922 | $worksheet->write((2*($i+15)), 1, sprintf("0x%02X", $i), $center); |
| 923 | |
| 924 | $worksheet->write((2*($i+15)), 3, "Border", $format); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | ###################################################################### |
| 931 | # |
| 932 | # Demonstrate the standard Excel cell patterns. |
| 933 | # |
| 934 | sub patterns { |
| 935 | |
| 936 | my $worksheet = $workbook->add_worksheet('Patterns'); |
| 937 | |
| 938 | $worksheet->set_column(0, 4, 10); |
| 939 | $worksheet->set_column(5, 5, 50); |
| 940 | |
| 941 | $worksheet->write(0, 0, "Index", $heading); |
| 942 | $worksheet->write(0, 1, "Index", $heading); |
| 943 | $worksheet->write(0, 3, "Pattern", $heading); |
| 944 | |
| 945 | $worksheet->write(0, 5, "The background colour has been set to silver.", $heading); |
| 946 | $worksheet->write(1, 5, "The foreground colour has been set to green.", $heading); |
| 947 | |
| 948 | for my $i (0..18){ |
| 949 | my $format = $workbook->add_format(); |
| 950 | |
| 951 | $format->set_pattern($i); |
| 952 | $format->set_bg_color('silver'); |
| 953 | $format->set_fg_color('green'); |
| 954 | $format->set_align('center'); |
| 955 | |
| 956 | $worksheet->write((2*($i+1)), 0, $i, $center); |
| 957 | $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center); |
| 958 | |
| 959 | $worksheet->write((2*($i+1)), 3, "Pattern", $format); |
| 960 | |
| 961 | if ($i == 1) { |
| 962 | $worksheet->write((2*($i+1)), 5, "This is solid colour, the most useful pattern.", $heading); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | |
| 968 | ###################################################################### |
| 969 | # |
| 970 | # Demonstrate the standard Excel cell alignments. |
| 971 | # |
| 972 | sub alignment { |
| 973 | |
| 974 | my $worksheet = $workbook->add_worksheet('Alignment'); |
| 975 | |
| 976 | $worksheet->set_column(0, 7, 12); |
| 977 | $worksheet->set_row(0, 40); |
| 978 | $worksheet->set_selection(7, 0); |
| 979 | |
| 980 | my $format01 = $workbook->add_format(); |
| 981 | my $format02 = $workbook->add_format(); |
| 982 | my $format03 = $workbook->add_format(); |
| 983 | my $format04 = $workbook->add_format(); |
| 984 | my $format05 = $workbook->add_format(); |
| 985 | my $format06 = $workbook->add_format(); |
| 986 | my $format07 = $workbook->add_format(); |
| 987 | my $format08 = $workbook->add_format(); |
| 988 | my $format09 = $workbook->add_format(); |
| 989 | my $format10 = $workbook->add_format(); |
| 990 | my $format11 = $workbook->add_format(); |
| 991 | my $format12 = $workbook->add_format(); |
| 992 | my $format13 = $workbook->add_format(); |
| 993 | my $format14 = $workbook->add_format(); |
| 994 | my $format15 = $workbook->add_format(); |
| 995 | my $format16 = $workbook->add_format(); |
| 996 | my $format17 = $workbook->add_format(); |
| 997 | |
| 998 | $format02->set_align('top'); |
| 999 | $format03->set_align('bottom'); |
| 1000 | $format04->set_align('vcenter'); |
| 1001 | $format05->set_align('vjustify'); |
| 1002 | $format06->set_text_wrap(); |
| 1003 | |
| 1004 | $format07->set_align('left'); |
| 1005 | $format08->set_align('right'); |
| 1006 | $format09->set_align('center'); |
| 1007 | $format10->set_align('fill'); |
| 1008 | $format11->set_align('justify'); |
| 1009 | $format12->set_merge(); |
| 1010 | |
| 1011 | $format13->set_rotation(45); |
| 1012 | $format14->set_rotation(-45); |
| 1013 | $format15->set_rotation(270); |
| 1014 | |
| 1015 | $format16->set_shrink(); |
| 1016 | $format17->set_indent(1); |
| 1017 | |
| 1018 | $worksheet->write(0, 0, 'Vertical', $heading); |
| 1019 | $worksheet->write(0, 1, 'top', $format02); |
| 1020 | $worksheet->write(0, 2, 'bottom', $format03); |
| 1021 | $worksheet->write(0, 3, 'vcenter', $format04); |
| 1022 | $worksheet->write(0, 4, 'vjustify', $format05); |
| 1023 | $worksheet->write(0, 5, "text\nwrap", $format06); |
| 1024 | |
| 1025 | $worksheet->write(2, 0, 'Horizontal', $heading); |
| 1026 | $worksheet->write(2, 1, 'left', $format07); |
| 1027 | $worksheet->write(2, 2, 'right', $format08); |
| 1028 | $worksheet->write(2, 3, 'center', $format09); |
| 1029 | $worksheet->write(2, 4, 'fill', $format10); |
| 1030 | $worksheet->write(2, 5, 'justify', $format11); |
| 1031 | |
| 1032 | $worksheet->write(3, 1, 'merge', $format12); |
| 1033 | $worksheet->write(3, 2, '', $format12); |
| 1034 | |
| 1035 | $worksheet->write(3, 3, 'Shrink ' x 3, $format16); |
| 1036 | $worksheet->write(3, 4, 'Indent', $format17); |
| 1037 | |
| 1038 | |
| 1039 | $worksheet->write(5, 0, 'Rotation', $heading); |
| 1040 | $worksheet->write(5, 1, 'Rotate 45', $format13); |
| 1041 | $worksheet->write(6, 1, 'Rotate -45', $format14); |
| 1042 | $worksheet->write(7, 1, 'Rotate 270', $format15); |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | ###################################################################### |
| 1047 | # |
| 1048 | # Demonstrate other miscellaneous features. |
| 1049 | # |
| 1050 | sub misc { |
| 1051 | |
| 1052 | my $worksheet = $workbook->add_worksheet('Miscellaneous'); |
| 1053 | |
| 1054 | $worksheet->set_column(2, 2, 25); |
| 1055 | |
| 1056 | my $format01 = $workbook->add_format(); |
| 1057 | my $format02 = $workbook->add_format(); |
| 1058 | my $format03 = $workbook->add_format(); |
| 1059 | my $format04 = $workbook->add_format(); |
| 1060 | my $format05 = $workbook->add_format(); |
| 1061 | my $format06 = $workbook->add_format(); |
| 1062 | my $format07 = $workbook->add_format(); |
| 1063 | |
| 1064 | $format01->set_underline(0x01); |
| 1065 | $format02->set_underline(0x02); |
| 1066 | $format03->set_underline(0x21); |
| 1067 | $format04->set_underline(0x22); |
| 1068 | $format05->set_font_strikeout(); |
| 1069 | $format06->set_font_outline(); |
| 1070 | $format07->set_font_shadow(); |
| 1071 | |
| 1072 | $worksheet->write(1, 2, 'Underline 0x01', $format01); |
| 1073 | $worksheet->write(3, 2, 'Underline 0x02', $format02); |
| 1074 | $worksheet->write(5, 2, 'Underline 0x21', $format03); |
| 1075 | $worksheet->write(7, 2, 'Underline 0x22', $format04); |
| 1076 | $worksheet->write(9, 2, 'Strikeout', $format05); |
| 1077 | $worksheet->write(11, 2, 'Outline (Macintosh only)', $format06); |
| 1078 | $worksheet->write(13, 2, 'Shadow (Macintosh only)', $format07); |
| 1079 | } |
| 1080 | |
| 1081 | __END__ |
| 1082 | |
| 1083 | |
| 1084 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/formats.pl> |
| 1085 | |
| 1086 | =head2 Example: bug_report.pl |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | A template for submitting a bug report. |
| 1091 | |
| 1092 | Run this program and read the output from the command line. |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | #!/usr/bin/perl -w |
| 1097 | |
| 1098 | |
| 1099 | ############################################################################### |
| 1100 | # |
| 1101 | # A template for submitting a bug report. |
| 1102 | # |
| 1103 | # Run this program and read the output from the command line. |
| 1104 | # |
| 1105 | # reverse('©'), March 2004, John McNamara, jmcnamara@cpan.org |
| 1106 | # |
| 1107 | |
| 1108 | |
| 1109 | use strict; |
| 1110 | |
| 1111 | print << 'HINTS_1'; |
| 1112 | |
| 1113 | REPORTING A BUG OR ASKING A QUESTION |
| 1114 | |
| 1115 | Feel free to report bugs or ask questions. However, to save time |
| 1116 | consider the following steps first: |
| 1117 | |
| 1118 | Read the documentation: |
| 1119 | |
| 1120 | The Spreadsheet::WriteExcel documentation has been refined in |
| 1121 | response to user questions. Therefore, if you have a question it is |
| 1122 | possible that someone else has asked it before you and that it is |
| 1123 | already addressed in the documentation. Since there is a lot of |
| 1124 | documentation to get through you should at least read the table of |
| 1125 | contents and search for keywords that you are interested in. |
| 1126 | |
| 1127 | Look at the example programs: |
| 1128 | |
| 1129 | There are over 70 example programs shipped with the standard |
| 1130 | Spreadsheet::WriteExcel distribution. Many of these were created in |
| 1131 | response to user questions. Try to identify an example program that |
| 1132 | corresponds to your query and adapt it to your needs. |
| 1133 | |
| 1134 | HINTS_1 |
| 1135 | print "Press enter ..."; <STDIN>; |
| 1136 | |
| 1137 | print << 'HINTS_2'; |
| 1138 | |
| 1139 | If you submit a bug report here are some pointers. |
| 1140 | |
| 1141 | 1. Put "WriteExcel:" at the beginning of the subject line. This helps |
| 1142 | to filter genuine messages from spam. |
| 1143 | |
| 1144 | 2. Describe the problems as clearly and as concisely as possible. |
| 1145 | |
| 1146 | 3. Send a sample program. It is often easier to describe a problem in |
| 1147 | code than in written prose. |
| 1148 | |
| 1149 | 4. The sample program should be as small as possible to demonstrate the |
| 1150 | problem. Don't copy and past large sections of your program. The |
| 1151 | program should also be self contained and working. |
| 1152 | |
| 1153 | A sample bug report is generated below. If you use this format then it |
| 1154 | will help to analyse your question and respond to it more quickly. |
| 1155 | |
| 1156 | Please don't send patches without contacting the author first. |
| 1157 | |
| 1158 | |
| 1159 | HINTS_2 |
| 1160 | print "Press enter ..."; <STDIN>; |
| 1161 | |
| 1162 | |
| 1163 | print << 'EMAIL'; |
| 1164 | |
| 1165 | ======================================================================= |
| 1166 | |
| 1167 | To: John McNamara <jmcnamara@cpan.org> |
| 1168 | Subject: WriteExcel: Problem with something. |
| 1169 | |
| 1170 | Hi John, |
| 1171 | |
| 1172 | I am using Spreadsheet::WriteExcel and I have encountered a problem. I |
| 1173 | want it to do SOMETHING but the module appears to do SOMETHING_ELSE. |
| 1174 | |
| 1175 | Here is some code that demonstrates the problem. |
| 1176 | |
| 1177 | #!/usr/bin/perl -w |
| 1178 | |
| 1179 | use strict; |
| 1180 | use Spreadsheet::WriteExcel; |
| 1181 | |
| 1182 | my $workbook = Spreadsheet::WriteExcel->new("reload.xls"); |
| 1183 | my $worksheet = $workbook->add_worksheet(); |
| 1184 | |
| 1185 | $worksheet->write(0, 0, "Hi Excel!"); |
| 1186 | |
| 1187 | __END__ |
| 1188 | |
| 1189 | |
| 1190 | I tested using Excel XX (or Gnumeric or OpenOffice.org). |
| 1191 | |
| 1192 | My automatically generated system details are as follows: |
| 1193 | EMAIL |
| 1194 | |
| 1195 | |
| 1196 | print "\n Perl version : $]"; |
| 1197 | print "\n OS name : $^O"; |
| 1198 | print "\n Module versions: (not all are required)\n"; |
| 1199 | |
| 1200 | |
| 1201 | my @modules = qw( |
| 1202 | Spreadsheet::WriteExcel |
| 1203 | Spreadsheet::ParseExcel |
| 1204 | OLE::Storage_Lite |
| 1205 | Parse::RecDescent |
| 1206 | File::Temp |
| 1207 | Digest::MD4 |
| 1208 | Digest::Perl::MD4 |
| 1209 | Digest::MD5 |
| 1210 | ); |
| 1211 | |
| 1212 | |
| 1213 | for my $module (@modules) { |
| 1214 | my $version; |
| 1215 | eval "require $module"; |
| 1216 | |
| 1217 | if (not $@) { |
| 1218 | $version = $module->VERSION; |
| 1219 | $version = '(unknown)' if not defined $version; |
| 1220 | } |
| 1221 | else { |
| 1222 | $version = '(not installed)'; |
| 1223 | } |
| 1224 | |
| 1225 | printf "%21s%-24s\t%s\n", "", $module, $version; |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | print << "BYE"; |
| 1230 | Yours etc., |
| 1231 | |
| 1232 | A. Person |
| 1233 | -- |
| 1234 | |
| 1235 | BYE |
| 1236 | |
| 1237 | __END__ |
| 1238 | |
| 1239 | |
| 1240 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/bug_report.pl> |
| 1241 | |
| 1242 | =head2 Example: autofilter.pl |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | An example of how to create autofilters with Spreadsheet::WriteExcel. |
| 1247 | |
| 1248 | An autofilter is a way of adding drop down lists to the headers of a 2D range |
| 1249 | of worksheet data. This is turn allow users to filter the data based on |
| 1250 | simple criteria so that some data is shown and some is hidden. |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | =begin html |
| 1255 | |
| 1256 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/autofilter.jpg" width="640" height="420" alt="Output from autofilter.pl" /></center></p> |
| 1257 | |
| 1258 | =end html |
| 1259 | |
| 1260 | Source code for this example: |
| 1261 | |
| 1262 | #!/usr/bin/perl -w |
| 1263 | |
| 1264 | ############################################################################### |
| 1265 | # |
| 1266 | # An example of how to create autofilters with Spreadsheet::WriteExcel. |
| 1267 | # |
| 1268 | # An autofilter is a way of adding drop down lists to the headers of a 2D range |
| 1269 | # of worksheet data. This is turn allow users to filter the data based on |
| 1270 | # simple criteria so that some data is shown and some is hidden. |
| 1271 | # |
| 1272 | # reverse('©'), September 2007, John McNamara, jmcnamara@cpan.org |
| 1273 | # |
| 1274 | |
| 1275 | use strict; |
| 1276 | use Spreadsheet::WriteExcel; |
| 1277 | |
| 1278 | my $workbook = Spreadsheet::WriteExcel->new('autofilter.xls'); |
| 1279 | |
| 1280 | die "Couldn't create new Excel file: $!.\n" unless defined $workbook; |
| 1281 | |
| 1282 | my $worksheet1 = $workbook->add_worksheet(); |
| 1283 | my $worksheet2 = $workbook->add_worksheet(); |
| 1284 | my $worksheet3 = $workbook->add_worksheet(); |
| 1285 | my $worksheet4 = $workbook->add_worksheet(); |
| 1286 | my $worksheet5 = $workbook->add_worksheet(); |
| 1287 | my $worksheet6 = $workbook->add_worksheet(); |
| 1288 | |
| 1289 | my $bold = $workbook->add_format(bold => 1); |
| 1290 | |
| 1291 | |
| 1292 | # Extract the data embedded at the end of this file. |
| 1293 | my @headings = split ' ', <DATA>; |
| 1294 | my @data; |
| 1295 | push @data, [split] while <DATA>; |
| 1296 | |
| 1297 | |
| 1298 | # Set up several sheets with the same data. |
| 1299 | for my $worksheet ($workbook->sheets()) { |
| 1300 | $worksheet->set_column('A:D', 12); |
| 1301 | $worksheet->set_row(0, 20, $bold); |
| 1302 | $worksheet->write('A1', \@headings); |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | ############################################################################### |
| 1307 | # |
| 1308 | # Example 1. Autofilter without conditions. |
| 1309 | # |
| 1310 | |
| 1311 | $worksheet1->autofilter('A1:D51'); |
| 1312 | $worksheet1->write('A2', [[@data]]); |
| 1313 | |
| 1314 | |
| 1315 | ############################################################################### |
| 1316 | # |
| 1317 | # |
| 1318 | # Example 2. Autofilter with a filter condition in the first column. |
| 1319 | # |
| 1320 | |
| 1321 | # The range in this example is the same as above but in row-column notation. |
| 1322 | $worksheet2->autofilter(0, 0, 50, 3); |
| 1323 | |
| 1324 | # The placeholder "Region" in the filter is ignored and can be any string |
| 1325 | # that adds clarity to the expression. |
| 1326 | # |
| 1327 | $worksheet2->filter_column(0, 'Region eq East'); |
| 1328 | |
| 1329 | # |
| 1330 | # Hide the rows that don't match the filter criteria. |
| 1331 | # |
| 1332 | my $row = 1; |
| 1333 | |
| 1334 | for my $row_data (@data) { |
| 1335 | my $region = $row_data->[0]; |
| 1336 | |
| 1337 | if ($region eq 'East') { |
| 1338 | # Row is visible. |
| 1339 | } |
| 1340 | else { |
| 1341 | # Hide row. |
| 1342 | $worksheet2->set_row($row, undef, undef, 1); |
| 1343 | } |
| 1344 | |
| 1345 | $worksheet2->write($row++, 0, $row_data); |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | ############################################################################### |
| 1350 | # |
| 1351 | # |
| 1352 | # Example 3. Autofilter with a dual filter condition in one of the columns. |
| 1353 | # |
| 1354 | |
| 1355 | $worksheet3->autofilter('A1:D51'); |
| 1356 | |
| 1357 | $worksheet3->filter_column('A', 'x eq East or x eq South'); |
| 1358 | |
| 1359 | # |
| 1360 | # Hide the rows that don't match the filter criteria. |
| 1361 | # |
| 1362 | $row = 1; |
| 1363 | |
| 1364 | for my $row_data (@data) { |
| 1365 | my $region = $row_data->[0]; |
| 1366 | |
| 1367 | if ($region eq 'East' or $region eq 'South') { |
| 1368 | # Row is visible. |
| 1369 | } |
| 1370 | else { |
| 1371 | # Hide row. |
| 1372 | $worksheet3->set_row($row, undef, undef, 1); |
| 1373 | } |
| 1374 | |
| 1375 | $worksheet3->write($row++, 0, $row_data); |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | ############################################################################### |
| 1380 | # |
| 1381 | # |
| 1382 | # Example 4. Autofilter with filter conditions in two columns. |
| 1383 | # |
| 1384 | |
| 1385 | $worksheet4->autofilter('A1:D51'); |
| 1386 | |
| 1387 | $worksheet4->filter_column('A', 'x eq East'); |
| 1388 | $worksheet4->filter_column('C', 'x > 3000 and x < 8000' ); |
| 1389 | |
| 1390 | # |
| 1391 | # Hide the rows that don't match the filter criteria. |
| 1392 | # |
| 1393 | $row = 1; |
| 1394 | |
| 1395 | for my $row_data (@data) { |
| 1396 | my $region = $row_data->[0]; |
| 1397 | my $volume = $row_data->[2]; |
| 1398 | |
| 1399 | if ($region eq 'East' and |
| 1400 | $volume > 3000 and $volume < 8000 |
| 1401 | ) |
| 1402 | { |
| 1403 | # Row is visible. |
| 1404 | } |
| 1405 | else { |
| 1406 | # Hide row. |
| 1407 | $worksheet4->set_row($row, undef, undef, 1); |
| 1408 | } |
| 1409 | |
| 1410 | $worksheet4->write($row++, 0, $row_data); |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | ############################################################################### |
| 1415 | # |
| 1416 | # |
| 1417 | # Example 5. Autofilter with filter for blanks. |
| 1418 | # |
| 1419 | |
| 1420 | # Create a blank cell in our test data. |
| 1421 | $data[5]->[0] = ''; |
| 1422 | |
| 1423 | |
| 1424 | $worksheet5->autofilter('A1:D51'); |
| 1425 | $worksheet5->filter_column('A', 'x == Blanks'); |
| 1426 | |
| 1427 | # |
| 1428 | # Hide the rows that don't match the filter criteria. |
| 1429 | # |
| 1430 | $row = 1; |
| 1431 | |
| 1432 | for my $row_data (@data) { |
| 1433 | my $region = $row_data->[0]; |
| 1434 | |
| 1435 | if ($region eq '') |
| 1436 | { |
| 1437 | # Row is visible. |
| 1438 | } |
| 1439 | else { |
| 1440 | # Hide row. |
| 1441 | $worksheet5->set_row($row, undef, undef, 1); |
| 1442 | } |
| 1443 | |
| 1444 | $worksheet5->write($row++, 0, $row_data); |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | ############################################################################### |
| 1449 | # |
| 1450 | # |
| 1451 | # Example 6. Autofilter with filter for non-blanks. |
| 1452 | # |
| 1453 | |
| 1454 | |
| 1455 | $worksheet6->autofilter('A1:D51'); |
| 1456 | $worksheet6->filter_column('A', 'x == NonBlanks'); |
| 1457 | |
| 1458 | # |
| 1459 | # Hide the rows that don't match the filter criteria. |
| 1460 | # |
| 1461 | $row = 1; |
| 1462 | |
| 1463 | for my $row_data (@data) { |
| 1464 | my $region = $row_data->[0]; |
| 1465 | |
| 1466 | if ($region ne '') |
| 1467 | { |
| 1468 | # Row is visible. |
| 1469 | } |
| 1470 | else { |
| 1471 | # Hide row. |
| 1472 | $worksheet6->set_row($row, undef, undef, 1); |
| 1473 | } |
| 1474 | |
| 1475 | $worksheet6->write($row++, 0, $row_data); |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | __DATA__ |
| 1481 | Region Item Volume Month |
| 1482 | East Apple 9000 July |
| 1483 | East Apple 5000 July |
| 1484 | South Orange 9000 September |
| 1485 | North Apple 2000 November |
| 1486 | West Apple 9000 November |
| 1487 | South Pear 7000 October |
| 1488 | North Pear 9000 August |
| 1489 | West Orange 1000 December |
| 1490 | West Grape 1000 November |
| 1491 | South Pear 10000 April |
| 1492 | West Grape 6000 January |
| 1493 | South Orange 3000 May |
| 1494 | North Apple 3000 December |
| 1495 | South Apple 7000 February |
| 1496 | West Grape 1000 December |
| 1497 | East Grape 8000 February |
| 1498 | South Grape 10000 June |
| 1499 | West Pear 7000 December |
| 1500 | South Apple 2000 October |
| 1501 | East Grape 7000 December |
| 1502 | North Grape 6000 April |
| 1503 | East Pear 8000 February |
| 1504 | North Apple 7000 August |
| 1505 | North Orange 7000 July |
| 1506 | North Apple 6000 June |
| 1507 | South Grape 8000 September |
| 1508 | West Apple 3000 October |
| 1509 | South Orange 10000 November |
| 1510 | West Grape 4000 July |
| 1511 | North Orange 5000 August |
| 1512 | East Orange 1000 November |
| 1513 | East Orange 4000 October |
| 1514 | North Grape 5000 August |
| 1515 | East Apple 1000 December |
| 1516 | South Apple 10000 March |
| 1517 | East Grape 7000 October |
| 1518 | West Grape 1000 September |
| 1519 | East Grape 10000 October |
| 1520 | South Orange 8000 March |
| 1521 | North Apple 4000 July |
| 1522 | South Orange 5000 July |
| 1523 | West Apple 4000 June |
| 1524 | East Apple 5000 April |
| 1525 | North Pear 3000 August |
| 1526 | East Grape 9000 November |
| 1527 | North Orange 8000 October |
| 1528 | East Apple 10000 June |
| 1529 | South Pear 1000 December |
| 1530 | North Grape 10000 July |
| 1531 | East Grape 6000 February |
| 1532 | |
| 1533 | |
| 1534 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/autofilter.pl> |
| 1535 | |
| 1536 | =head2 Example: autofit.pl |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | Simulate Excel's autofit for column widths. |
| 1541 | |
| 1542 | Excel provides a function called Autofit (Format->Columns->Autofit) that |
| 1543 | adjusts column widths to match the length of the longest string in a column. |
| 1544 | Excel calculates these widths at run time when it has access to information |
| 1545 | about string lengths and font information. This function is *not* a feature |
| 1546 | of the file format and thus cannot be implemented by Spreadsheet::WriteExcel. |
| 1547 | |
| 1548 | However, we can make an attempt to simulate it by keeping track of the |
| 1549 | longest string written to each column and then adjusting the column widths |
| 1550 | prior to closing the file. |
| 1551 | |
| 1552 | We keep track of the longest strings by adding a handler to the write() |
| 1553 | function. See add_handler() in the S::WE docs for more information. |
| 1554 | |
| 1555 | The main problem with trying to simulate Autofit lies in defining a |
| 1556 | relationship between a string length and its width in a arbitrary font and |
| 1557 | size. We use two approaches below. The first is a simple direct relationship |
| 1558 | obtained by trial and error. The second is a slightly more sophisticated |
| 1559 | method using an external module. For more complicated applications you will |
| 1560 | probably have to work out your own methods. |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | =begin html |
| 1565 | |
| 1566 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/autofit.jpg" width="640" height="420" alt="Output from autofit.pl" /></center></p> |
| 1567 | |
| 1568 | =end html |
| 1569 | |
| 1570 | Source code for this example: |
| 1571 | |
| 1572 | #!/usr/bin/perl -w |
| 1573 | |
| 1574 | ############################################################################## |
| 1575 | # |
| 1576 | # Simulate Excel's autofit for column widths. |
| 1577 | # |
| 1578 | # Excel provides a function called Autofit (Format->Columns->Autofit) that |
| 1579 | # adjusts column widths to match the length of the longest string in a column. |
| 1580 | # Excel calculates these widths at run time when it has access to information |
| 1581 | # about string lengths and font information. This function is *not* a feature |
| 1582 | # of the file format and thus cannot be implemented by Spreadsheet::WriteExcel. |
| 1583 | # |
| 1584 | # However, we can make an attempt to simulate it by keeping track of the |
| 1585 | # longest string written to each column and then adjusting the column widths |
| 1586 | # prior to closing the file. |
| 1587 | # |
| 1588 | # We keep track of the longest strings by adding a handler to the write() |
| 1589 | # function. See add_handler() in the S::WE docs for more information. |
| 1590 | # |
| 1591 | # The main problem with trying to simulate Autofit lies in defining a |
| 1592 | # relationship between a string length and its width in a arbitrary font and |
| 1593 | # size. We use two approaches below. The first is a simple direct relationship |
| 1594 | # obtained by trial and error. The second is a slightly more sophisticated |
| 1595 | # method using an external module. For more complicated applications you will |
| 1596 | # probably have to work out your own methods. |
| 1597 | # |
| 1598 | # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org |
| 1599 | # |
| 1600 | |
| 1601 | use strict; |
| 1602 | use Spreadsheet::WriteExcel; |
| 1603 | |
| 1604 | my $workbook = Spreadsheet::WriteExcel->new('autofit.xls'); |
| 1605 | my $worksheet = $workbook->add_worksheet(); |
| 1606 | |
| 1607 | |
| 1608 | ############################################################################### |
| 1609 | # |
| 1610 | # Add a handler to store the width of the longest string written to a column. |
| 1611 | # We use the stored width to simulate an autofit of the column widths. |
| 1612 | # |
| 1613 | # You should do this for every worksheet you want to autofit. |
| 1614 | # |
| 1615 | $worksheet->add_write_handler(qr[\w], \&store_string_widths); |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | $worksheet->write('A1', 'Hello'); |
| 1620 | $worksheet->write('B1', 'Hello World'); |
| 1621 | $worksheet->write('D1', 'Hello'); |
| 1622 | $worksheet->write('F1', 'This is a long string as an example.'); |
| 1623 | |
| 1624 | # Run the autofit after you have finished writing strings to the workbook. |
| 1625 | autofit_columns($worksheet); |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | ############################################################################### |
| 1630 | # |
| 1631 | # Functions used for Autofit. |
| 1632 | # |
| 1633 | ############################################################################### |
| 1634 | |
| 1635 | ############################################################################### |
| 1636 | # |
| 1637 | # Adjust the column widths to fit the longest string in the column. |
| 1638 | # |
| 1639 | sub autofit_columns { |
| 1640 | |
| 1641 | my $worksheet = shift; |
| 1642 | my $col = 0; |
| 1643 | |
| 1644 | for my $width (@{$worksheet->{__col_widths}}) { |
| 1645 | |
| 1646 | $worksheet->set_column($col, $col, $width) if $width; |
| 1647 | $col++; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | ############################################################################### |
| 1653 | # |
| 1654 | # The following function is a callback that was added via add_write_handler() |
| 1655 | # above. It modifies the write() function so that it stores the maximum |
| 1656 | # unwrapped width of a string in a column. |
| 1657 | # |
| 1658 | sub store_string_widths { |
| 1659 | |
| 1660 | my $worksheet = shift; |
| 1661 | my $col = $_[1]; |
| 1662 | my $token = $_[2]; |
| 1663 | |
| 1664 | # Ignore some tokens that we aren't interested in. |
| 1665 | return if not defined $token; # Ignore undefs. |
| 1666 | return if $token eq ''; # Ignore blank cells. |
| 1667 | return if ref $token eq 'ARRAY'; # Ignore array refs. |
| 1668 | return if $token =~ /^=/; # Ignore formula |
| 1669 | |
| 1670 | # Ignore numbers |
| 1671 | return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; |
| 1672 | |
| 1673 | # Ignore various internal and external hyperlinks. In a real scenario |
| 1674 | # you may wish to track the length of the optional strings used with |
| 1675 | # urls. |
| 1676 | return if $token =~ m{^[fh]tt?ps?://}; |
| 1677 | return if $token =~ m{^mailto:}; |
| 1678 | return if $token =~ m{^(?:in|ex)ternal:}; |
| 1679 | |
| 1680 | |
| 1681 | # We store the string width as data in the Worksheet object. We use |
| 1682 | # a double underscore key name to avoid conflicts with future names. |
| 1683 | # |
| 1684 | my $old_width = $worksheet->{__col_widths}->[$col]; |
| 1685 | my $string_width = string_width($token); |
| 1686 | |
| 1687 | if (not defined $old_width or $string_width > $old_width) { |
| 1688 | # You may wish to set a minimum column width as follows. |
| 1689 | #return undef if $string_width < 10; |
| 1690 | |
| 1691 | $worksheet->{__col_widths}->[$col] = $string_width; |
| 1692 | } |
| 1693 | |
| 1694 | |
| 1695 | # Return control to write(); |
| 1696 | return undef; |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | ############################################################################### |
| 1701 | # |
| 1702 | # Very simple conversion between string length and string width for Arial 10. |
| 1703 | # See below for a more sophisticated method. |
| 1704 | # |
| 1705 | sub string_width { |
| 1706 | |
| 1707 | return 0.9 * length $_[0]; |
| 1708 | } |
| 1709 | |
| 1710 | __END__ |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | ############################################################################### |
| 1715 | # |
| 1716 | # This function uses an external module to get a more accurate width for a |
| 1717 | # string. Note that in a real program you could "use" the module instead of |
| 1718 | # "require"-ing it and you could make the Font object global to avoid repeated |
| 1719 | # initialisation. |
| 1720 | # |
| 1721 | # Note also that the $pixel_width to $cell_width is specific to Arial. For |
| 1722 | # other fonts you should calculate appropriate relationships. A future version |
| 1723 | # of S::WE will provide a way of specifying column widths in pixels instead of |
| 1724 | # cell units in order to simplify this conversion. |
| 1725 | # |
| 1726 | sub string_width { |
| 1727 | |
| 1728 | require Font::TTFMetrics; |
| 1729 | |
| 1730 | my $arial = Font::TTFMetrics->new('c:\windows\fonts\arial.ttf'); |
| 1731 | |
| 1732 | my $font_size = 10; |
| 1733 | my $dpi = 96; |
| 1734 | my $units_per_em = $arial->get_units_per_em(); |
| 1735 | my $font_width = $arial->string_width($_[0]); |
| 1736 | |
| 1737 | # Convert to pixels as per TTFMetrics docs. |
| 1738 | my $pixel_width = 6 + $font_width *$font_size *$dpi /(72 *$units_per_em); |
| 1739 | |
| 1740 | # Add extra pixels for border around text. |
| 1741 | $pixel_width += 6; |
| 1742 | |
| 1743 | # Convert to cell width (for Arial) and for cell widths > 1. |
| 1744 | my $cell_width = ($pixel_width -5) /7; |
| 1745 | |
| 1746 | return $cell_width; |
| 1747 | |
| 1748 | } |
| 1749 | |
| 1750 | __END__ |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/autofit.pl> |
| 1755 | |
| 1756 | =head2 Example: bigfile.pl |
| 1757 | |
| 1758 | |
| 1759 | |
| 1760 | Example of creating a Spreadsheet::WriteExcel that is larger than the |
| 1761 | default 7MB limit. |
| 1762 | |
| 1763 | This is exactly that same as any other Spreadsheet::WriteExcel program except |
| 1764 | that is requires that the OLE::Storage module is installed. |
| 1765 | |
| 1766 | |
| 1767 | =begin html |
| 1768 | |
| 1769 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/bigfile.jpg" width="640" height="420" alt="Output from bigfile.pl" /></center></p> |
| 1770 | |
| 1771 | =end html |
| 1772 | |
| 1773 | Source code for this example: |
| 1774 | |
| 1775 | #!/usr/bin/perl -w |
| 1776 | |
| 1777 | ############################################################################### |
| 1778 | # |
| 1779 | # Example of creating a Spreadsheet::WriteExcel that is larger than the |
| 1780 | # default 7MB limit. |
| 1781 | # |
| 1782 | # This is exactly that same as any other Spreadsheet::WriteExcel program except |
| 1783 | # that is requires that the OLE::Storage module is installed. |
| 1784 | # |
| 1785 | # reverse('©'), Jan 2007, John McNamara, jmcnamara@cpan.org |
| 1786 | |
| 1787 | |
| 1788 | use strict; |
| 1789 | use Spreadsheet::WriteExcel; |
| 1790 | |
| 1791 | |
| 1792 | my $workbook = Spreadsheet::WriteExcel->new('bigfile.xls'); |
| 1793 | my $worksheet = $workbook->add_worksheet(); |
| 1794 | |
| 1795 | $worksheet->set_column(0, 50, 18); |
| 1796 | |
| 1797 | for my $col (0 .. 50) { |
| 1798 | for my $row (0 .. 6000) { |
| 1799 | $worksheet->write($row, $col, "Row: $row Col: $col"); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | __END__ |
| 1804 | |
| 1805 | |
| 1806 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/bigfile.pl> |
| 1807 | |
| 1808 | =head2 Example: cgi.pl |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 1813 | file to a browser in a CGI program. |
| 1814 | |
| 1815 | On Windows the hash-bang line should be something like: |
| 1816 | |
| 1817 | #!C:\Perl\bin\perl.exe |
| 1818 | |
| 1819 | The "Content-Disposition" line will cause a prompt to be generated to save |
| 1820 | the file. If you want to stream the file to the browser instead, comment out |
| 1821 | that line as shown below. |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | #!/usr/bin/perl -w |
| 1826 | |
| 1827 | ############################################################################### |
| 1828 | # |
| 1829 | # Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 1830 | # file to a browser in a CGI program. |
| 1831 | # |
| 1832 | # On Windows the hash-bang line should be something like: |
| 1833 | # |
| 1834 | # #!C:\Perl\bin\perl.exe |
| 1835 | # |
| 1836 | # The "Content-Disposition" line will cause a prompt to be generated to save |
| 1837 | # the file. If you want to stream the file to the browser instead, comment out |
| 1838 | # that line as shown below. |
| 1839 | # |
| 1840 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 1841 | # |
| 1842 | |
| 1843 | use strict; |
| 1844 | use Spreadsheet::WriteExcel; |
| 1845 | |
| 1846 | # Set the filename and send the content type |
| 1847 | my $filename ="cgitest.xls"; |
| 1848 | |
| 1849 | print "Content-type: application/vnd.ms-excel\n"; |
| 1850 | # The Content-Disposition will generate a prompt to save the file. If you want |
| 1851 | # to stream the file to the browser, comment out the following line. |
| 1852 | print "Content-Disposition: attachment; filename=$filename\n"; |
| 1853 | print "\n"; |
| 1854 | |
| 1855 | # Create a new workbook and add a worksheet. The special Perl filehandle - will |
| 1856 | # redirect the output to STDOUT |
| 1857 | # |
| 1858 | my $workbook = Spreadsheet::WriteExcel->new("-"); |
| 1859 | my $worksheet = $workbook->add_worksheet(); |
| 1860 | |
| 1861 | |
| 1862 | # Set the column width for column 1 |
| 1863 | $worksheet->set_column(0, 0, 20); |
| 1864 | |
| 1865 | |
| 1866 | # Create a format |
| 1867 | my $format = $workbook->add_format(); |
| 1868 | $format->set_bold(); |
| 1869 | $format->set_size(15); |
| 1870 | $format->set_color('blue'); |
| 1871 | |
| 1872 | |
| 1873 | # Write to the workbook |
| 1874 | $worksheet->write(0, 0, "Hi Excel!", $format); |
| 1875 | |
| 1876 | __END__ |
| 1877 | |
| 1878 | |
| 1879 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/cgi.pl> |
| 1880 | |
| 1881 | =head2 Example: chart_area.pl |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | A simple demo of Area charts in Spreadsheet::WriteExcel. |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | =begin html |
| 1890 | |
| 1891 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_area.jpg" width="640" height="420" alt="Output from chart_area.pl" /></center></p> |
| 1892 | |
| 1893 | =end html |
| 1894 | |
| 1895 | Source code for this example: |
| 1896 | |
| 1897 | #!/usr/bin/perl -w |
| 1898 | |
| 1899 | ############################################################################### |
| 1900 | # |
| 1901 | # A simple demo of Area charts in Spreadsheet::WriteExcel. |
| 1902 | # |
| 1903 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 1904 | # |
| 1905 | |
| 1906 | use strict; |
| 1907 | use Spreadsheet::WriteExcel; |
| 1908 | |
| 1909 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_area.xls' ); |
| 1910 | my $worksheet = $workbook->add_worksheet(); |
| 1911 | my $bold = $workbook->add_format( bold => 1 ); |
| 1912 | |
| 1913 | # Add the worksheet data that the charts will refer to. |
| 1914 | my $headings = [ 'Category', 'Values 1', 'Values 2' ]; |
| 1915 | my $data = [ |
| 1916 | [ 2, 3, 4, 5, 6, 7 ], |
| 1917 | [ 1, 4, 5, 2, 1, 5 ], |
| 1918 | [ 3, 6, 7, 5, 4, 3 ], |
| 1919 | ]; |
| 1920 | |
| 1921 | $worksheet->write( 'A1', $headings, $bold ); |
| 1922 | $worksheet->write( 'A2', $data ); |
| 1923 | |
| 1924 | |
| 1925 | ############################################################################### |
| 1926 | # |
| 1927 | # Example 1. A minimal chart. |
| 1928 | # |
| 1929 | my $chart1 = $workbook->add_chart( type => 'area' ); |
| 1930 | |
| 1931 | # Add values only. Use the default categories. |
| 1932 | $chart1->add_series( values => '=Sheet1!$B$2:$B$7' ); |
| 1933 | |
| 1934 | |
| 1935 | ############################################################################### |
| 1936 | # |
| 1937 | # Example 2. A minimal chart with user specified categories (X axis) |
| 1938 | # and a series name. |
| 1939 | # |
| 1940 | my $chart2 = $workbook->add_chart( type => 'area' ); |
| 1941 | |
| 1942 | # Configure the series. |
| 1943 | $chart2->add_series( |
| 1944 | categories => '=Sheet1!$A$2:$A$7', |
| 1945 | values => '=Sheet1!$B$2:$B$7', |
| 1946 | name => 'Test data series 1', |
| 1947 | ); |
| 1948 | |
| 1949 | |
| 1950 | ############################################################################### |
| 1951 | # |
| 1952 | # Example 3. Same as previous chart but with added title and axes labels. |
| 1953 | # |
| 1954 | my $chart3 = $workbook->add_chart( type => 'area' ); |
| 1955 | |
| 1956 | # Configure the series. |
| 1957 | $chart3->add_series( |
| 1958 | categories => '=Sheet1!$A$2:$A$7', |
| 1959 | values => '=Sheet1!$B$2:$B$7', |
| 1960 | name => 'Test data series 1', |
| 1961 | ); |
| 1962 | |
| 1963 | # Add some labels. |
| 1964 | $chart3->set_title( name => 'Results of sample analysis' ); |
| 1965 | $chart3->set_x_axis( name => 'Sample number' ); |
| 1966 | $chart3->set_y_axis( name => 'Sample length (cm)' ); |
| 1967 | |
| 1968 | |
| 1969 | ############################################################################### |
| 1970 | # |
| 1971 | # Example 4. Same as previous chart but with an added series and with a |
| 1972 | # user specified chart sheet name. |
| 1973 | # |
| 1974 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'area' ); |
| 1975 | |
| 1976 | # Configure the series. |
| 1977 | $chart4->add_series( |
| 1978 | categories => '=Sheet1!$A$2:$A$7', |
| 1979 | values => '=Sheet1!$B$2:$B$7', |
| 1980 | name => 'Test data series 1', |
| 1981 | ); |
| 1982 | |
| 1983 | # Add another series. |
| 1984 | $chart4->add_series( |
| 1985 | categories => '=Sheet1!$A$2:$A$7', |
| 1986 | values => '=Sheet1!$C$2:$C$7', |
| 1987 | name => 'Test data series 2', |
| 1988 | ); |
| 1989 | |
| 1990 | # Add some labels. |
| 1991 | $chart4->set_title( name => 'Results of sample analysis' ); |
| 1992 | $chart4->set_x_axis( name => 'Sample number' ); |
| 1993 | $chart4->set_y_axis( name => 'Sample length (cm)' ); |
| 1994 | |
| 1995 | |
| 1996 | ############################################################################### |
| 1997 | # |
| 1998 | # Example 5. Same as Example 3 but as an embedded chart. |
| 1999 | # |
| 2000 | my $chart5 = $workbook->add_chart( type => 'area', embedded => 1 ); |
| 2001 | |
| 2002 | # Configure the series. |
| 2003 | $chart5->add_series( |
| 2004 | categories => '=Sheet1!$A$2:$A$7', |
| 2005 | values => '=Sheet1!$B$2:$B$7', |
| 2006 | name => 'Test data series 1', |
| 2007 | ); |
| 2008 | |
| 2009 | # Add some labels. |
| 2010 | $chart5->set_title( name => 'Results of sample analysis' ); |
| 2011 | $chart5->set_x_axis( name => 'Sample number' ); |
| 2012 | $chart5->set_y_axis( name => 'Sample length (cm)' ); |
| 2013 | |
| 2014 | # Insert the chart into the main worksheet. |
| 2015 | $worksheet->insert_chart( 'E2', $chart5 ); |
| 2016 | |
| 2017 | __END__ |
| 2018 | |
| 2019 | |
| 2020 | |
| 2021 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_area.pl> |
| 2022 | |
| 2023 | =head2 Example: chart_bar.pl |
| 2024 | |
| 2025 | |
| 2026 | |
| 2027 | A simple demo of Bar charts in Spreadsheet::WriteExcel. |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | =begin html |
| 2032 | |
| 2033 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_bar.jpg" width="640" height="420" alt="Output from chart_bar.pl" /></center></p> |
| 2034 | |
| 2035 | =end html |
| 2036 | |
| 2037 | Source code for this example: |
| 2038 | |
| 2039 | #!/usr/bin/perl -w |
| 2040 | |
| 2041 | ############################################################################### |
| 2042 | # |
| 2043 | # A simple demo of Bar charts in Spreadsheet::WriteExcel. |
| 2044 | # |
| 2045 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 2046 | # |
| 2047 | |
| 2048 | use strict; |
| 2049 | use Spreadsheet::WriteExcel; |
| 2050 | |
| 2051 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_bar.xls' ); |
| 2052 | my $worksheet = $workbook->add_worksheet(); |
| 2053 | my $bold = $workbook->add_format( bold => 1 ); |
| 2054 | |
| 2055 | # Add the worksheet data that the charts will refer to. |
| 2056 | my $headings = [ 'Category', 'Values 1', 'Values 2' ]; |
| 2057 | my $data = [ |
| 2058 | [ 2, 3, 4, 5, 6, 7 ], |
| 2059 | [ 1, 4, 5, 2, 1, 5 ], |
| 2060 | [ 3, 6, 7, 5, 4, 3 ], |
| 2061 | ]; |
| 2062 | |
| 2063 | $worksheet->write( 'A1', $headings, $bold ); |
| 2064 | $worksheet->write( 'A2', $data ); |
| 2065 | |
| 2066 | |
| 2067 | ############################################################################### |
| 2068 | # |
| 2069 | # Example 1. A minimal chart. |
| 2070 | # |
| 2071 | my $chart1 = $workbook->add_chart( type => 'bar' ); |
| 2072 | |
| 2073 | # Add values only. Use the default categories. |
| 2074 | $chart1->add_series( values => '=Sheet1!$B$2:$B$7' ); |
| 2075 | |
| 2076 | |
| 2077 | ############################################################################### |
| 2078 | # |
| 2079 | # Example 2. A minimal chart with user specified categories (X axis) |
| 2080 | # and a series name. |
| 2081 | # |
| 2082 | my $chart2 = $workbook->add_chart( type => 'bar' ); |
| 2083 | |
| 2084 | # Configure the series. |
| 2085 | $chart2->add_series( |
| 2086 | categories => '=Sheet1!$A$2:$A$7', |
| 2087 | values => '=Sheet1!$B$2:$B$7', |
| 2088 | name => 'Test data series 1', |
| 2089 | ); |
| 2090 | |
| 2091 | |
| 2092 | ############################################################################### |
| 2093 | # |
| 2094 | # Example 3. Same as previous chart but with added title and axes labels. |
| 2095 | # |
| 2096 | my $chart3 = $workbook->add_chart( type => 'bar' ); |
| 2097 | |
| 2098 | # Configure the series. |
| 2099 | $chart3->add_series( |
| 2100 | categories => '=Sheet1!$A$2:$A$7', |
| 2101 | values => '=Sheet1!$B$2:$B$7', |
| 2102 | name => 'Test data series 1', |
| 2103 | ); |
| 2104 | |
| 2105 | # Add some labels. |
| 2106 | $chart3->set_title( name => 'Results of sample analysis' ); |
| 2107 | $chart3->set_x_axis( name => 'Sample number' ); |
| 2108 | $chart3->set_y_axis( name => 'Sample length (cm)' ); |
| 2109 | |
| 2110 | |
| 2111 | ############################################################################### |
| 2112 | # |
| 2113 | # Example 4. Same as previous chart but with an added series and with a |
| 2114 | # user specified chart sheet name. |
| 2115 | # |
| 2116 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'bar' ); |
| 2117 | |
| 2118 | # Configure the series. |
| 2119 | $chart4->add_series( |
| 2120 | categories => '=Sheet1!$A$2:$A$7', |
| 2121 | values => '=Sheet1!$B$2:$B$7', |
| 2122 | name => 'Test data series 1', |
| 2123 | ); |
| 2124 | |
| 2125 | # Add another series. |
| 2126 | $chart4->add_series( |
| 2127 | categories => '=Sheet1!$A$2:$A$7', |
| 2128 | values => '=Sheet1!$C$2:$C$7', |
| 2129 | name => 'Test data series 2', |
| 2130 | ); |
| 2131 | |
| 2132 | # Add some labels. |
| 2133 | $chart4->set_title( name => 'Results of sample analysis' ); |
| 2134 | $chart4->set_x_axis( name => 'Sample number' ); |
| 2135 | $chart4->set_y_axis( name => 'Sample length (cm)' ); |
| 2136 | |
| 2137 | |
| 2138 | ############################################################################### |
| 2139 | # |
| 2140 | # Example 5. Same as Example 3 but as an embedded chart. |
| 2141 | # |
| 2142 | my $chart5 = $workbook->add_chart( type => 'bar', embedded => 1 ); |
| 2143 | |
| 2144 | # Configure the series. |
| 2145 | $chart5->add_series( |
| 2146 | categories => '=Sheet1!$A$2:$A$7', |
| 2147 | values => '=Sheet1!$B$2:$B$7', |
| 2148 | name => 'Test data series 1', |
| 2149 | ); |
| 2150 | |
| 2151 | # Add some labels. |
| 2152 | $chart5->set_title( name => 'Results of sample analysis' ); |
| 2153 | $chart5->set_x_axis( name => 'Sample number' ); |
| 2154 | $chart5->set_y_axis( name => 'Sample length (cm)' ); |
| 2155 | |
| 2156 | # Insert the chart into the main worksheet. |
| 2157 | $worksheet->insert_chart( 'E2', $chart5 ); |
| 2158 | |
| 2159 | __END__ |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_bar.pl> |
| 2164 | |
| 2165 | =head2 Example: chart_column.pl |
| 2166 | |
| 2167 | |
| 2168 | |
| 2169 | A simple demo of Column charts in Spreadsheet::WriteExcel. |
| 2170 | |
| 2171 | |
| 2172 | |
| 2173 | =begin html |
| 2174 | |
| 2175 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_column.jpg" width="640" height="420" alt="Output from chart_column.pl" /></center></p> |
| 2176 | |
| 2177 | =end html |
| 2178 | |
| 2179 | Source code for this example: |
| 2180 | |
| 2181 | #!/usr/bin/perl -w |
| 2182 | |
| 2183 | ############################################################################### |
| 2184 | # |
| 2185 | # A simple demo of Column charts in Spreadsheet::WriteExcel. |
| 2186 | # |
| 2187 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 2188 | # |
| 2189 | |
| 2190 | use strict; |
| 2191 | use Spreadsheet::WriteExcel; |
| 2192 | |
| 2193 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_column.xls' ); |
| 2194 | my $worksheet = $workbook->add_worksheet(); |
| 2195 | my $bold = $workbook->add_format( bold => 1 ); |
| 2196 | |
| 2197 | # Add the worksheet data that the charts will refer to. |
| 2198 | my $headings = [ 'Category', 'Values 1', 'Values 2' ]; |
| 2199 | my $data = [ |
| 2200 | [ 2, 3, 4, 5, 6, 7 ], |
| 2201 | [ 1, 4, 5, 2, 1, 5 ], |
| 2202 | [ 3, 6, 7, 5, 4, 3 ], |
| 2203 | ]; |
| 2204 | |
| 2205 | $worksheet->write( 'A1', $headings, $bold ); |
| 2206 | $worksheet->write( 'A2', $data ); |
| 2207 | |
| 2208 | |
| 2209 | ############################################################################### |
| 2210 | # |
| 2211 | # Example 1. A minimal chart. |
| 2212 | # |
| 2213 | my $chart1 = $workbook->add_chart( type => 'column' ); |
| 2214 | |
| 2215 | # Add values only. Use the default categories. |
| 2216 | $chart1->add_series( values => '=Sheet1!$B$2:$B$7' ); |
| 2217 | |
| 2218 | |
| 2219 | ############################################################################### |
| 2220 | # |
| 2221 | # Example 2. A minimal chart with user specified categories (X axis) |
| 2222 | # and a series name. |
| 2223 | # |
| 2224 | my $chart2 = $workbook->add_chart( type => 'column' ); |
| 2225 | |
| 2226 | # Configure the series. |
| 2227 | $chart2->add_series( |
| 2228 | categories => '=Sheet1!$A$2:$A$7', |
| 2229 | values => '=Sheet1!$B$2:$B$7', |
| 2230 | name => 'Test data series 1', |
| 2231 | ); |
| 2232 | |
| 2233 | |
| 2234 | ############################################################################### |
| 2235 | # |
| 2236 | # Example 3. Same as previous chart but with added title and axes labels. |
| 2237 | # |
| 2238 | my $chart3 = $workbook->add_chart( type => 'column' ); |
| 2239 | |
| 2240 | # Configure the series. |
| 2241 | $chart3->add_series( |
| 2242 | categories => '=Sheet1!$A$2:$A$7', |
| 2243 | values => '=Sheet1!$B$2:$B$7', |
| 2244 | name => 'Test data series 1', |
| 2245 | ); |
| 2246 | |
| 2247 | # Add some labels. |
| 2248 | $chart3->set_title( name => 'Results of sample analysis' ); |
| 2249 | $chart3->set_x_axis( name => 'Sample number' ); |
| 2250 | $chart3->set_y_axis( name => 'Sample length (cm)' ); |
| 2251 | |
| 2252 | |
| 2253 | ############################################################################### |
| 2254 | # |
| 2255 | # Example 4. Same as previous chart but with an added series and with a |
| 2256 | # user specified chart sheet name. |
| 2257 | # |
| 2258 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'column' ); |
| 2259 | |
| 2260 | # Configure the series. |
| 2261 | $chart4->add_series( |
| 2262 | categories => '=Sheet1!$A$2:$A$7', |
| 2263 | values => '=Sheet1!$B$2:$B$7', |
| 2264 | name => 'Test data series 1', |
| 2265 | ); |
| 2266 | |
| 2267 | # Add another series. |
| 2268 | $chart4->add_series( |
| 2269 | categories => '=Sheet1!$A$2:$A$7', |
| 2270 | values => '=Sheet1!$C$2:$C$7', |
| 2271 | name => 'Test data series 2', |
| 2272 | ); |
| 2273 | |
| 2274 | # Add some labels. |
| 2275 | $chart4->set_title( name => 'Results of sample analysis' ); |
| 2276 | $chart4->set_x_axis( name => 'Sample number' ); |
| 2277 | $chart4->set_y_axis( name => 'Sample length (cm)' ); |
| 2278 | |
| 2279 | |
| 2280 | ############################################################################### |
| 2281 | # |
| 2282 | # Example 5. Same as Example 3 but as an embedded chart. |
| 2283 | # |
| 2284 | my $chart5 = $workbook->add_chart( type => 'column', embedded => 1 ); |
| 2285 | |
| 2286 | # Configure the series. |
| 2287 | $chart5->add_series( |
| 2288 | categories => '=Sheet1!$A$2:$A$7', |
| 2289 | values => '=Sheet1!$B$2:$B$7', |
| 2290 | name => 'Test data series 1', |
| 2291 | ); |
| 2292 | |
| 2293 | # Add some labels. |
| 2294 | $chart5->set_title( name => 'Results of sample analysis' ); |
| 2295 | $chart5->set_x_axis( name => 'Sample number' ); |
| 2296 | $chart5->set_y_axis( name => 'Sample length (cm)' ); |
| 2297 | |
| 2298 | # Insert the chart into the main worksheet. |
| 2299 | $worksheet->insert_chart( 'E2', $chart5 ); |
| 2300 | |
| 2301 | __END__ |
| 2302 | |
| 2303 | |
| 2304 | |
| 2305 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_column.pl> |
| 2306 | |
| 2307 | =head2 Example: chart_line.pl |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | A simple demo of Line charts in Spreadsheet::WriteExcel. |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | =begin html |
| 2316 | |
| 2317 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_line.jpg" width="640" height="420" alt="Output from chart_line.pl" /></center></p> |
| 2318 | |
| 2319 | =end html |
| 2320 | |
| 2321 | Source code for this example: |
| 2322 | |
| 2323 | #!/usr/bin/perl -w |
| 2324 | |
| 2325 | ############################################################################### |
| 2326 | # |
| 2327 | # A simple demo of Line charts in Spreadsheet::WriteExcel. |
| 2328 | # |
| 2329 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 2330 | # |
| 2331 | |
| 2332 | use strict; |
| 2333 | use Spreadsheet::WriteExcel; |
| 2334 | |
| 2335 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_line.xls' ); |
| 2336 | my $worksheet = $workbook->add_worksheet(); |
| 2337 | my $bold = $workbook->add_format( bold => 1 ); |
| 2338 | |
| 2339 | # Add the worksheet data that the charts will refer to. |
| 2340 | my $headings = [ 'Category', 'Values 1', 'Values 2' ]; |
| 2341 | my $data = [ |
| 2342 | [ 2, 3, 4, 5, 6, 7 ], |
| 2343 | [ 1, 4, 5, 2, 1, 5 ], |
| 2344 | [ 3, 6, 7, 5, 4, 3 ], |
| 2345 | ]; |
| 2346 | |
| 2347 | $worksheet->write( 'A1', $headings, $bold ); |
| 2348 | $worksheet->write( 'A2', $data ); |
| 2349 | |
| 2350 | |
| 2351 | ############################################################################### |
| 2352 | # |
| 2353 | # Example 1. A minimal chart. |
| 2354 | # |
| 2355 | my $chart1 = $workbook->add_chart( type => 'line' ); |
| 2356 | |
| 2357 | # Add values only. Use the default categories. |
| 2358 | $chart1->add_series( values => '=Sheet1!$B$2:$B$7' ); |
| 2359 | |
| 2360 | |
| 2361 | ############################################################################### |
| 2362 | # |
| 2363 | # Example 2. A minimal chart with user specified categories (X axis) |
| 2364 | # and a series name. |
| 2365 | # |
| 2366 | my $chart2 = $workbook->add_chart( type => 'line' ); |
| 2367 | |
| 2368 | # Configure the series. |
| 2369 | $chart2->add_series( |
| 2370 | categories => '=Sheet1!$A$2:$A$7', |
| 2371 | values => '=Sheet1!$B$2:$B$7', |
| 2372 | name => 'Test data series 1', |
| 2373 | ); |
| 2374 | |
| 2375 | |
| 2376 | ############################################################################### |
| 2377 | # |
| 2378 | # Example 3. Same as previous chart but with added title and axes labels. |
| 2379 | # |
| 2380 | my $chart3 = $workbook->add_chart( type => 'line' ); |
| 2381 | |
| 2382 | # Configure the series. |
| 2383 | $chart3->add_series( |
| 2384 | categories => '=Sheet1!$A$2:$A$7', |
| 2385 | values => '=Sheet1!$B$2:$B$7', |
| 2386 | name => 'Test data series 1', |
| 2387 | ); |
| 2388 | |
| 2389 | # Add some labels. |
| 2390 | $chart3->set_title( name => 'Results of sample analysis' ); |
| 2391 | $chart3->set_x_axis( name => 'Sample number' ); |
| 2392 | $chart3->set_y_axis( name => 'Sample length (cm)' ); |
| 2393 | |
| 2394 | |
| 2395 | ############################################################################### |
| 2396 | # |
| 2397 | # Example 4. Same as previous chart but with an added series and with a |
| 2398 | # user specified chart sheet name. |
| 2399 | # |
| 2400 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'line' ); |
| 2401 | |
| 2402 | # Configure the series. |
| 2403 | $chart4->add_series( |
| 2404 | categories => '=Sheet1!$A$2:$A$7', |
| 2405 | values => '=Sheet1!$B$2:$B$7', |
| 2406 | name => 'Test data series 1', |
| 2407 | ); |
| 2408 | |
| 2409 | # Add another series. |
| 2410 | $chart4->add_series( |
| 2411 | categories => '=Sheet1!$A$2:$A$7', |
| 2412 | values => '=Sheet1!$C$2:$C$7', |
| 2413 | name => 'Test data series 2', |
| 2414 | ); |
| 2415 | |
| 2416 | # Add some labels. |
| 2417 | $chart4->set_title( name => 'Results of sample analysis' ); |
| 2418 | $chart4->set_x_axis( name => 'Sample number' ); |
| 2419 | $chart4->set_y_axis( name => 'Sample length (cm)' ); |
| 2420 | |
| 2421 | |
| 2422 | ############################################################################### |
| 2423 | # |
| 2424 | # Example 5. Same as Example 3 but as an embedded chart. |
| 2425 | # |
| 2426 | my $chart5 = $workbook->add_chart( type => 'line', embedded => 1 ); |
| 2427 | |
| 2428 | # Configure the series. |
| 2429 | $chart5->add_series( |
| 2430 | categories => '=Sheet1!$A$2:$A$7', |
| 2431 | values => '=Sheet1!$B$2:$B$7', |
| 2432 | name => 'Test data series 1', |
| 2433 | ); |
| 2434 | |
| 2435 | # Add some labels. |
| 2436 | $chart5->set_title( name => 'Results of sample analysis' ); |
| 2437 | $chart5->set_x_axis( name => 'Sample number' ); |
| 2438 | $chart5->set_y_axis( name => 'Sample length (cm)' ); |
| 2439 | |
| 2440 | # Insert the chart into the main worksheet. |
| 2441 | $worksheet->insert_chart( 'E2', $chart5 ); |
| 2442 | |
| 2443 | __END__ |
| 2444 | |
| 2445 | |
| 2446 | |
| 2447 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_line.pl> |
| 2448 | |
| 2449 | =head2 Example: chart_pie.pl |
| 2450 | |
| 2451 | |
| 2452 | |
| 2453 | A simple demo of Pie charts in Spreadsheet::WriteExcel. |
| 2454 | |
| 2455 | |
| 2456 | |
| 2457 | =begin html |
| 2458 | |
| 2459 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_pie.jpg" width="640" height="420" alt="Output from chart_pie.pl" /></center></p> |
| 2460 | |
| 2461 | =end html |
| 2462 | |
| 2463 | Source code for this example: |
| 2464 | |
| 2465 | #!/usr/bin/perl -w |
| 2466 | |
| 2467 | ############################################################################### |
| 2468 | # |
| 2469 | # A simple demo of Pie charts in Spreadsheet::WriteExcel. |
| 2470 | # |
| 2471 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 2472 | # |
| 2473 | |
| 2474 | use strict; |
| 2475 | use Spreadsheet::WriteExcel; |
| 2476 | |
| 2477 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' ); |
| 2478 | my $worksheet = $workbook->add_worksheet(); |
| 2479 | my $bold = $workbook->add_format( bold => 1 ); |
| 2480 | |
| 2481 | # Add the worksheet data that the charts will refer to. |
| 2482 | my $headings = [ 'Category', 'Values' ]; |
| 2483 | my $data = [ |
| 2484 | [ 'Apple', 'Cherry', 'Pecan' ], |
| 2485 | [ 60, 30, 10 ], |
| 2486 | ]; |
| 2487 | |
| 2488 | $worksheet->write( 'A1', $headings, $bold ); |
| 2489 | $worksheet->write( 'A2', $data ); |
| 2490 | |
| 2491 | |
| 2492 | ############################################################################### |
| 2493 | # |
| 2494 | # Example 1. A minimal chart. |
| 2495 | # |
| 2496 | my $chart1 = $workbook->add_chart( type => 'pie' ); |
| 2497 | |
| 2498 | # Add values only. Use the default categories. |
| 2499 | $chart1->add_series( values => '=Sheet1!$B$2:$B$4' ); |
| 2500 | |
| 2501 | |
| 2502 | ############################################################################### |
| 2503 | # |
| 2504 | # Example 2. A minimal chart with user specified categories and a series name. |
| 2505 | # |
| 2506 | my $chart2 = $workbook->add_chart( type => 'pie' ); |
| 2507 | |
| 2508 | # Configure the series. |
| 2509 | $chart2->add_series( |
| 2510 | categories => '=Sheet1!$A$2:$A$4', |
| 2511 | values => '=Sheet1!$B$2:$B$4', |
| 2512 | name => 'Pie sales data', |
| 2513 | ); |
| 2514 | |
| 2515 | |
| 2516 | ############################################################################### |
| 2517 | # |
| 2518 | # Example 3. Same as previous chart but with an added title. |
| 2519 | # |
| 2520 | my $chart3 = $workbook->add_chart( type => 'pie' ); |
| 2521 | |
| 2522 | # Configure the series. |
| 2523 | $chart3->add_series( |
| 2524 | categories => '=Sheet1!$A$2:$A$4', |
| 2525 | values => '=Sheet1!$B$2:$B$4', |
| 2526 | name => 'Pie sales data', |
| 2527 | ); |
| 2528 | |
| 2529 | # Add a title. |
| 2530 | $chart3->set_title( name => 'Popular Pie Types' ); |
| 2531 | |
| 2532 | |
| 2533 | ############################################################################### |
| 2534 | # |
| 2535 | # Example 4. Same as previous chart with a user specified chart sheet name. |
| 2536 | # |
| 2537 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'pie' ); |
| 2538 | |
| 2539 | # Configure the series. |
| 2540 | $chart4->add_series( |
| 2541 | categories => '=Sheet1!$A$2:$A$4', |
| 2542 | values => '=Sheet1!$B$2:$B$4', |
| 2543 | name => 'Pie sales data', |
| 2544 | ); |
| 2545 | |
| 2546 | # The other chart_*.pl examples add a second series in example 4 but additional |
| 2547 | # series aren't plotted in a pie chart. |
| 2548 | |
| 2549 | # Add a title. |
| 2550 | $chart4->set_title( name => 'Popular Pie Types' ); |
| 2551 | |
| 2552 | |
| 2553 | ############################################################################### |
| 2554 | # |
| 2555 | # Example 5. Same as Example 3 but as an embedded chart. |
| 2556 | # |
| 2557 | my $chart5 = $workbook->add_chart( type => 'pie', embedded => 1 ); |
| 2558 | |
| 2559 | # Configure the series. |
| 2560 | $chart5->add_series( |
| 2561 | categories => '=Sheet1!$A$2:$A$4', |
| 2562 | values => '=Sheet1!$B$2:$B$4', |
| 2563 | name => 'Pie sales data', |
| 2564 | ); |
| 2565 | |
| 2566 | # Add a title. |
| 2567 | $chart5->set_title( name => 'Popular Pie Types' ); |
| 2568 | |
| 2569 | # Insert the chart into the main worksheet. |
| 2570 | $worksheet->insert_chart( 'D2', $chart5 ); |
| 2571 | |
| 2572 | __END__ |
| 2573 | |
| 2574 | |
| 2575 | |
| 2576 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_pie.pl> |
| 2577 | |
| 2578 | =head2 Example: chart_scatter.pl |
| 2579 | |
| 2580 | |
| 2581 | |
| 2582 | A simple demo of Scatter charts in Spreadsheet::WriteExcel. |
| 2583 | |
| 2584 | |
| 2585 | |
| 2586 | =begin html |
| 2587 | |
| 2588 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_scatter.jpg" width="640" height="420" alt="Output from chart_scatter.pl" /></center></p> |
| 2589 | |
| 2590 | =end html |
| 2591 | |
| 2592 | Source code for this example: |
| 2593 | |
| 2594 | #!/usr/bin/perl -w |
| 2595 | |
| 2596 | ############################################################################### |
| 2597 | # |
| 2598 | # A simple demo of Scatter charts in Spreadsheet::WriteExcel. |
| 2599 | # |
| 2600 | # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org |
| 2601 | # |
| 2602 | |
| 2603 | use strict; |
| 2604 | use Spreadsheet::WriteExcel; |
| 2605 | |
| 2606 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_scatter.xls' ); |
| 2607 | my $worksheet = $workbook->add_worksheet(); |
| 2608 | my $bold = $workbook->add_format( bold => 1 ); |
| 2609 | |
| 2610 | # Add the worksheet data that the charts will refer to. |
| 2611 | my $headings = [ 'Category', 'Values 1', 'Values 2' ]; |
| 2612 | my $data = [ |
| 2613 | [ 2, 3, 4, 5, 6, 7 ], |
| 2614 | [ 1, 4, 5, 2, 1, 5 ], |
| 2615 | [ 3, 6, 7, 5, 4, 3 ], |
| 2616 | ]; |
| 2617 | |
| 2618 | $worksheet->write( 'A1', $headings, $bold ); |
| 2619 | $worksheet->write( 'A2', $data ); |
| 2620 | |
| 2621 | |
| 2622 | ############################################################################### |
| 2623 | # |
| 2624 | # Example 1. A minimal chart. |
| 2625 | # |
| 2626 | my $chart1 = $workbook->add_chart( type => 'scatter' ); |
| 2627 | |
| 2628 | # Add values only. Use the default categories. |
| 2629 | $chart1->add_series( values => '=Sheet1!$B$2:$B$7' ); |
| 2630 | |
| 2631 | |
| 2632 | ############################################################################### |
| 2633 | # |
| 2634 | # Example 2. A minimal chart with user specified categories (X axis) |
| 2635 | # and a series name. |
| 2636 | # |
| 2637 | my $chart2 = $workbook->add_chart( type => 'scatter' ); |
| 2638 | |
| 2639 | # Configure the series. |
| 2640 | $chart2->add_series( |
| 2641 | categories => '=Sheet1!$A$2:$A$7', |
| 2642 | values => '=Sheet1!$B$2:$B$7', |
| 2643 | name => 'Test data series 1', |
| 2644 | ); |
| 2645 | |
| 2646 | |
| 2647 | ############################################################################### |
| 2648 | # |
| 2649 | # Example 3. Same as previous chart but with added title and axes labels. |
| 2650 | # |
| 2651 | my $chart3 = $workbook->add_chart( type => 'scatter' ); |
| 2652 | |
| 2653 | # Configure the series. |
| 2654 | $chart3->add_series( |
| 2655 | categories => '=Sheet1!$A$2:$A$7', |
| 2656 | values => '=Sheet1!$B$2:$B$7', |
| 2657 | name => 'Test data series 1', |
| 2658 | ); |
| 2659 | |
| 2660 | # Add some labels. |
| 2661 | $chart3->set_title( name => 'Results of sample analysis' ); |
| 2662 | $chart3->set_x_axis( name => 'Sample number' ); |
| 2663 | $chart3->set_y_axis( name => 'Sample length (cm)' ); |
| 2664 | |
| 2665 | |
| 2666 | ############################################################################### |
| 2667 | # |
| 2668 | # Example 4. Same as previous chart but with an added series and with a |
| 2669 | # user specified chart sheet name. |
| 2670 | # |
| 2671 | my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'scatter' ); |
| 2672 | |
| 2673 | # Configure the series. |
| 2674 | $chart4->add_series( |
| 2675 | categories => '=Sheet1!$A$2:$A$7', |
| 2676 | values => '=Sheet1!$B$2:$B$7', |
| 2677 | name => 'Test data series 1', |
| 2678 | ); |
| 2679 | |
| 2680 | # Add another series. |
| 2681 | $chart4->add_series( |
| 2682 | categories => '=Sheet1!$A$2:$A$7', |
| 2683 | values => '=Sheet1!$C$2:$C$7', |
| 2684 | name => 'Test data series 2', |
| 2685 | ); |
| 2686 | |
| 2687 | # Add some labels. |
| 2688 | $chart4->set_title( name => 'Results of sample analysis' ); |
| 2689 | $chart4->set_x_axis( name => 'Sample number' ); |
| 2690 | $chart4->set_y_axis( name => 'Sample length (cm)' ); |
| 2691 | |
| 2692 | |
| 2693 | ############################################################################### |
| 2694 | # |
| 2695 | # Example 5. Same as Example 3 but as an embedded chart. |
| 2696 | # |
| 2697 | my $chart5 = $workbook->add_chart( type => 'scatter', embedded => 1 ); |
| 2698 | |
| 2699 | # Configure the series. |
| 2700 | $chart5->add_series( |
| 2701 | categories => '=Sheet1!$A$2:$A$7', |
| 2702 | values => '=Sheet1!$B$2:$B$7', |
| 2703 | name => 'Test data series 1', |
| 2704 | ); |
| 2705 | |
| 2706 | # Add some labels. |
| 2707 | $chart5->set_title( name => 'Results of sample analysis' ); |
| 2708 | $chart5->set_x_axis( name => 'Sample number' ); |
| 2709 | $chart5->set_y_axis( name => 'Sample length (cm)' ); |
| 2710 | |
| 2711 | # Insert the chart into the main worksheet. |
| 2712 | $worksheet->insert_chart( 'E2', $chart5 ); |
| 2713 | |
| 2714 | __END__ |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_scatter.pl> |
| 2719 | |
| 2720 | =head2 Example: chart_stock.pl |
| 2721 | |
| 2722 | |
| 2723 | |
| 2724 | A simple demo of Stock charts in Spreadsheet::WriteExcel. |
| 2725 | |
| 2726 | |
| 2727 | |
| 2728 | =begin html |
| 2729 | |
| 2730 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_stock.jpg" width="640" height="420" alt="Output from chart_stock.pl" /></center></p> |
| 2731 | |
| 2732 | =end html |
| 2733 | |
| 2734 | Source code for this example: |
| 2735 | |
| 2736 | #!/usr/bin/perl -w |
| 2737 | |
| 2738 | ############################################################################### |
| 2739 | # |
| 2740 | # A simple demo of Stock charts in Spreadsheet::WriteExcel. |
| 2741 | # |
| 2742 | # reverse('©'), January 2010, John McNamara, jmcnamara@cpan.org |
| 2743 | # |
| 2744 | |
| 2745 | |
| 2746 | use strict; |
| 2747 | use Spreadsheet::WriteExcel; |
| 2748 | |
| 2749 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_stock.xls' ); |
| 2750 | my $worksheet = $workbook->add_worksheet(); |
| 2751 | |
| 2752 | |
| 2753 | ############################################################################### |
| 2754 | # |
| 2755 | # Set up the data worksheet that the charts will refer to. We read the example |
| 2756 | # data from the __DATA__ section at the end of the file. This simulates |
| 2757 | # reading the data from a database or other source. |
| 2758 | # |
| 2759 | # The default Excel Stock chart is an Open-High-Low-Close chart. Therefore |
| 2760 | # we will need data for each of those series. |
| 2761 | # |
| 2762 | # The layout of the __DATA__ section is similar to the layout of the worksheet. |
| 2763 | # |
| 2764 | |
| 2765 | # Add some formats. |
| 2766 | my $bold = $workbook->add_format( bold => 1 ); |
| 2767 | my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' ); |
| 2768 | |
| 2769 | # Increase the width of the column used for date to make it clearer. |
| 2770 | $worksheet->set_column( 'A:A', 12 ); |
| 2771 | |
| 2772 | # Read the data from the __DATA__ section at the end. In a real example this |
| 2773 | # would probably be a database query. |
| 2774 | my @stock_data; |
| 2775 | |
| 2776 | while ( <DATA> ) { |
| 2777 | next unless /\S/; # Skip blank lines. |
| 2778 | next if /^#/; # Skip comments. |
| 2779 | |
| 2780 | push @stock_data, [split]; |
| 2781 | } |
| 2782 | |
| 2783 | # Write the data to the worksheet. |
| 2784 | my $row = 0; |
| 2785 | my $col = 0; |
| 2786 | |
| 2787 | my $headers = shift @stock_data; |
| 2788 | $worksheet->write( $row++, $col, $headers, $bold ); |
| 2789 | |
| 2790 | for my $stock_data ( @stock_data ) { |
| 2791 | |
| 2792 | my @data = @$stock_data; |
| 2793 | my $date = shift @data; |
| 2794 | |
| 2795 | $worksheet->write( $row, $col, $date, $date_format ); |
| 2796 | $worksheet->write( $row, $col + 1, \@data ); |
| 2797 | |
| 2798 | $row++; |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | ############################################################################### |
| 2803 | # |
| 2804 | # Example 1. A default Open-High-Low-Close chart with series names, axes labels |
| 2805 | # and a title. |
| 2806 | # |
| 2807 | |
| 2808 | my $chart1 = $workbook->add_chart( type => 'stock' ); |
| 2809 | |
| 2810 | # Add a series for each of the Open-High-Low-Close columns. The categories are |
| 2811 | # the dates in the first column. |
| 2812 | |
| 2813 | $chart1->add_series( |
| 2814 | categories => '=Sheet1!$A$2:$A$10', |
| 2815 | values => '=Sheet1!$B$2:$B$10', |
| 2816 | name => 'Open', |
| 2817 | ); |
| 2818 | |
| 2819 | $chart1->add_series( |
| 2820 | categories => '=Sheet1!$A$2:$A$10', |
| 2821 | values => '=Sheet1!$C$2:$C$10', |
| 2822 | name => 'High', |
| 2823 | ); |
| 2824 | |
| 2825 | $chart1->add_series( |
| 2826 | categories => '=Sheet1!$A$2:$A$10', |
| 2827 | values => '=Sheet1!$D$2:$D$10', |
| 2828 | name => 'Low', |
| 2829 | ); |
| 2830 | |
| 2831 | $chart1->add_series( |
| 2832 | categories => '=Sheet1!$A$2:$A$10', |
| 2833 | values => '=Sheet1!$E$2:$E$10', |
| 2834 | name => 'Close', |
| 2835 | ); |
| 2836 | |
| 2837 | # Add a chart title and axes labels. |
| 2838 | $chart1->set_title( name => 'Open-High-Low-Close', ); |
| 2839 | $chart1->set_x_axis( name => 'Date', ); |
| 2840 | $chart1->set_y_axis( name => 'Share price', ); |
| 2841 | |
| 2842 | ############################################################################### |
| 2843 | # |
| 2844 | # Example 2. Same as the previous as an embedded chart. |
| 2845 | # |
| 2846 | |
| 2847 | my $chart2 = $workbook->add_chart( type => 'stock', embedded => 1 ); |
| 2848 | |
| 2849 | # Add a series for each of the Open-High-Low-Close columns. The categories are |
| 2850 | # the dates in the first column. |
| 2851 | |
| 2852 | $chart2->add_series( |
| 2853 | categories => '=Sheet1!$A$2:$A$10', |
| 2854 | values => '=Sheet1!$B$2:$B$10', |
| 2855 | name => 'Open', |
| 2856 | ); |
| 2857 | |
| 2858 | $chart2->add_series( |
| 2859 | categories => '=Sheet1!$A$2:$A$10', |
| 2860 | values => '=Sheet1!$C$2:$C$10', |
| 2861 | name => 'High', |
| 2862 | ); |
| 2863 | |
| 2864 | $chart2->add_series( |
| 2865 | categories => '=Sheet1!$A$2:$A$10', |
| 2866 | values => '=Sheet1!$D$2:$D$10', |
| 2867 | name => 'Low', |
| 2868 | ); |
| 2869 | |
| 2870 | $chart2->add_series( |
| 2871 | categories => '=Sheet1!$A$2:$A$10', |
| 2872 | values => '=Sheet1!$E$2:$E$10', |
| 2873 | name => 'Close', |
| 2874 | ); |
| 2875 | |
| 2876 | # Add a chart title and axes labels. |
| 2877 | $chart2->set_title( name => 'Open-High-Low-Close', ); |
| 2878 | $chart2->set_x_axis( name => 'Date', ); |
| 2879 | $chart2->set_y_axis( name => 'Share price', ); |
| 2880 | |
| 2881 | # Insert the chart into the main worksheet. |
| 2882 | $worksheet->insert_chart( 'G2', $chart2 ); |
| 2883 | |
| 2884 | |
| 2885 | __DATA__ |
| 2886 | # Some sample stock data used for charting. |
| 2887 | Date Open High Low Close |
| 2888 | 2009-08-19 100.00 104.06 95.96 100.34 |
| 2889 | 2009-08-20 101.01 109.08 100.50 108.31 |
| 2890 | 2009-08-23 110.75 113.48 109.05 109.40 |
| 2891 | 2009-08-24 111.24 111.60 103.57 104.87 |
| 2892 | 2009-08-25 104.96 108.00 103.88 106.00 |
| 2893 | 2009-08-26 104.95 107.95 104.66 107.91 |
| 2894 | 2009-08-27 108.10 108.62 105.69 106.15 |
| 2895 | 2009-08-30 105.28 105.49 102.01 102.01 |
| 2896 | 2009-08-31 102.30 103.71 102.16 102.37 |
| 2897 | |
| 2898 | |
| 2899 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_stock.pl> |
| 2900 | |
| 2901 | =head2 Example: chess.pl |
| 2902 | |
| 2903 | |
| 2904 | |
| 2905 | Example of formatting using the Spreadsheet::WriteExcel module via |
| 2906 | property hashes. |
| 2907 | |
| 2908 | Setting format properties via hashes of values is useful when you have |
| 2909 | to deal with a large number of similar formats. Consider for example a |
| 2910 | chess board pattern with black squares, white unformatted squares and |
| 2911 | a border. |
| 2912 | |
| 2913 | This relatively simple example requires 14 separate Format |
| 2914 | objects although there are only 5 different properties: black |
| 2915 | background, top border, bottom border, left border and right border. |
| 2916 | |
| 2917 | Using property hashes it is possible to define these 5 sets of |
| 2918 | properties and then add them together to create the 14 Format |
| 2919 | configurations. |
| 2920 | |
| 2921 | |
| 2922 | |
| 2923 | =begin html |
| 2924 | |
| 2925 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chess.jpg" width="640" height="420" alt="Output from chess.pl" /></center></p> |
| 2926 | |
| 2927 | =end html |
| 2928 | |
| 2929 | Source code for this example: |
| 2930 | |
| 2931 | #!/usr/bin/perl -w |
| 2932 | |
| 2933 | ######################################################################## |
| 2934 | # |
| 2935 | # Example of formatting using the Spreadsheet::WriteExcel module via |
| 2936 | # property hashes. |
| 2937 | # |
| 2938 | # Setting format properties via hashes of values is useful when you have |
| 2939 | # to deal with a large number of similar formats. Consider for example a |
| 2940 | # chess board pattern with black squares, white unformatted squares and |
| 2941 | # a border. |
| 2942 | # |
| 2943 | # This relatively simple example requires 14 separate Format |
| 2944 | # objects although there are only 5 different properties: black |
| 2945 | # background, top border, bottom border, left border and right border. |
| 2946 | # |
| 2947 | # Using property hashes it is possible to define these 5 sets of |
| 2948 | # properties and then add them together to create the 14 Format |
| 2949 | # configurations. |
| 2950 | # |
| 2951 | # reverse('©'), July 2001, John McNamara, jmcnamara@cpan.org |
| 2952 | # |
| 2953 | |
| 2954 | use strict; |
| 2955 | use Spreadsheet::WriteExcel; |
| 2956 | |
| 2957 | my $workbook = Spreadsheet::WriteExcel->new("chess.xls"); |
| 2958 | my $worksheet = $workbook->add_worksheet(); |
| 2959 | |
| 2960 | |
| 2961 | # Some row and column formatting |
| 2962 | $worksheet->set_column('B:I', 10); |
| 2963 | |
| 2964 | for my $i (1..8) { |
| 2965 | $worksheet->set_row($i, 50); |
| 2966 | } |
| 2967 | |
| 2968 | |
| 2969 | # Define the property hashes |
| 2970 | # |
| 2971 | my %black = ( |
| 2972 | 'fg_color' => 'black', |
| 2973 | 'pattern' => 1, |
| 2974 | ); |
| 2975 | |
| 2976 | my %top = ( 'top' => 6 ); |
| 2977 | my %bottom = ( 'bottom' => 6 ); |
| 2978 | my %left = ( 'left' => 6 ); |
| 2979 | my %right = ( 'right' => 6 ); |
| 2980 | |
| 2981 | |
| 2982 | # Define the formats |
| 2983 | # |
| 2984 | my $format01 = $workbook->add_format(%top, %left ); |
| 2985 | my $format02 = $workbook->add_format(%top, %black ); |
| 2986 | my $format03 = $workbook->add_format(%top, ); |
| 2987 | my $format04 = $workbook->add_format(%top, %right, %black ); |
| 2988 | |
| 2989 | my $format05 = $workbook->add_format(%left ); |
| 2990 | my $format06 = $workbook->add_format(%black ); |
| 2991 | my $format07 = $workbook->add_format( ); |
| 2992 | my $format08 = $workbook->add_format(%right, %black ); |
| 2993 | my $format09 = $workbook->add_format(%right ); |
| 2994 | my $format10 = $workbook->add_format(%left, %black ); |
| 2995 | |
| 2996 | my $format11 = $workbook->add_format(%bottom, %left, %black ); |
| 2997 | my $format12 = $workbook->add_format(%bottom ); |
| 2998 | my $format13 = $workbook->add_format(%bottom, %black ); |
| 2999 | my $format14 = $workbook->add_format(%bottom, %right ); |
| 3000 | |
| 3001 | |
| 3002 | # Draw the pattern |
| 3003 | $worksheet->write('B2', '', $format01); |
| 3004 | $worksheet->write('C2', '', $format02); |
| 3005 | $worksheet->write('D2', '', $format03); |
| 3006 | $worksheet->write('E2', '', $format02); |
| 3007 | $worksheet->write('F2', '', $format03); |
| 3008 | $worksheet->write('G2', '', $format02); |
| 3009 | $worksheet->write('H2', '', $format03); |
| 3010 | $worksheet->write('I2', '', $format04); |
| 3011 | |
| 3012 | $worksheet->write('B3', '', $format10); |
| 3013 | $worksheet->write('C3', '', $format07); |
| 3014 | $worksheet->write('D3', '', $format06); |
| 3015 | $worksheet->write('E3', '', $format07); |
| 3016 | $worksheet->write('F3', '', $format06); |
| 3017 | $worksheet->write('G3', '', $format07); |
| 3018 | $worksheet->write('H3', '', $format06); |
| 3019 | $worksheet->write('I3', '', $format09); |
| 3020 | |
| 3021 | $worksheet->write('B4', '', $format05); |
| 3022 | $worksheet->write('C4', '', $format06); |
| 3023 | $worksheet->write('D4', '', $format07); |
| 3024 | $worksheet->write('E4', '', $format06); |
| 3025 | $worksheet->write('F4', '', $format07); |
| 3026 | $worksheet->write('G4', '', $format06); |
| 3027 | $worksheet->write('H4', '', $format07); |
| 3028 | $worksheet->write('I4', '', $format08); |
| 3029 | |
| 3030 | $worksheet->write('B5', '', $format10); |
| 3031 | $worksheet->write('C5', '', $format07); |
| 3032 | $worksheet->write('D5', '', $format06); |
| 3033 | $worksheet->write('E5', '', $format07); |
| 3034 | $worksheet->write('F5', '', $format06); |
| 3035 | $worksheet->write('G5', '', $format07); |
| 3036 | $worksheet->write('H5', '', $format06); |
| 3037 | $worksheet->write('I5', '', $format09); |
| 3038 | |
| 3039 | $worksheet->write('B6', '', $format05); |
| 3040 | $worksheet->write('C6', '', $format06); |
| 3041 | $worksheet->write('D6', '', $format07); |
| 3042 | $worksheet->write('E6', '', $format06); |
| 3043 | $worksheet->write('F6', '', $format07); |
| 3044 | $worksheet->write('G6', '', $format06); |
| 3045 | $worksheet->write('H6', '', $format07); |
| 3046 | $worksheet->write('I6', '', $format08); |
| 3047 | |
| 3048 | $worksheet->write('B7', '', $format10); |
| 3049 | $worksheet->write('C7', '', $format07); |
| 3050 | $worksheet->write('D7', '', $format06); |
| 3051 | $worksheet->write('E7', '', $format07); |
| 3052 | $worksheet->write('F7', '', $format06); |
| 3053 | $worksheet->write('G7', '', $format07); |
| 3054 | $worksheet->write('H7', '', $format06); |
| 3055 | $worksheet->write('I7', '', $format09); |
| 3056 | |
| 3057 | $worksheet->write('B8', '', $format05); |
| 3058 | $worksheet->write('C8', '', $format06); |
| 3059 | $worksheet->write('D8', '', $format07); |
| 3060 | $worksheet->write('E8', '', $format06); |
| 3061 | $worksheet->write('F8', '', $format07); |
| 3062 | $worksheet->write('G8', '', $format06); |
| 3063 | $worksheet->write('H8', '', $format07); |
| 3064 | $worksheet->write('I8', '', $format08); |
| 3065 | |
| 3066 | $worksheet->write('B9', '', $format11); |
| 3067 | $worksheet->write('C9', '', $format12); |
| 3068 | $worksheet->write('D9', '', $format13); |
| 3069 | $worksheet->write('E9', '', $format12); |
| 3070 | $worksheet->write('F9', '', $format13); |
| 3071 | $worksheet->write('G9', '', $format12); |
| 3072 | $worksheet->write('H9', '', $format13); |
| 3073 | $worksheet->write('I9', '', $format14); |
| 3074 | |
| 3075 | |
| 3076 | |
| 3077 | |
| 3078 | |
| 3079 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chess.pl> |
| 3080 | |
| 3081 | =head2 Example: colors.pl |
| 3082 | |
| 3083 | |
| 3084 | |
| 3085 | Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color |
| 3086 | palette. |
| 3087 | |
| 3088 | The set_custom_color() Worksheet method can be used to override one of the |
| 3089 | built-in palette values with a more suitable colour. See the main docs. |
| 3090 | |
| 3091 | |
| 3092 | |
| 3093 | =begin html |
| 3094 | |
| 3095 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/colors.jpg" width="640" height="420" alt="Output from colors.pl" /></center></p> |
| 3096 | |
| 3097 | =end html |
| 3098 | |
| 3099 | Source code for this example: |
| 3100 | |
| 3101 | #!/usr/bin/perl -w |
| 3102 | |
| 3103 | ################################################################################ |
| 3104 | # |
| 3105 | # Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color |
| 3106 | # palette. |
| 3107 | # |
| 3108 | # The set_custom_color() Worksheet method can be used to override one of the |
| 3109 | # built-in palette values with a more suitable colour. See the main docs. |
| 3110 | # |
| 3111 | # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org |
| 3112 | # |
| 3113 | |
| 3114 | use strict; |
| 3115 | use Spreadsheet::WriteExcel; |
| 3116 | |
| 3117 | my $workbook = Spreadsheet::WriteExcel->new("colors.xls"); |
| 3118 | |
| 3119 | # Some common formats |
| 3120 | my $center = $workbook->add_format(align => 'center'); |
| 3121 | my $heading = $workbook->add_format(align => 'center', bold => 1); |
| 3122 | |
| 3123 | |
| 3124 | ###################################################################### |
| 3125 | # |
| 3126 | # Demonstrate the named colors. |
| 3127 | # |
| 3128 | |
| 3129 | my %colors = ( |
| 3130 | 0x08, 'black', |
| 3131 | 0x0C, 'blue', |
| 3132 | 0x10, 'brown', |
| 3133 | 0x0F, 'cyan', |
| 3134 | 0x17, 'gray', |
| 3135 | 0x11, 'green', |
| 3136 | 0x0B, 'lime', |
| 3137 | 0x0E, 'magenta', |
| 3138 | 0x12, 'navy', |
| 3139 | 0x35, 'orange', |
| 3140 | 0x21, 'pink', |
| 3141 | 0x14, 'purple', |
| 3142 | 0x0A, 'red', |
| 3143 | 0x16, 'silver', |
| 3144 | 0x09, 'white', |
| 3145 | 0x0D, 'yellow', |
| 3146 | ); |
| 3147 | |
| 3148 | my $worksheet1 = $workbook->add_worksheet('Named colors'); |
| 3149 | |
| 3150 | $worksheet1->set_column(0, 3, 15); |
| 3151 | |
| 3152 | $worksheet1->write(0, 0, "Index", $heading); |
| 3153 | $worksheet1->write(0, 1, "Index", $heading); |
| 3154 | $worksheet1->write(0, 2, "Name", $heading); |
| 3155 | $worksheet1->write(0, 3, "Color", $heading); |
| 3156 | |
| 3157 | my $i = 1; |
| 3158 | |
| 3159 | while (my($index, $color) = each %colors) { |
| 3160 | my $format = $workbook->add_format( |
| 3161 | fg_color => $color, |
| 3162 | pattern => 1, |
| 3163 | border => 1 |
| 3164 | ); |
| 3165 | |
| 3166 | $worksheet1->write($i+1, 0, $index, $center); |
| 3167 | $worksheet1->write($i+1, 1, sprintf("0x%02X", $index), $center); |
| 3168 | $worksheet1->write($i+1, 2, $color, $center); |
| 3169 | $worksheet1->write($i+1, 3, '', $format); |
| 3170 | $i++; |
| 3171 | } |
| 3172 | |
| 3173 | |
| 3174 | ###################################################################### |
| 3175 | # |
| 3176 | # Demonstrate the standard Excel colors in the range 8..63. |
| 3177 | # |
| 3178 | |
| 3179 | my $worksheet2 = $workbook->add_worksheet('Standard colors'); |
| 3180 | |
| 3181 | $worksheet2->set_column(0, 3, 15); |
| 3182 | |
| 3183 | $worksheet2->write(0, 0, "Index", $heading); |
| 3184 | $worksheet2->write(0, 1, "Index", $heading); |
| 3185 | $worksheet2->write(0, 2, "Color", $heading); |
| 3186 | $worksheet2->write(0, 3, "Name", $heading); |
| 3187 | |
| 3188 | for my $i (8..63) { |
| 3189 | my $format = $workbook->add_format( |
| 3190 | fg_color => $i, |
| 3191 | pattern => 1, |
| 3192 | border => 1 |
| 3193 | ); |
| 3194 | |
| 3195 | $worksheet2->write(($i -7), 0, $i, $center); |
| 3196 | $worksheet2->write(($i -7), 1, sprintf("0x%02X", $i), $center); |
| 3197 | $worksheet2->write(($i -7), 2, '', $format); |
| 3198 | |
| 3199 | # Add the color names |
| 3200 | if (exists $colors{$i}) { |
| 3201 | $worksheet2->write(($i -7), 3, $colors{$i}, $center); |
| 3202 | |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | __END__ |
| 3207 | |
| 3208 | |
| 3209 | |
| 3210 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/colors.pl> |
| 3211 | |
| 3212 | =head2 Example: comments1.pl |
| 3213 | |
| 3214 | |
| 3215 | |
| 3216 | This example demonstrates writing cell comments. |
| 3217 | |
| 3218 | A cell comment is indicated in Excel by a small red triangle in the upper |
| 3219 | right-hand corner of the cell. |
| 3220 | |
| 3221 | For more advanced comment options see comments2.pl. |
| 3222 | |
| 3223 | |
| 3224 | |
| 3225 | =begin html |
| 3226 | |
| 3227 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/comments1.jpg" width="640" height="420" alt="Output from comments1.pl" /></center></p> |
| 3228 | |
| 3229 | =end html |
| 3230 | |
| 3231 | Source code for this example: |
| 3232 | |
| 3233 | #!/usr/bin/perl -w |
| 3234 | |
| 3235 | ############################################################################### |
| 3236 | # |
| 3237 | # This example demonstrates writing cell comments. |
| 3238 | # |
| 3239 | # A cell comment is indicated in Excel by a small red triangle in the upper |
| 3240 | # right-hand corner of the cell. |
| 3241 | # |
| 3242 | # For more advanced comment options see comments2.pl. |
| 3243 | # |
| 3244 | # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org |
| 3245 | # |
| 3246 | |
| 3247 | use strict; |
| 3248 | use Spreadsheet::WriteExcel; |
| 3249 | |
| 3250 | my $workbook = Spreadsheet::WriteExcel->new("comments1.xls"); |
| 3251 | my $worksheet = $workbook->add_worksheet(); |
| 3252 | |
| 3253 | |
| 3254 | |
| 3255 | $worksheet->write ('A1', 'Hello' ); |
| 3256 | $worksheet->write_comment('A1', 'This is a comment'); |
| 3257 | |
| 3258 | __END__ |
| 3259 | |
| 3260 | |
| 3261 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/comments1.pl> |
| 3262 | |
| 3263 | =head2 Example: comments2.pl |
| 3264 | |
| 3265 | |
| 3266 | |
| 3267 | This example demonstrates writing cell comments. |
| 3268 | |
| 3269 | A cell comment is indicated in Excel by a small red triangle in the upper |
| 3270 | right-hand corner of the cell. |
| 3271 | |
| 3272 | Each of the worksheets demonstrates different features of cell comments. |
| 3273 | |
| 3274 | |
| 3275 | |
| 3276 | =begin html |
| 3277 | |
| 3278 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/comments2.jpg" width="640" height="420" alt="Output from comments2.pl" /></center></p> |
| 3279 | |
| 3280 | =end html |
| 3281 | |
| 3282 | Source code for this example: |
| 3283 | |
| 3284 | #!/usr/bin/perl -w |
| 3285 | |
| 3286 | ############################################################################### |
| 3287 | # |
| 3288 | # This example demonstrates writing cell comments. |
| 3289 | # |
| 3290 | # A cell comment is indicated in Excel by a small red triangle in the upper |
| 3291 | # right-hand corner of the cell. |
| 3292 | # |
| 3293 | # Each of the worksheets demonstrates different features of cell comments. |
| 3294 | # |
| 3295 | # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org |
| 3296 | # |
| 3297 | |
| 3298 | use strict; |
| 3299 | use Spreadsheet::WriteExcel; |
| 3300 | |
| 3301 | my $workbook = Spreadsheet::WriteExcel->new("comments2.xls"); |
| 3302 | my $text_wrap = $workbook->add_format(text_wrap => 1, valign => 'top'); |
| 3303 | my $worksheet1 = $workbook->add_worksheet(); |
| 3304 | my $worksheet2 = $workbook->add_worksheet(); |
| 3305 | my $worksheet3 = $workbook->add_worksheet(); |
| 3306 | my $worksheet4 = $workbook->add_worksheet(); |
| 3307 | my $worksheet5 = $workbook->add_worksheet(); |
| 3308 | my $worksheet6 = $workbook->add_worksheet(); |
| 3309 | my $worksheet7 = $workbook->add_worksheet(); |
| 3310 | my $worksheet8 = $workbook->add_worksheet(); |
| 3311 | |
| 3312 | |
| 3313 | # Variables that we will use in each example. |
| 3314 | my $cell_text = ''; |
| 3315 | my $comment = ''; |
| 3316 | |
| 3317 | |
| 3318 | |
| 3319 | |
| 3320 | ############################################################################### |
| 3321 | # |
| 3322 | # Example 1. Demonstrates a simple cell comment without formatting and Unicode |
| 3323 | # comments encoded as UTF-16 and as UTF-8. |
| 3324 | # |
| 3325 | |
| 3326 | # Set up some formatting. |
| 3327 | $worksheet1->set_column('C:C', 25); |
| 3328 | $worksheet1->set_row(2, 50); |
| 3329 | $worksheet1->set_row(5, 50); |
| 3330 | |
| 3331 | |
| 3332 | # Simple ascii string. |
| 3333 | $cell_text = 'Hold the mouse over this cell to see the comment.'; |
| 3334 | |
| 3335 | $comment = 'This is a comment.'; |
| 3336 | |
| 3337 | $worksheet1->write ('C3', $cell_text, $text_wrap); |
| 3338 | $worksheet1->write_comment('C3', $comment); |
| 3339 | |
| 3340 | |
| 3341 | # UTF-16 string. |
| 3342 | $cell_text = 'This is a UTF-16 comment.'; |
| 3343 | |
| 3344 | $comment = pack "n", 0x263a; |
| 3345 | |
| 3346 | $worksheet1->write ('C6', $cell_text, $text_wrap); |
| 3347 | $worksheet1->write_comment('C6', $comment, encoding => 1); |
| 3348 | |
| 3349 | |
| 3350 | # UTF-8 string in perl 5.8. |
| 3351 | if ($] >= 5.008) { |
| 3352 | |
| 3353 | $worksheet1->set_row(8, 50); |
| 3354 | $cell_text = 'This is a UTF-8 string.'; |
| 3355 | $comment = chr 0x263a; |
| 3356 | |
| 3357 | $worksheet1->write ('C9', $cell_text, $text_wrap); |
| 3358 | $worksheet1->write_comment('C9', $comment); |
| 3359 | } |
| 3360 | |
| 3361 | |
| 3362 | |
| 3363 | ############################################################################### |
| 3364 | # |
| 3365 | # Example 2. Demonstrates visible and hidden comments. |
| 3366 | # |
| 3367 | |
| 3368 | # Set up some formatting. |
| 3369 | $worksheet2->set_column('C:C', 25); |
| 3370 | $worksheet2->set_row(2, 50); |
| 3371 | $worksheet2->set_row(5, 50); |
| 3372 | |
| 3373 | |
| 3374 | $cell_text = 'This cell comment is visible.'; |
| 3375 | |
| 3376 | $comment = 'Hello.'; |
| 3377 | |
| 3378 | $worksheet2->write ('C3', $cell_text, $text_wrap); |
| 3379 | $worksheet2->write_comment('C3', $comment, visible => 1); |
| 3380 | |
| 3381 | |
| 3382 | $cell_text = "This cell comment isn't visible (the default)."; |
| 3383 | |
| 3384 | $comment = 'Hello.'; |
| 3385 | |
| 3386 | $worksheet2->write ('C6', $cell_text, $text_wrap); |
| 3387 | $worksheet2->write_comment('C6', $comment); |
| 3388 | |
| 3389 | |
| 3390 | |
| 3391 | |
| 3392 | ############################################################################### |
| 3393 | # |
| 3394 | # Example 3. Demonstrates visible and hidden comments set at the worksheet |
| 3395 | # level. |
| 3396 | # |
| 3397 | |
| 3398 | # Set up some formatting. |
| 3399 | $worksheet3->set_column('C:C', 25); |
| 3400 | $worksheet3->set_row(2, 50); |
| 3401 | $worksheet3->set_row(5, 50); |
| 3402 | $worksheet3->set_row(8, 50); |
| 3403 | |
| 3404 | # Make all comments on the worksheet visible. |
| 3405 | $worksheet3->show_comments(); |
| 3406 | |
| 3407 | $cell_text = 'This cell comment is visible, explicitly.'; |
| 3408 | |
| 3409 | $comment = 'Hello.'; |
| 3410 | |
| 3411 | $worksheet3->write ('C3', $cell_text, $text_wrap); |
| 3412 | $worksheet3->write_comment('C3', $comment, visible => 1); |
| 3413 | |
| 3414 | |
| 3415 | $cell_text = 'This cell comment is also visible because '. |
| 3416 | 'we used show_comments().'; |
| 3417 | |
| 3418 | $comment = 'Hello.'; |
| 3419 | |
| 3420 | $worksheet3->write ('C6', $cell_text, $text_wrap); |
| 3421 | $worksheet3->write_comment('C6', $comment); |
| 3422 | |
| 3423 | |
| 3424 | $cell_text = 'However, we can still override it locally.'; |
| 3425 | |
| 3426 | $comment = 'Hello.'; |
| 3427 | |
| 3428 | $worksheet3->write ('C9', $cell_text, $text_wrap); |
| 3429 | $worksheet3->write_comment('C9', $comment, visible => 0); |
| 3430 | |
| 3431 | |
| 3432 | |
| 3433 | |
| 3434 | ############################################################################### |
| 3435 | # |
| 3436 | # Example 4. Demonstrates changes to the comment box dimensions. |
| 3437 | # |
| 3438 | |
| 3439 | # Set up some formatting. |
| 3440 | $worksheet4->set_column('C:C', 25); |
| 3441 | $worksheet4->set_row(2, 50); |
| 3442 | $worksheet4->set_row(5, 50); |
| 3443 | $worksheet4->set_row(8, 50); |
| 3444 | $worksheet4->set_row(15, 50); |
| 3445 | |
| 3446 | $worksheet4->show_comments(); |
| 3447 | |
| 3448 | $cell_text = 'This cell comment is default size.'; |
| 3449 | |
| 3450 | $comment = 'Hello.'; |
| 3451 | |
| 3452 | $worksheet4->write ('C3', $cell_text, $text_wrap); |
| 3453 | $worksheet4->write_comment('C3', $comment); |
| 3454 | |
| 3455 | |
| 3456 | $cell_text = 'This cell comment is twice as wide.'; |
| 3457 | |
| 3458 | $comment = 'Hello.'; |
| 3459 | |
| 3460 | $worksheet4->write ('C6', $cell_text, $text_wrap); |
| 3461 | $worksheet4->write_comment('C6', $comment, x_scale => 2); |
| 3462 | |
| 3463 | |
| 3464 | $cell_text = 'This cell comment is twice as high.'; |
| 3465 | |
| 3466 | $comment = 'Hello.'; |
| 3467 | |
| 3468 | $worksheet4->write ('C9', $cell_text, $text_wrap); |
| 3469 | $worksheet4->write_comment('C9', $comment, y_scale => 2); |
| 3470 | |
| 3471 | |
| 3472 | $cell_text = 'This cell comment is scaled in both directions.'; |
| 3473 | |
| 3474 | $comment = 'Hello.'; |
| 3475 | |
| 3476 | $worksheet4->write ('C16', $cell_text, $text_wrap); |
| 3477 | $worksheet4->write_comment('C16', $comment, x_scale => 1.2, y_scale => 0.8); |
| 3478 | |
| 3479 | |
| 3480 | $cell_text = 'This cell comment has width and height specified in pixels.'; |
| 3481 | |
| 3482 | $comment = 'Hello.'; |
| 3483 | |
| 3484 | $worksheet4->write ('C19', $cell_text, $text_wrap); |
| 3485 | $worksheet4->write_comment('C19', $comment, width => 200, height => 20); |
| 3486 | |
| 3487 | |
| 3488 | |
| 3489 | ############################################################################### |
| 3490 | # |
| 3491 | # Example 5. Demonstrates changes to the cell comment position. |
| 3492 | # |
| 3493 | |
| 3494 | $worksheet5->set_column('C:C', 25); |
| 3495 | $worksheet5->set_row(2, 50); |
| 3496 | $worksheet5->set_row(5, 50); |
| 3497 | $worksheet5->set_row(8, 50); |
| 3498 | $worksheet5->set_row(11, 50); |
| 3499 | |
| 3500 | $worksheet5->show_comments(); |
| 3501 | |
| 3502 | $cell_text = 'This cell comment is in the default position.'; |
| 3503 | |
| 3504 | $comment = 'Hello.'; |
| 3505 | |
| 3506 | $worksheet5->write ('C3', $cell_text, $text_wrap); |
| 3507 | $worksheet5->write_comment('C3', $comment); |
| 3508 | |
| 3509 | |
| 3510 | $cell_text = 'This cell comment has been moved to another cell.'; |
| 3511 | |
| 3512 | $comment = 'Hello.'; |
| 3513 | |
| 3514 | $worksheet5->write ('C6', $cell_text, $text_wrap); |
| 3515 | $worksheet5->write_comment('C6', $comment, start_cell => 'E4'); |
| 3516 | |
| 3517 | |
| 3518 | $cell_text = 'This cell comment has been moved to another cell.'; |
| 3519 | |
| 3520 | $comment = 'Hello.'; |
| 3521 | |
| 3522 | $worksheet5->write ('C9', $cell_text, $text_wrap); |
| 3523 | $worksheet5->write_comment('C9', $comment, start_row => 8, start_col => 4); |
| 3524 | |
| 3525 | |
| 3526 | $cell_text = 'This cell comment has been shifted within its default cell.'; |
| 3527 | |
| 3528 | $comment = 'Hello.'; |
| 3529 | |
| 3530 | $worksheet5->write ('C12', $cell_text, $text_wrap); |
| 3531 | $worksheet5->write_comment('C12', $comment, x_offset => 30, y_offset => 12); |
| 3532 | |
| 3533 | |
| 3534 | |
| 3535 | ############################################################################### |
| 3536 | # |
| 3537 | # Example 6. Demonstrates changes to the comment background colour. |
| 3538 | # |
| 3539 | |
| 3540 | $worksheet6->set_column('C:C', 25); |
| 3541 | $worksheet6->set_row(2, 50); |
| 3542 | $worksheet6->set_row(5, 50); |
| 3543 | $worksheet6->set_row(8, 50); |
| 3544 | |
| 3545 | $worksheet6->show_comments(); |
| 3546 | |
| 3547 | $cell_text = 'This cell comment has a different colour.'; |
| 3548 | |
| 3549 | $comment = 'Hello.'; |
| 3550 | |
| 3551 | $worksheet6->write ('C3', $cell_text, $text_wrap); |
| 3552 | $worksheet6->write_comment('C3', $comment, color => 'green'); |
| 3553 | |
| 3554 | |
| 3555 | $cell_text = 'This cell comment has the default colour.'; |
| 3556 | |
| 3557 | $comment = 'Hello.'; |
| 3558 | |
| 3559 | $worksheet6->write ('C6', $cell_text, $text_wrap); |
| 3560 | $worksheet6->write_comment('C6', $comment); |
| 3561 | |
| 3562 | |
| 3563 | $cell_text = 'This cell comment has a different colour.'; |
| 3564 | |
| 3565 | $comment = 'Hello.'; |
| 3566 | |
| 3567 | $worksheet6->write ('C9', $cell_text, $text_wrap); |
| 3568 | $worksheet6->write_comment('C9', $comment, color => 0x35); |
| 3569 | |
| 3570 | |
| 3571 | |
| 3572 | |
| 3573 | ############################################################################### |
| 3574 | # |
| 3575 | # Example 7. Demonstrates how to set the cell comment author. |
| 3576 | # |
| 3577 | |
| 3578 | $worksheet7->set_column('C:C', 30); |
| 3579 | $worksheet7->set_row(2, 50); |
| 3580 | $worksheet7->set_row(5, 50); |
| 3581 | $worksheet7->set_row(8, 50); |
| 3582 | $worksheet7->set_row(11, 50); |
| 3583 | |
| 3584 | my $author = ''; |
| 3585 | my $cell = 'C3'; |
| 3586 | |
| 3587 | $cell_text = "Move the mouse over this cell and you will see 'Cell commented ". |
| 3588 | "by $author' (blank) in the status bar at the bottom"; |
| 3589 | |
| 3590 | $comment = 'Hello.'; |
| 3591 | |
| 3592 | $worksheet7->write ($cell, $cell_text, $text_wrap); |
| 3593 | $worksheet7->write_comment($cell, $comment); |
| 3594 | |
| 3595 | |
| 3596 | $author = 'Perl'; |
| 3597 | $cell = 'C6'; |
| 3598 | $cell_text = "Move the mouse over this cell and you will see 'Cell commented ". |
| 3599 | "by $author' in the status bar at the bottom"; |
| 3600 | |
| 3601 | $comment = 'Hello.'; |
| 3602 | |
| 3603 | $worksheet7->write ($cell, $cell_text, $text_wrap); |
| 3604 | $worksheet7->write_comment($cell, $comment, author => $author); |
| 3605 | |
| 3606 | |
| 3607 | $author = pack "n", 0x20AC; # UTF-16 Euro |
| 3608 | $cell = 'C9'; |
| 3609 | $cell_text = "Move the mouse over this cell and you will see 'Cell commented ". |
| 3610 | "by Euro' in the status bar at the bottom"; |
| 3611 | |
| 3612 | $comment = 'Hello.'; |
| 3613 | |
| 3614 | $worksheet7->write ($cell, $cell_text, $text_wrap); |
| 3615 | $worksheet7->write_comment($cell, $comment, author => $author, |
| 3616 | author_encoding => 1 ); |
| 3617 | |
| 3618 | # UTF-8 string in perl 5.8. |
| 3619 | if ($] >= 5.008) { |
| 3620 | $author = chr 0x20AC; |
| 3621 | $cell = 'C12'; |
| 3622 | $cell_text = "Move the mouse over this cell and you will see 'Cell commented ". |
| 3623 | "by $author' in the status bar at the bottom"; |
| 3624 | $comment = 'Hello.'; |
| 3625 | |
| 3626 | $worksheet7->write ($cell, $cell_text, $text_wrap); |
| 3627 | $worksheet7->write_comment($cell, $comment, author => $author); |
| 3628 | |
| 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | ############################################################################### |
| 3633 | # |
| 3634 | # Example 8. Demonstrates the need to explicitly set the row height. |
| 3635 | # |
| 3636 | |
| 3637 | # Set up some formatting. |
| 3638 | $worksheet8->set_column('C:C', 25); |
| 3639 | $worksheet8->set_row(2, 80); |
| 3640 | |
| 3641 | $worksheet8->show_comments(); |
| 3642 | |
| 3643 | |
| 3644 | $cell_text = 'The height of this row has been adjusted explicitly using ' . |
| 3645 | 'set_row(). The size of the comment box is adjusted ' . |
| 3646 | 'accordingly by WriteExcel.'; |
| 3647 | |
| 3648 | $comment = 'Hello.'; |
| 3649 | |
| 3650 | $worksheet8->write ('C3', $cell_text, $text_wrap); |
| 3651 | $worksheet8->write_comment('C3', $comment); |
| 3652 | |
| 3653 | |
| 3654 | $cell_text = 'The height of this row has been adjusted by Excel due to the ' . |
| 3655 | 'text wrap property being set. Unfortunately this means that ' . |
| 3656 | 'the height of the row is unknown to WriteExcel at run time ' . |
| 3657 | "and thus the comment box is stretched as well.\n\n" . |
| 3658 | 'Use set_row() to specify the row height explicitly to avoid ' . |
| 3659 | 'this problem.'; |
| 3660 | |
| 3661 | $comment = 'Hello.'; |
| 3662 | |
| 3663 | $worksheet8->write ('C6', $cell_text, $text_wrap); |
| 3664 | $worksheet8->write_comment('C6', $comment); |
| 3665 | |
| 3666 | __END__ |
| 3667 | |
| 3668 | |
| 3669 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/comments2.pl> |
| 3670 | |
| 3671 | =head2 Example: copyformat.pl |
| 3672 | |
| 3673 | |
| 3674 | |
| 3675 | Example of how to use the format copying method with Spreadsheet::WriteExcel. |
| 3676 | |
| 3677 | This feature isn't required very often. |
| 3678 | |
| 3679 | |
| 3680 | |
| 3681 | =begin html |
| 3682 | |
| 3683 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/copyformat.jpg" width="640" height="420" alt="Output from copyformat.pl" /></center></p> |
| 3684 | |
| 3685 | =end html |
| 3686 | |
| 3687 | Source code for this example: |
| 3688 | |
| 3689 | #!/usr/bin/perl -w |
| 3690 | |
| 3691 | ############################################################################### |
| 3692 | # |
| 3693 | # Example of how to use the format copying method with Spreadsheet::WriteExcel. |
| 3694 | # |
| 3695 | # This feature isn't required very often. |
| 3696 | # |
| 3697 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 3698 | # |
| 3699 | |
| 3700 | use strict; |
| 3701 | use Spreadsheet::WriteExcel; |
| 3702 | |
| 3703 | # Create workbook1 |
| 3704 | my $workbook1 = Spreadsheet::WriteExcel->new("workbook1.xls"); |
| 3705 | my $worksheet1 = $workbook1->add_worksheet(); |
| 3706 | my $format1a = $workbook1->add_format(); |
| 3707 | my $format1b = $workbook1->add_format(); |
| 3708 | |
| 3709 | # Create workbook2 |
| 3710 | my $workbook2 = Spreadsheet::WriteExcel->new("workbook2.xls"); |
| 3711 | my $worksheet2 = $workbook2->add_worksheet(); |
| 3712 | my $format2a = $workbook2->add_format(); |
| 3713 | my $format2b = $workbook2->add_format(); |
| 3714 | |
| 3715 | |
| 3716 | # Create a global format object that isn't tied to a workbook |
| 3717 | my $global_format = Spreadsheet::WriteExcel::Format->new(); |
| 3718 | |
| 3719 | # Set the formatting |
| 3720 | $global_format->set_color('blue'); |
| 3721 | $global_format->set_bold(); |
| 3722 | $global_format->set_italic(); |
| 3723 | |
| 3724 | # Create another example format |
| 3725 | $format1b->set_color('red'); |
| 3726 | |
| 3727 | # Copy the global format properties to the worksheet formats |
| 3728 | $format1a->copy($global_format); |
| 3729 | $format2a->copy($global_format); |
| 3730 | |
| 3731 | # Copy a format from worksheet1 to worksheet2 |
| 3732 | $format2b->copy($format1b); |
| 3733 | |
| 3734 | # Write some output |
| 3735 | $worksheet1->write(0, 0, "Ciao", $format1a); |
| 3736 | $worksheet1->write(1, 0, "Ciao", $format1b); |
| 3737 | |
| 3738 | $worksheet2->write(0, 0, "Hello", $format2a); |
| 3739 | $worksheet2->write(1, 0, "Hello", $format2b); |
| 3740 | |
| 3741 | |
| 3742 | |
| 3743 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/copyformat.pl> |
| 3744 | |
| 3745 | =head2 Example: data_validate.pl |
| 3746 | |
| 3747 | |
| 3748 | |
| 3749 | Example of how to add data validation and dropdown lists to a |
| 3750 | Spreadsheet::WriteExcel file. |
| 3751 | |
| 3752 | Data validation is a feature of Excel which allows you to restrict the data |
| 3753 | that a users enters in a cell and to display help and warning messages. It |
| 3754 | also allows you to restrict input to values in a drop down list. |
| 3755 | |
| 3756 | |
| 3757 | |
| 3758 | =begin html |
| 3759 | |
| 3760 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/data_validate.jpg" width="640" height="420" alt="Output from data_validate.pl" /></center></p> |
| 3761 | |
| 3762 | =end html |
| 3763 | |
| 3764 | Source code for this example: |
| 3765 | |
| 3766 | #!/usr/bin/perl -w |
| 3767 | |
| 3768 | ############################################################################### |
| 3769 | # |
| 3770 | # Example of how to add data validation and dropdown lists to a |
| 3771 | # Spreadsheet::WriteExcel file. |
| 3772 | # |
| 3773 | # Data validation is a feature of Excel which allows you to restrict the data |
| 3774 | # that a users enters in a cell and to display help and warning messages. It |
| 3775 | # also allows you to restrict input to values in a drop down list. |
| 3776 | # |
| 3777 | # reverse('©'), August 2008, John McNamara, jmcnamara@cpan.org |
| 3778 | # |
| 3779 | |
| 3780 | use strict; |
| 3781 | use Spreadsheet::WriteExcel; |
| 3782 | |
| 3783 | my $workbook = Spreadsheet::WriteExcel->new('data_validate.xls'); |
| 3784 | my $worksheet = $workbook->add_worksheet(); |
| 3785 | |
| 3786 | # Add a format for the header cells. |
| 3787 | my $header_format = $workbook->add_format( |
| 3788 | border => 1, |
| 3789 | bg_color => 43, |
| 3790 | bold => 1, |
| 3791 | text_wrap => 1, |
| 3792 | valign => 'vcenter', |
| 3793 | indent => 1, |
| 3794 | ); |
| 3795 | |
| 3796 | # Set up layout of the worksheet. |
| 3797 | $worksheet->set_column('A:A', 64); |
| 3798 | $worksheet->set_column('B:B', 15); |
| 3799 | $worksheet->set_column('D:D', 15); |
| 3800 | $worksheet->set_row(0, 36); |
| 3801 | $worksheet->set_selection('B3'); |
| 3802 | |
| 3803 | |
| 3804 | # Write the header cells and some data that will be used in the examples. |
| 3805 | my $row = 0; |
| 3806 | my $txt; |
| 3807 | my $heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel'; |
| 3808 | my $heading2 = 'Enter values in this column'; |
| 3809 | my $heading3 = 'Sample Data'; |
| 3810 | |
| 3811 | $worksheet->write('A1', $heading1, $header_format); |
| 3812 | $worksheet->write('B1', $heading2, $header_format); |
| 3813 | $worksheet->write('D1', $heading3, $header_format); |
| 3814 | |
| 3815 | $worksheet->write('D3', ['Integers', 1, 10]); |
| 3816 | $worksheet->write('D4', ['List data', 'open', 'high', 'close']); |
| 3817 | $worksheet->write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60]); |
| 3818 | |
| 3819 | |
| 3820 | # |
| 3821 | # Example 1. Limiting input to an integer in a fixed range. |
| 3822 | # |
| 3823 | $txt = 'Enter an integer between 1 and 10'; |
| 3824 | $row += 2; |
| 3825 | |
| 3826 | $worksheet->write($row, 0, $txt); |
| 3827 | $worksheet->data_validation($row, 1, |
| 3828 | { |
| 3829 | validate => 'integer', |
| 3830 | criteria => 'between', |
| 3831 | minimum => 1, |
| 3832 | maximum => 10, |
| 3833 | }); |
| 3834 | |
| 3835 | |
| 3836 | # |
| 3837 | # Example 2. Limiting input to an integer outside a fixed range. |
| 3838 | # |
| 3839 | $txt = 'Enter an integer that is not between 1 and 10 (using cell references)'; |
| 3840 | $row += 2; |
| 3841 | |
| 3842 | $worksheet->write($row, 0, $txt); |
| 3843 | $worksheet->data_validation($row, 1, |
| 3844 | { |
| 3845 | validate => 'integer', |
| 3846 | criteria => 'not between', |
| 3847 | minimum => '=E3', |
| 3848 | maximum => '=F3', |
| 3849 | }); |
| 3850 | |
| 3851 | |
| 3852 | # |
| 3853 | # Example 3. Limiting input to an integer greater than a fixed value. |
| 3854 | # |
| 3855 | $txt = 'Enter an integer greater than 0'; |
| 3856 | $row += 2; |
| 3857 | |
| 3858 | $worksheet->write($row, 0, $txt); |
| 3859 | $worksheet->data_validation($row, 1, |
| 3860 | { |
| 3861 | validate => 'integer', |
| 3862 | criteria => '>', |
| 3863 | value => 0, |
| 3864 | }); |
| 3865 | |
| 3866 | |
| 3867 | # |
| 3868 | # Example 4. Limiting input to an integer less than a fixed value. |
| 3869 | # |
| 3870 | $txt = 'Enter an integer less than 10'; |
| 3871 | $row += 2; |
| 3872 | |
| 3873 | $worksheet->write($row, 0, $txt); |
| 3874 | $worksheet->data_validation($row, 1, |
| 3875 | { |
| 3876 | validate => 'integer', |
| 3877 | criteria => '<', |
| 3878 | value => 10, |
| 3879 | }); |
| 3880 | |
| 3881 | |
| 3882 | # |
| 3883 | # Example 5. Limiting input to a decimal in a fixed range. |
| 3884 | # |
| 3885 | $txt = 'Enter a decimal between 0.1 and 0.5'; |
| 3886 | $row += 2; |
| 3887 | |
| 3888 | $worksheet->write($row, 0, $txt); |
| 3889 | $worksheet->data_validation($row, 1, |
| 3890 | { |
| 3891 | validate => 'decimal', |
| 3892 | criteria => 'between', |
| 3893 | minimum => 0.1, |
| 3894 | maximum => 0.5, |
| 3895 | }); |
| 3896 | |
| 3897 | |
| 3898 | # |
| 3899 | # Example 6. Limiting input to a value in a dropdown list. |
| 3900 | # |
| 3901 | $txt = 'Select a value from a drop down list'; |
| 3902 | $row += 2; |
| 3903 | |
| 3904 | $worksheet->write($row, 0, $txt); |
| 3905 | $worksheet->data_validation($row, 1, |
| 3906 | { |
| 3907 | validate => 'list', |
| 3908 | source => ['open', 'high', 'close'], |
| 3909 | }); |
| 3910 | |
| 3911 | |
| 3912 | # |
| 3913 | # Example 6. Limiting input to a value in a dropdown list. |
| 3914 | # |
| 3915 | $txt = 'Select a value from a drop down list (using a cell range)'; |
| 3916 | $row += 2; |
| 3917 | |
| 3918 | $worksheet->write($row, 0, $txt); |
| 3919 | $worksheet->data_validation($row, 1, |
| 3920 | { |
| 3921 | validate => 'list', |
| 3922 | source => '=E4:G4', |
| 3923 | }); |
| 3924 | |
| 3925 | |
| 3926 | # |
| 3927 | # Example 7. Limiting input to a date in a fixed range. |
| 3928 | # |
| 3929 | $txt = 'Enter a date between 1/1/2008 and 12/12/2008'; |
| 3930 | $row += 2; |
| 3931 | |
| 3932 | $worksheet->write($row, 0, $txt); |
| 3933 | $worksheet->data_validation($row, 1, |
| 3934 | { |
| 3935 | validate => 'date', |
| 3936 | criteria => 'between', |
| 3937 | minimum => '2008-01-01T', |
| 3938 | maximum => '2008-12-12T', |
| 3939 | }); |
| 3940 | |
| 3941 | |
| 3942 | # |
| 3943 | # Example 8. Limiting input to a time in a fixed range. |
| 3944 | # |
| 3945 | $txt = 'Enter a time between 6:00 and 12:00'; |
| 3946 | $row += 2; |
| 3947 | |
| 3948 | $worksheet->write($row, 0, $txt); |
| 3949 | $worksheet->data_validation($row, 1, |
| 3950 | { |
| 3951 | validate => 'time', |
| 3952 | criteria => 'between', |
| 3953 | minimum => 'T06:00', |
| 3954 | maximum => 'T12:00', |
| 3955 | }); |
| 3956 | |
| 3957 | |
| 3958 | # |
| 3959 | # Example 9. Limiting input to a string greater than a fixed length. |
| 3960 | # |
| 3961 | $txt = 'Enter a string longer than 3 characters'; |
| 3962 | $row += 2; |
| 3963 | |
| 3964 | $worksheet->write($row, 0, $txt); |
| 3965 | $worksheet->data_validation($row, 1, |
| 3966 | { |
| 3967 | validate => 'length', |
| 3968 | criteria => '>', |
| 3969 | value => 3, |
| 3970 | }); |
| 3971 | |
| 3972 | |
| 3973 | # |
| 3974 | # Example 10. Limiting input based on a formula. |
| 3975 | # |
| 3976 | $txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"'; |
| 3977 | $row += 2; |
| 3978 | |
| 3979 | $worksheet->write($row, 0, $txt); |
| 3980 | $worksheet->data_validation($row, 1, |
| 3981 | { |
| 3982 | validate => 'custom', |
| 3983 | value => '=AND(F5=50,G5=60)', |
| 3984 | }); |
| 3985 | |
| 3986 | |
| 3987 | # |
| 3988 | # Example 11. Displaying and modify data validation messages. |
| 3989 | # |
| 3990 | $txt = 'Displays a message when you select the cell'; |
| 3991 | $row += 2; |
| 3992 | |
| 3993 | $worksheet->write($row, 0, $txt); |
| 3994 | $worksheet->data_validation($row, 1, |
| 3995 | { |
| 3996 | validate => 'integer', |
| 3997 | criteria => 'between', |
| 3998 | minimum => 1, |
| 3999 | maximum => 100, |
| 4000 | input_title => 'Enter an integer:', |
| 4001 | input_message => 'between 1 and 100', |
| 4002 | }); |
| 4003 | |
| 4004 | |
| 4005 | # |
| 4006 | # Example 12. Displaying and modify data validation messages. |
| 4007 | # |
| 4008 | $txt = 'Display a custom error message when integer isn\'t between 1 and 100'; |
| 4009 | $row += 2; |
| 4010 | |
| 4011 | $worksheet->write($row, 0, $txt); |
| 4012 | $worksheet->data_validation($row, 1, |
| 4013 | { |
| 4014 | validate => 'integer', |
| 4015 | criteria => 'between', |
| 4016 | minimum => 1, |
| 4017 | maximum => 100, |
| 4018 | input_title => 'Enter an integer:', |
| 4019 | input_message => 'between 1 and 100', |
| 4020 | error_title => 'Input value is not valid!', |
| 4021 | error_message => 'It should be an integer between 1 and 100', |
| 4022 | }); |
| 4023 | |
| 4024 | |
| 4025 | # |
| 4026 | # Example 13. Displaying and modify data validation messages. |
| 4027 | # |
| 4028 | $txt = 'Display a custom information message when integer isn\'t between 1 and 100'; |
| 4029 | $row += 2; |
| 4030 | |
| 4031 | $worksheet->write($row, 0, $txt); |
| 4032 | $worksheet->data_validation($row, 1, |
| 4033 | { |
| 4034 | validate => 'integer', |
| 4035 | criteria => 'between', |
| 4036 | minimum => 1, |
| 4037 | maximum => 100, |
| 4038 | input_title => 'Enter an integer:', |
| 4039 | input_message => 'between 1 and 100', |
| 4040 | error_title => 'Input value is not valid!', |
| 4041 | error_message => 'It should be an integer between 1 and 100', |
| 4042 | error_type => 'information', |
| 4043 | }); |
| 4044 | |
| 4045 | |
| 4046 | __END__ |
| 4047 | |
| 4048 | |
| 4049 | |
| 4050 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/data_validate.pl> |
| 4051 | |
| 4052 | =head2 Example: date_time.pl |
| 4053 | |
| 4054 | |
| 4055 | |
| 4056 | Spreadsheet::WriteExcel example of writing dates and times using the |
| 4057 | write_date_time() Worksheet method. |
| 4058 | |
| 4059 | |
| 4060 | |
| 4061 | =begin html |
| 4062 | |
| 4063 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/date_time.jpg" width="640" height="420" alt="Output from date_time.pl" /></center></p> |
| 4064 | |
| 4065 | =end html |
| 4066 | |
| 4067 | Source code for this example: |
| 4068 | |
| 4069 | #!/usr/bin/perl -w |
| 4070 | |
| 4071 | ############################################################################### |
| 4072 | # |
| 4073 | # Spreadsheet::WriteExcel example of writing dates and times using the |
| 4074 | # write_date_time() Worksheet method. |
| 4075 | # |
| 4076 | # reverse('©'), August 2004, John McNamara, jmcnamara@cpan.org |
| 4077 | # |
| 4078 | |
| 4079 | use strict; |
| 4080 | use Spreadsheet::WriteExcel; |
| 4081 | |
| 4082 | |
| 4083 | # Create a new workbook and add a worksheet |
| 4084 | my $workbook = Spreadsheet::WriteExcel->new("date_time.xls"); |
| 4085 | my $worksheet = $workbook->add_worksheet(); |
| 4086 | my $bold = $workbook->add_format(bold => 1); |
| 4087 | my $row = 0; |
| 4088 | |
| 4089 | |
| 4090 | # Expand the first column so that the date is visible. |
| 4091 | $worksheet->set_column("A:B", 30); |
| 4092 | |
| 4093 | |
| 4094 | # Write the column headers |
| 4095 | $worksheet->write('A1', 'Formatted date', $bold); |
| 4096 | $worksheet->write('B1', 'Format', $bold); |
| 4097 | |
| 4098 | |
| 4099 | # Examples date and time formats. In the output file compare how changing |
| 4100 | # the format codes change the appearance of the date. |
| 4101 | # |
| 4102 | my @date_formats = ( |
| 4103 | 'dd/mm/yy', |
| 4104 | 'mm/dd/yy', |
| 4105 | '', |
| 4106 | 'd mm yy', |
| 4107 | 'dd mm yy', |
| 4108 | '', |
| 4109 | 'dd m yy', |
| 4110 | 'dd mm yy', |
| 4111 | 'dd mmm yy', |
| 4112 | 'dd mmmm yy', |
| 4113 | '', |
| 4114 | 'dd mm y', |
| 4115 | 'dd mm yyy', |
| 4116 | 'dd mm yyyy', |
| 4117 | '', |
| 4118 | 'd mmmm yyyy', |
| 4119 | '', |
| 4120 | 'dd/mm/yy', |
| 4121 | 'dd/mm/yy hh:mm', |
| 4122 | 'dd/mm/yy hh:mm:ss', |
| 4123 | 'dd/mm/yy hh:mm:ss.000', |
| 4124 | '', |
| 4125 | 'hh:mm', |
| 4126 | 'hh:mm:ss', |
| 4127 | 'hh:mm:ss.000', |
| 4128 | ); |
| 4129 | |
| 4130 | |
| 4131 | # Write the same date and time using each of the above formats. The empty |
| 4132 | # string formats create a blank line to make the example clearer. |
| 4133 | # |
| 4134 | for my $date_format (@date_formats) { |
| 4135 | $row++; |
| 4136 | next if $date_format eq ''; |
| 4137 | |
| 4138 | # Create a format for the date or time. |
| 4139 | my $format = $workbook->add_format( |
| 4140 | num_format => $date_format, |
| 4141 | align => 'left' |
| 4142 | ); |
| 4143 | |
| 4144 | # Write the same date using different formats. |
| 4145 | $worksheet->write_date_time($row, 0, '2004-08-01T12:30:45.123', $format); |
| 4146 | $worksheet->write ($row, 1, $date_format); |
| 4147 | } |
| 4148 | |
| 4149 | |
| 4150 | # The following is an example of an invalid date. It is written as a string |
| 4151 | # instead of a number. This is also Excel's default behaviour. |
| 4152 | # |
| 4153 | $row += 2; |
| 4154 | $worksheet->write_date_time($row, 0, '2004-13-01T12:30:45.123'); |
| 4155 | $worksheet->write ($row, 1, 'Invalid date. Written as string.', $bold); |
| 4156 | |
| 4157 | __END__ |
| 4158 | |
| 4159 | |
| 4160 | |
| 4161 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/date_time.pl> |
| 4162 | |
| 4163 | =head2 Example: defined_name.pl |
| 4164 | |
| 4165 | |
| 4166 | |
| 4167 | Example of how to create defined names in a Spreadsheet::WriteExcel file. |
| 4168 | |
| 4169 | This method is used to defined a name that can be used to represent a value, |
| 4170 | a single cell or a range of cells in a workbook. |
| 4171 | |
| 4172 | |
| 4173 | |
| 4174 | =begin html |
| 4175 | |
| 4176 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/defined_name.jpg" width="640" height="420" alt="Output from defined_name.pl" /></center></p> |
| 4177 | |
| 4178 | =end html |
| 4179 | |
| 4180 | Source code for this example: |
| 4181 | |
| 4182 | #!/usr/bin/perl -w |
| 4183 | |
| 4184 | ############################################################################### |
| 4185 | # |
| 4186 | # Example of how to create defined names in a Spreadsheet::WriteExcel file. |
| 4187 | # |
| 4188 | # This method is used to defined a name that can be used to represent a value, |
| 4189 | # a single cell or a range of cells in a workbook. |
| 4190 | # |
| 4191 | # reverse('©'), September 2008, John McNamara, jmcnamara@cpan.org |
| 4192 | # |
| 4193 | |
| 4194 | use strict; |
| 4195 | use Spreadsheet::WriteExcel; |
| 4196 | |
| 4197 | my $workbook = Spreadsheet::WriteExcel->new('defined_name.xls'); |
| 4198 | my $worksheet1 = $workbook->add_worksheet(); |
| 4199 | my $worksheet2 = $workbook->add_worksheet(); |
| 4200 | |
| 4201 | |
| 4202 | $workbook->define_name('Exchange_rate', '=0.96'); |
| 4203 | $workbook->define_name('Sales', '=Sheet1!$G$1:$H$10'); |
| 4204 | $workbook->define_name('Sheet2!Sales', '=Sheet2!$G$1:$G$10'); |
| 4205 | |
| 4206 | |
| 4207 | for my $worksheet ($workbook->sheets()) { |
| 4208 | $worksheet->set_column('A:A', 45); |
| 4209 | $worksheet->write('A2', 'This worksheet contains some defined names,'); |
| 4210 | $worksheet->write('A3', 'See the Insert -> Name -> Define dialog.'); |
| 4211 | |
| 4212 | } |
| 4213 | |
| 4214 | |
| 4215 | $worksheet1->write('A4', '=Exchange_rate'); |
| 4216 | |
| 4217 | __END__ |
| 4218 | |
| 4219 | |
| 4220 | |
| 4221 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/defined_name.pl> |
| 4222 | |
| 4223 | =head2 Example: diag_border.pl |
| 4224 | |
| 4225 | |
| 4226 | |
| 4227 | A simple formatting example that demonstrates how to add a diagonal cell |
| 4228 | border with Spreadsheet::WriteExcel |
| 4229 | |
| 4230 | |
| 4231 | |
| 4232 | =begin html |
| 4233 | |
| 4234 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/diag_border.jpg" width="640" height="420" alt="Output from diag_border.pl" /></center></p> |
| 4235 | |
| 4236 | =end html |
| 4237 | |
| 4238 | Source code for this example: |
| 4239 | |
| 4240 | #!/usr/bin/perl -w |
| 4241 | |
| 4242 | ############################################################################## |
| 4243 | # |
| 4244 | # A simple formatting example that demonstrates how to add a diagonal cell |
| 4245 | # border with Spreadsheet::WriteExcel |
| 4246 | # |
| 4247 | # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org |
| 4248 | # |
| 4249 | |
| 4250 | use strict; |
| 4251 | use Spreadsheet::WriteExcel; |
| 4252 | |
| 4253 | |
| 4254 | my $workbook = Spreadsheet::WriteExcel->new('diag_border.xls'); |
| 4255 | my $worksheet = $workbook->add_worksheet(); |
| 4256 | |
| 4257 | |
| 4258 | my $format1 = $workbook->add_format(diag_type => '1'); |
| 4259 | |
| 4260 | my $format2 = $workbook->add_format(diag_type => '2'); |
| 4261 | |
| 4262 | my $format3 = $workbook->add_format(diag_type => '3'); |
| 4263 | |
| 4264 | my $format4 = $workbook->add_format( |
| 4265 | diag_type => '3', |
| 4266 | diag_border => '7', |
| 4267 | diag_color => 'red', |
| 4268 | ); |
| 4269 | |
| 4270 | |
| 4271 | $worksheet->write('B3', 'Text', $format1); |
| 4272 | $worksheet->write('B6', 'Text', $format2); |
| 4273 | $worksheet->write('B9', 'Text', $format3); |
| 4274 | $worksheet->write('B12', 'Text', $format4); |
| 4275 | |
| 4276 | |
| 4277 | |
| 4278 | __END__ |
| 4279 | |
| 4280 | |
| 4281 | |
| 4282 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/diag_border.pl> |
| 4283 | |
| 4284 | =head2 Example: easter_egg.pl |
| 4285 | |
| 4286 | |
| 4287 | |
| 4288 | This uses the Win32::OLE module to expose the Flight Simulator easter egg |
| 4289 | in Excel 97 SR2. |
| 4290 | |
| 4291 | |
| 4292 | |
| 4293 | #!/usr/bin/perl -w |
| 4294 | |
| 4295 | ############################################################################### |
| 4296 | # |
| 4297 | # This uses the Win32::OLE module to expose the Flight Simulator easter egg |
| 4298 | # in Excel 97 SR2. |
| 4299 | # |
| 4300 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 4301 | # |
| 4302 | |
| 4303 | use strict; |
| 4304 | use Win32::OLE; |
| 4305 | |
| 4306 | my $application = Win32::OLE->new("Excel.Application"); |
| 4307 | my $workbook = $application->Workbooks->Add; |
| 4308 | my $worksheet = $workbook->Worksheets(1); |
| 4309 | |
| 4310 | $application->{Visible} = 1; |
| 4311 | |
| 4312 | $worksheet->Range("L97:X97")->Select; |
| 4313 | $worksheet->Range("M97")->Activate; |
| 4314 | |
| 4315 | my $message = "Hold down Shift and Ctrl and click the ". |
| 4316 | "Chart Wizard icon on the toolbar.\n\n". |
| 4317 | "Use the mouse motion and buttons to control ". |
| 4318 | "movement. Try to find the monolith. ". |
| 4319 | "Close this dialog first."; |
| 4320 | |
| 4321 | $application->InputBox($message); |
| 4322 | |
| 4323 | |
| 4324 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/easter_egg.pl> |
| 4325 | |
| 4326 | =head2 Example: filehandle.pl |
| 4327 | |
| 4328 | |
| 4329 | |
| 4330 | Example of using Spreadsheet::WriteExcel to write Excel files to |
| 4331 | different filehandles. |
| 4332 | |
| 4333 | |
| 4334 | |
| 4335 | =begin html |
| 4336 | |
| 4337 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/filehandle.jpg" width="640" height="420" alt="Output from filehandle.pl" /></center></p> |
| 4338 | |
| 4339 | =end html |
| 4340 | |
| 4341 | Source code for this example: |
| 4342 | |
| 4343 | #!/usr/bin/perl -w |
| 4344 | |
| 4345 | ############################################################################### |
| 4346 | # |
| 4347 | # Example of using Spreadsheet::WriteExcel to write Excel files to |
| 4348 | # different filehandles. |
| 4349 | # |
| 4350 | # reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org |
| 4351 | # |
| 4352 | |
| 4353 | use strict; |
| 4354 | use Spreadsheet::WriteExcel; |
| 4355 | use IO::Scalar; |
| 4356 | |
| 4357 | |
| 4358 | |
| 4359 | |
| 4360 | ############################################################################### |
| 4361 | # |
| 4362 | # Example 1. This demonstrates the standard way of creating an Excel file by |
| 4363 | # specifying a file name. |
| 4364 | # |
| 4365 | |
| 4366 | my $workbook1 = Spreadsheet::WriteExcel->new('fh_01.xls'); |
| 4367 | my $worksheet1 = $workbook1->add_worksheet(); |
| 4368 | |
| 4369 | $worksheet1->write(0, 0, "Hi Excel!"); |
| 4370 | |
| 4371 | |
| 4372 | |
| 4373 | |
| 4374 | ############################################################################### |
| 4375 | # |
| 4376 | # Example 2. Write an Excel file to an existing filehandle. |
| 4377 | # |
| 4378 | |
| 4379 | open TEST, "> fh_02.xls" or die "Couldn't open file: $!"; |
| 4380 | binmode TEST; # Always do this regardless of whether the platform requires it. |
| 4381 | |
| 4382 | my $workbook2 = Spreadsheet::WriteExcel->new(\*TEST); |
| 4383 | my $worksheet2 = $workbook2->add_worksheet(); |
| 4384 | |
| 4385 | $worksheet2->write(0, 0, "Hi Excel!"); |
| 4386 | |
| 4387 | |
| 4388 | |
| 4389 | |
| 4390 | ############################################################################### |
| 4391 | # |
| 4392 | # Example 3. Write an Excel file to an existing OO style filehandle. |
| 4393 | # |
| 4394 | |
| 4395 | my $fh = FileHandle->new("> fh_03.xls") |
| 4396 | or die "Couldn't open file: $!"; |
| 4397 | |
| 4398 | binmode($fh); |
| 4399 | |
| 4400 | my $workbook3 = Spreadsheet::WriteExcel->new($fh); |
| 4401 | my $worksheet3 = $workbook3->add_worksheet(); |
| 4402 | |
| 4403 | $worksheet3->write(0, 0, "Hi Excel!"); |
| 4404 | |
| 4405 | |
| 4406 | |
| 4407 | |
| 4408 | ############################################################################### |
| 4409 | # |
| 4410 | # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to |
| 4411 | # the IO::Scalar documentation for further details. |
| 4412 | # |
| 4413 | |
| 4414 | my $xls_str; |
| 4415 | |
| 4416 | tie *XLS, 'IO::Scalar', \$xls_str; |
| 4417 | |
| 4418 | my $workbook4 = Spreadsheet::WriteExcel->new(\*XLS); |
| 4419 | my $worksheet4 = $workbook4->add_worksheet(); |
| 4420 | |
| 4421 | $worksheet4->write(0, 0, "Hi Excel 4"); |
| 4422 | $workbook4->close(); # This is required before we use the scalar |
| 4423 | |
| 4424 | |
| 4425 | # The Excel file is now in $xls_str. As a demonstration, print it to a file. |
| 4426 | open TMP, "> fh_04.xls" or die "Couldn't open file: $!"; |
| 4427 | binmode TMP; |
| 4428 | print TMP $xls_str; |
| 4429 | close TMP; |
| 4430 | |
| 4431 | |
| 4432 | |
| 4433 | |
| 4434 | ############################################################################### |
| 4435 | # |
| 4436 | # Example 5. Write an Excel file to a string via IO::Scalar's newer interface. |
| 4437 | # Please refer to the IO::Scalar documentation for further details. |
| 4438 | # |
| 4439 | my $xls_str2; |
| 4440 | |
| 4441 | my $fh5 = IO::Scalar->new(\$xls_str2); |
| 4442 | |
| 4443 | |
| 4444 | my $workbook5 = Spreadsheet::WriteExcel->new($fh5); |
| 4445 | my $worksheet5 = $workbook5->add_worksheet(); |
| 4446 | |
| 4447 | $worksheet5->write(0, 0, "Hi Excel 5"); |
| 4448 | $workbook5->close(); # This is required before we use the scalar |
| 4449 | |
| 4450 | # The Excel file is now in $xls_str. As a demonstration, print it to a file. |
| 4451 | open TMP, "> fh_05.xls" or die "Couldn't open file: $!"; |
| 4452 | binmode TMP; |
| 4453 | print TMP $xls_str2; |
| 4454 | close TMP; |
| 4455 | |
| 4456 | |
| 4457 | |
| 4458 | |
| 4459 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/filehandle.pl> |
| 4460 | |
| 4461 | =head2 Example: formula_result.pl |
| 4462 | |
| 4463 | |
| 4464 | |
| 4465 | Example of how to write Spreadsheet::WriteExcel formulas with a user |
| 4466 | specified result. |
| 4467 | |
| 4468 | This is generally only required when writing a spreadsheet for an |
| 4469 | application other than Excel where the formula isn't evaluated. |
| 4470 | |
| 4471 | |
| 4472 | |
| 4473 | =begin html |
| 4474 | |
| 4475 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/formula_result.jpg" width="640" height="420" alt="Output from formula_result.pl" /></center></p> |
| 4476 | |
| 4477 | =end html |
| 4478 | |
| 4479 | Source code for this example: |
| 4480 | |
| 4481 | #!/usr/bin/perl -w |
| 4482 | |
| 4483 | ####################################################################### |
| 4484 | # |
| 4485 | # Example of how to write Spreadsheet::WriteExcel formulas with a user |
| 4486 | # specified result. |
| 4487 | # |
| 4488 | # This is generally only required when writing a spreadsheet for an |
| 4489 | # application other than Excel where the formula isn't evaluated. |
| 4490 | # |
| 4491 | # reverse('©'), August 2005, John McNamara, jmcnamara@cpan.org |
| 4492 | # |
| 4493 | |
| 4494 | use strict; |
| 4495 | use Spreadsheet::WriteExcel; |
| 4496 | |
| 4497 | my $workbook = Spreadsheet::WriteExcel->new('formula_result.xls'); |
| 4498 | my $worksheet = $workbook->add_worksheet(); |
| 4499 | my $format = $workbook->add_format(color => 'blue'); |
| 4500 | |
| 4501 | |
| 4502 | $worksheet->write('A1', '=1+2'); |
| 4503 | $worksheet->write('A2', '=1+2', $format, 4); |
| 4504 | $worksheet->write('A3', '="ABC"', undef, 'DEF'); |
| 4505 | $worksheet->write('A4', '=IF(A1 > 1, TRUE, FALSE)', undef, 'TRUE'); |
| 4506 | $worksheet->write('A5', '=1/0', undef, '#DIV/0!'); |
| 4507 | |
| 4508 | |
| 4509 | __END__ |
| 4510 | |
| 4511 | |
| 4512 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/formula_result.pl> |
| 4513 | |
| 4514 | =head2 Example: headers.pl |
| 4515 | |
| 4516 | |
| 4517 | |
| 4518 | This program shows several examples of how to set up headers and |
| 4519 | footers with Spreadsheet::WriteExcel. |
| 4520 | |
| 4521 | The control characters used in the header/footer strings are: |
| 4522 | |
| 4523 | Control Category Description |
| 4524 | ======= ======== =========== |
| 4525 | &L Justification Left |
| 4526 | &C Center |
| 4527 | &R Right |
| 4528 | |
| 4529 | &P Information Page number |
| 4530 | &N Total number of pages |
| 4531 | &D Date |
| 4532 | &T Time |
| 4533 | &F File name |
| 4534 | &A Worksheet name |
| 4535 | |
| 4536 | &fontsize Font Font size |
| 4537 | &"font,style" Font name and style |
| 4538 | &U Single underline |
| 4539 | &E Double underline |
| 4540 | &S Strikethrough |
| 4541 | &X Superscript |
| 4542 | &Y Subscript |
| 4543 | |
| 4544 | && Miscellaneous Literal ampersand & |
| 4545 | |
| 4546 | See the main Spreadsheet::WriteExcel documentation for more information. |
| 4547 | |
| 4548 | |
| 4549 | |
| 4550 | =begin html |
| 4551 | |
| 4552 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/headers.jpg" width="640" height="420" alt="Output from headers.pl" /></center></p> |
| 4553 | |
| 4554 | =end html |
| 4555 | |
| 4556 | Source code for this example: |
| 4557 | |
| 4558 | #!/usr/bin/perl -w |
| 4559 | |
| 4560 | ###################################################################### |
| 4561 | # |
| 4562 | # This program shows several examples of how to set up headers and |
| 4563 | # footers with Spreadsheet::WriteExcel. |
| 4564 | # |
| 4565 | # The control characters used in the header/footer strings are: |
| 4566 | # |
| 4567 | # Control Category Description |
| 4568 | # ======= ======== =========== |
| 4569 | # &L Justification Left |
| 4570 | # &C Center |
| 4571 | # &R Right |
| 4572 | # |
| 4573 | # &P Information Page number |
| 4574 | # &N Total number of pages |
| 4575 | # &D Date |
| 4576 | # &T Time |
| 4577 | # &F File name |
| 4578 | # &A Worksheet name |
| 4579 | # |
| 4580 | # &fontsize Font Font size |
| 4581 | # &"font,style" Font name and style |
| 4582 | # &U Single underline |
| 4583 | # &E Double underline |
| 4584 | # &S Strikethrough |
| 4585 | # &X Superscript |
| 4586 | # &Y Subscript |
| 4587 | # |
| 4588 | # && Miscellaneous Literal ampersand & |
| 4589 | # |
| 4590 | # See the main Spreadsheet::WriteExcel documentation for more information. |
| 4591 | # |
| 4592 | # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org |
| 4593 | # |
| 4594 | |
| 4595 | |
| 4596 | use strict; |
| 4597 | use Spreadsheet::WriteExcel; |
| 4598 | |
| 4599 | my $workbook = Spreadsheet::WriteExcel->new("headers.xls"); |
| 4600 | my $preview = "Select Print Preview to see the header and footer"; |
| 4601 | |
| 4602 | |
| 4603 | ###################################################################### |
| 4604 | # |
| 4605 | # A simple example to start |
| 4606 | # |
| 4607 | my $worksheet1 = $workbook->add_worksheet('Simple'); |
| 4608 | |
| 4609 | my $header1 = '&CHere is some centred text.'; |
| 4610 | |
| 4611 | my $footer1 = '&LHere is some left aligned text.'; |
| 4612 | |
| 4613 | |
| 4614 | $worksheet1->set_header($header1); |
| 4615 | $worksheet1->set_footer($footer1); |
| 4616 | |
| 4617 | $worksheet1->set_column('A:A', 50); |
| 4618 | $worksheet1->write('A1', $preview); |
| 4619 | |
| 4620 | |
| 4621 | |
| 4622 | |
| 4623 | ###################################################################### |
| 4624 | # |
| 4625 | # This is an example of some of the header/footer variables. |
| 4626 | # |
| 4627 | my $worksheet2 = $workbook->add_worksheet('Variables'); |
| 4628 | |
| 4629 | my $header2 = '&LPage &P of &N'. |
| 4630 | '&CFilename: &F' . |
| 4631 | '&RSheetname: &A'; |
| 4632 | |
| 4633 | my $footer2 = '&LCurrent date: &D'. |
| 4634 | '&RCurrent time: &T'; |
| 4635 | |
| 4636 | |
| 4637 | |
| 4638 | $worksheet2->set_header($header2); |
| 4639 | $worksheet2->set_footer($footer2); |
| 4640 | |
| 4641 | |
| 4642 | $worksheet2->set_column('A:A', 50); |
| 4643 | $worksheet2->write('A1', $preview); |
| 4644 | $worksheet2->write('A21', "Next sheet"); |
| 4645 | $worksheet2->set_h_pagebreaks(20); |
| 4646 | |
| 4647 | |
| 4648 | |
| 4649 | ###################################################################### |
| 4650 | # |
| 4651 | # This example shows how to use more than one font |
| 4652 | # |
| 4653 | my $worksheet3 = $workbook->add_worksheet('Mixed fonts'); |
| 4654 | |
| 4655 | my $header3 = '&C' . |
| 4656 | '&"Courier New,Bold"Hello ' . |
| 4657 | '&"Arial,Italic"World'; |
| 4658 | |
| 4659 | my $footer3 = '&C' . |
| 4660 | '&"Symbol"e' . |
| 4661 | '&"Arial" = mc&X2'; |
| 4662 | |
| 4663 | $worksheet3->set_header($header3); |
| 4664 | $worksheet3->set_footer($footer3); |
| 4665 | |
| 4666 | $worksheet3->set_column('A:A', 50); |
| 4667 | $worksheet3->write('A1', $preview); |
| 4668 | |
| 4669 | |
| 4670 | |
| 4671 | |
| 4672 | ###################################################################### |
| 4673 | # |
| 4674 | # Example of line wrapping |
| 4675 | # |
| 4676 | my $worksheet4 = $workbook->add_worksheet('Word wrap'); |
| 4677 | |
| 4678 | my $header4 = "&CHeading 1\nHeading 2\nHeading 3"; |
| 4679 | |
| 4680 | $worksheet4->set_header($header4); |
| 4681 | |
| 4682 | $worksheet4->set_column('A:A', 50); |
| 4683 | $worksheet4->write('A1', $preview); |
| 4684 | |
| 4685 | |
| 4686 | |
| 4687 | |
| 4688 | ###################################################################### |
| 4689 | # |
| 4690 | # Example of inserting a literal ampersand & |
| 4691 | # |
| 4692 | my $worksheet5 = $workbook->add_worksheet('Ampersand'); |
| 4693 | |
| 4694 | my $header5 = "&CCuriouser && Curiouser - Attorneys at Law"; |
| 4695 | |
| 4696 | $worksheet5->set_header($header5); |
| 4697 | |
| 4698 | $worksheet5->set_column('A:A', 50); |
| 4699 | $worksheet5->write('A1', $preview); |
| 4700 | |
| 4701 | |
| 4702 | |
| 4703 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/headers.pl> |
| 4704 | |
| 4705 | =head2 Example: hide_sheet.pl |
| 4706 | |
| 4707 | |
| 4708 | |
| 4709 | Example of how to hide a worksheet with Spreadsheet::WriteExcel. |
| 4710 | |
| 4711 | |
| 4712 | |
| 4713 | =begin html |
| 4714 | |
| 4715 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/hide_sheet.jpg" width="640" height="420" alt="Output from hide_sheet.pl" /></center></p> |
| 4716 | |
| 4717 | =end html |
| 4718 | |
| 4719 | Source code for this example: |
| 4720 | |
| 4721 | #!/usr/bin/perl -w |
| 4722 | |
| 4723 | ####################################################################### |
| 4724 | # |
| 4725 | # Example of how to hide a worksheet with Spreadsheet::WriteExcel. |
| 4726 | # |
| 4727 | # reverse('©'), April 2005, John McNamara, jmcnamara@cpan.org |
| 4728 | # |
| 4729 | |
| 4730 | use strict; |
| 4731 | use Spreadsheet::WriteExcel; |
| 4732 | |
| 4733 | my $workbook = Spreadsheet::WriteExcel->new('hidden.xls'); |
| 4734 | my $worksheet1 = $workbook->add_worksheet(); |
| 4735 | my $worksheet2 = $workbook->add_worksheet(); |
| 4736 | my $worksheet3 = $workbook->add_worksheet(); |
| 4737 | |
| 4738 | # Sheet2 won't be visible until it is unhidden in Excel. |
| 4739 | $worksheet2->hide(); |
| 4740 | |
| 4741 | $worksheet1->write(0, 0, 'Sheet2 is hidden'); |
| 4742 | $worksheet2->write(0, 0, 'How did you find me?'); |
| 4743 | $worksheet3->write(0, 0, 'Sheet2 is hidden'); |
| 4744 | |
| 4745 | |
| 4746 | __END__ |
| 4747 | |
| 4748 | |
| 4749 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hide_sheet.pl> |
| 4750 | |
| 4751 | =head2 Example: hyperlink1.pl |
| 4752 | |
| 4753 | |
| 4754 | |
| 4755 | Example of how to use the WriteExcel module to write hyperlinks. |
| 4756 | |
| 4757 | See also hyperlink2.pl for worksheet URL examples. |
| 4758 | |
| 4759 | |
| 4760 | |
| 4761 | =begin html |
| 4762 | |
| 4763 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/hyperlink1.jpg" width="640" height="420" alt="Output from hyperlink1.pl" /></center></p> |
| 4764 | |
| 4765 | =end html |
| 4766 | |
| 4767 | Source code for this example: |
| 4768 | |
| 4769 | #!/usr/bin/perl -w |
| 4770 | |
| 4771 | ############################################################################### |
| 4772 | # |
| 4773 | # Example of how to use the WriteExcel module to write hyperlinks. |
| 4774 | # |
| 4775 | # See also hyperlink2.pl for worksheet URL examples. |
| 4776 | # |
| 4777 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 4778 | # |
| 4779 | |
| 4780 | use strict; |
| 4781 | use Spreadsheet::WriteExcel; |
| 4782 | |
| 4783 | # Create a new workbook and add a worksheet |
| 4784 | my $workbook = Spreadsheet::WriteExcel->new("hyperlink.xls"); |
| 4785 | my $worksheet = $workbook->add_worksheet('Hyperlinks'); |
| 4786 | |
| 4787 | # Format the first column |
| 4788 | $worksheet->set_column('A:A', 30); |
| 4789 | $worksheet->set_selection('B1'); |
| 4790 | |
| 4791 | |
| 4792 | # Add a sample format |
| 4793 | my $format = $workbook->add_format(); |
| 4794 | $format->set_size(12); |
| 4795 | $format->set_bold(); |
| 4796 | $format->set_color('red'); |
| 4797 | $format->set_underline(); |
| 4798 | |
| 4799 | |
| 4800 | # Write some hyperlinks |
| 4801 | $worksheet->write('A1', 'http://www.perl.com/' ); |
| 4802 | $worksheet->write('A3', 'http://www.perl.com/', 'Perl home' ); |
| 4803 | $worksheet->write('A5', 'http://www.perl.com/', undef, $format); |
| 4804 | $worksheet->write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me'); |
| 4805 | |
| 4806 | # Write a URL that isn't a hyperlink |
| 4807 | $worksheet->write_string('A9', 'http://www.perl.com/'); |
| 4808 | |
| 4809 | |
| 4810 | |
| 4811 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hyperlink1.pl> |
| 4812 | |
| 4813 | =head2 Example: hyperlink2.pl |
| 4814 | |
| 4815 | |
| 4816 | |
| 4817 | Example of how to use the WriteExcel module to write internal and internal |
| 4818 | hyperlinks. |
| 4819 | |
| 4820 | If you wish to run this program and follow the hyperlinks you should create |
| 4821 | the following directory structure: |
| 4822 | |
| 4823 | C:\ -- Temp --+-- Europe |
| 4824 | | |
| 4825 | \-- Asia |
| 4826 | |
| 4827 | |
| 4828 | See also hyperlink1.pl for web URL examples. |
| 4829 | |
| 4830 | |
| 4831 | |
| 4832 | #!/usr/bin/perl -w |
| 4833 | |
| 4834 | ############################################################################### |
| 4835 | # |
| 4836 | # Example of how to use the WriteExcel module to write internal and internal |
| 4837 | # hyperlinks. |
| 4838 | # |
| 4839 | # If you wish to run this program and follow the hyperlinks you should create |
| 4840 | # the following directory structure: |
| 4841 | # |
| 4842 | # C:\ -- Temp --+-- Europe |
| 4843 | # | |
| 4844 | # \-- Asia |
| 4845 | # |
| 4846 | # |
| 4847 | # See also hyperlink1.pl for web URL examples. |
| 4848 | # |
| 4849 | # reverse('©'), February 2002, John McNamara, jmcnamara@cpan.org |
| 4850 | # |
| 4851 | |
| 4852 | |
| 4853 | use strict; |
| 4854 | use Spreadsheet::WriteExcel; |
| 4855 | |
| 4856 | # Create three workbooks: |
| 4857 | # C:\Temp\Europe\Ireland.xls |
| 4858 | # C:\Temp\Europe\Italy.xls |
| 4859 | # C:\Temp\Asia\China.xls |
| 4860 | # |
| 4861 | my $ireland = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Ireland.xls'); |
| 4862 | my $ire_links = $ireland->add_worksheet('Links'); |
| 4863 | my $ire_sales = $ireland->add_worksheet('Sales'); |
| 4864 | my $ire_data = $ireland->add_worksheet('Product Data'); |
| 4865 | |
| 4866 | my $italy = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Italy.xls'); |
| 4867 | my $ita_links = $italy->add_worksheet('Links'); |
| 4868 | my $ita_sales = $italy->add_worksheet('Sales'); |
| 4869 | my $ita_data = $italy->add_worksheet('Product Data'); |
| 4870 | |
| 4871 | my $china = Spreadsheet::WriteExcel->new('C:\Temp\Asia\China.xls'); |
| 4872 | my $cha_links = $china->add_worksheet('Links'); |
| 4873 | my $cha_sales = $china->add_worksheet('Sales'); |
| 4874 | my $cha_data = $china->add_worksheet('Product Data'); |
| 4875 | |
| 4876 | # Add a format |
| 4877 | my $format = $ireland->add_format(color => 'green', bold => 1); |
| 4878 | $ire_links->set_column('A:B', 25); |
| 4879 | |
| 4880 | |
| 4881 | ############################################################################### |
| 4882 | # |
| 4883 | # Examples of internal links |
| 4884 | # |
| 4885 | $ire_links->write('A1', 'Internal links', $format); |
| 4886 | |
| 4887 | # Internal link |
| 4888 | $ire_links->write('A2', 'internal:Sales!A2'); |
| 4889 | |
| 4890 | # Internal link to a range |
| 4891 | $ire_links->write('A3', 'internal:Sales!A3:D3'); |
| 4892 | |
| 4893 | # Internal link with an alternative string |
| 4894 | $ire_links->write('A4', 'internal:Sales!A4', 'Link'); |
| 4895 | |
| 4896 | # Internal link with a format |
| 4897 | $ire_links->write('A5', 'internal:Sales!A5', $format); |
| 4898 | |
| 4899 | # Internal link with an alternative string and format |
| 4900 | $ire_links->write('A6', 'internal:Sales!A6', 'Link', $format); |
| 4901 | |
| 4902 | # Internal link (spaces in worksheet name) |
| 4903 | $ire_links->write('A7', q{internal:'Product Data'!A7}); |
| 4904 | |
| 4905 | |
| 4906 | ############################################################################### |
| 4907 | # |
| 4908 | # Examples of external links |
| 4909 | # |
| 4910 | $ire_links->write('B1', 'External links', $format); |
| 4911 | |
| 4912 | # External link to a local file |
| 4913 | $ire_links->write('B2', 'external:Italy.xls'); |
| 4914 | |
| 4915 | # External link to a local file with worksheet |
| 4916 | $ire_links->write('B3', 'external:Italy.xls#Sales!B3'); |
| 4917 | |
| 4918 | # External link to a local file with worksheet and alternative string |
| 4919 | $ire_links->write('B4', 'external:Italy.xls#Sales!B4', 'Link'); |
| 4920 | |
| 4921 | # External link to a local file with worksheet and format |
| 4922 | $ire_links->write('B5', 'external:Italy.xls#Sales!B5', $format); |
| 4923 | |
| 4924 | # External link to a remote file, absolute path |
| 4925 | $ire_links->write('B6', 'external:c:/Temp/Asia/China.xls'); |
| 4926 | |
| 4927 | # External link to a remote file, relative path |
| 4928 | $ire_links->write('B7', 'external:../Asia/China.xls'); |
| 4929 | |
| 4930 | # External link to a remote file with worksheet |
| 4931 | $ire_links->write('B8', 'external:c:/Temp/Asia/China.xls#Sales!B8'); |
| 4932 | |
| 4933 | # External link to a remote file with worksheet (with spaces in the name) |
| 4934 | $ire_links->write('B9', q{external:c:/Temp/Asia/China.xls#'Product Data'!B9}); |
| 4935 | |
| 4936 | |
| 4937 | ############################################################################### |
| 4938 | # |
| 4939 | # Some utility links to return to the main sheet |
| 4940 | # |
| 4941 | $ire_sales->write('A2', 'internal:Links!A2', 'Back'); |
| 4942 | $ire_sales->write('A3', 'internal:Links!A3', 'Back'); |
| 4943 | $ire_sales->write('A4', 'internal:Links!A4', 'Back'); |
| 4944 | $ire_sales->write('A5', 'internal:Links!A5', 'Back'); |
| 4945 | $ire_sales->write('A6', 'internal:Links!A6', 'Back'); |
| 4946 | $ire_data-> write('A7', 'internal:Links!A7', 'Back'); |
| 4947 | |
| 4948 | $ita_links->write('A1', 'external:Ireland.xls#Links!B2', 'Back'); |
| 4949 | $ita_sales->write('B3', 'external:Ireland.xls#Links!B3', 'Back'); |
| 4950 | $ita_sales->write('B4', 'external:Ireland.xls#Links!B4', 'Back'); |
| 4951 | $ita_sales->write('B5', 'external:Ireland.xls#Links!B5', 'Back'); |
| 4952 | $cha_links->write('A1', 'external:../Europe/Ireland.xls#Links!B6', 'Back'); |
| 4953 | $cha_sales->write('B8', 'external:../Europe/Ireland.xls#Links!B8', 'Back'); |
| 4954 | $cha_data-> write('B9', 'external:../Europe/Ireland.xls#Links!B9', 'Back'); |
| 4955 | |
| 4956 | |
| 4957 | |
| 4958 | |
| 4959 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hyperlink2.pl> |
| 4960 | |
| 4961 | =head2 Example: images.pl |
| 4962 | |
| 4963 | |
| 4964 | |
| 4965 | Example of how to insert images into an Excel worksheet using the |
| 4966 | Spreadsheet::WriteExcel insert_image() method. |
| 4967 | |
| 4968 | |
| 4969 | |
| 4970 | =begin html |
| 4971 | |
| 4972 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/images.jpg" width="640" height="420" alt="Output from images.pl" /></center></p> |
| 4973 | |
| 4974 | =end html |
| 4975 | |
| 4976 | Source code for this example: |
| 4977 | |
| 4978 | #!/usr/bin/perl -w |
| 4979 | |
| 4980 | ####################################################################### |
| 4981 | # |
| 4982 | # Example of how to insert images into an Excel worksheet using the |
| 4983 | # Spreadsheet::WriteExcel insert_image() method. |
| 4984 | # |
| 4985 | # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org |
| 4986 | # |
| 4987 | |
| 4988 | use strict; |
| 4989 | use Spreadsheet::WriteExcel; |
| 4990 | |
| 4991 | # Create a new workbook called simple.xls and add a worksheet |
| 4992 | my $workbook = Spreadsheet::WriteExcel->new("images.xls"); |
| 4993 | my $worksheet1 = $workbook->add_worksheet('Image 1'); |
| 4994 | my $worksheet2 = $workbook->add_worksheet('Image 2'); |
| 4995 | my $worksheet3 = $workbook->add_worksheet('Image 3'); |
| 4996 | my $worksheet4 = $workbook->add_worksheet('Image 4'); |
| 4997 | |
| 4998 | # Insert a basic image |
| 4999 | $worksheet1->write('A10', "Image inserted into worksheet."); |
| 5000 | $worksheet1->insert_image('A1', 'republic.png'); |
| 5001 | |
| 5002 | |
| 5003 | # Insert an image with an offset |
| 5004 | $worksheet2->write('A10', "Image inserted with an offset."); |
| 5005 | $worksheet2->insert_image('A1', 'republic.png', 32, 10); |
| 5006 | |
| 5007 | # Insert a scaled image |
| 5008 | $worksheet3->write('A10', "Image scaled: width x 2, height x 0.8."); |
| 5009 | $worksheet3->insert_image('A1', 'republic.png', 0, 0, 2, 0.8); |
| 5010 | |
| 5011 | # Insert an image over varied column and row sizes |
| 5012 | # This does not require any additional work |
| 5013 | |
| 5014 | # Set the cols and row sizes |
| 5015 | # NOTE: you must do this before you call insert_image() |
| 5016 | $worksheet4->set_column('A:A', 5); |
| 5017 | $worksheet4->set_column('B:B', undef, undef, 1); # Hidden |
| 5018 | $worksheet4->set_column('C:D', 10); |
| 5019 | $worksheet4->set_row(0, 30); |
| 5020 | $worksheet4->set_row(3, 5); |
| 5021 | |
| 5022 | $worksheet4->write('A10', "Image inserted over scaled rows and columns."); |
| 5023 | $worksheet4->insert_image('A1', 'republic.png'); |
| 5024 | |
| 5025 | |
| 5026 | |
| 5027 | |
| 5028 | |
| 5029 | |
| 5030 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/images.pl> |
| 5031 | |
| 5032 | =head2 Example: indent.pl |
| 5033 | |
| 5034 | |
| 5035 | |
| 5036 | A simple formatting example using Spreadsheet::WriteExcel. |
| 5037 | |
| 5038 | This program demonstrates the indentation cell format. |
| 5039 | |
| 5040 | |
| 5041 | |
| 5042 | =begin html |
| 5043 | |
| 5044 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/indent.jpg" width="640" height="420" alt="Output from indent.pl" /></center></p> |
| 5045 | |
| 5046 | =end html |
| 5047 | |
| 5048 | Source code for this example: |
| 5049 | |
| 5050 | #!/usr/bin/perl -w |
| 5051 | |
| 5052 | ############################################################################## |
| 5053 | # |
| 5054 | # A simple formatting example using Spreadsheet::WriteExcel. |
| 5055 | # |
| 5056 | # This program demonstrates the indentation cell format. |
| 5057 | # |
| 5058 | # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org |
| 5059 | # |
| 5060 | |
| 5061 | |
| 5062 | use strict; |
| 5063 | use Spreadsheet::WriteExcel; |
| 5064 | |
| 5065 | my $workbook = Spreadsheet::WriteExcel->new('indent.xls'); |
| 5066 | |
| 5067 | my $worksheet = $workbook->add_worksheet(); |
| 5068 | my $indent1 = $workbook->add_format(indent => 1); |
| 5069 | my $indent2 = $workbook->add_format(indent => 2); |
| 5070 | |
| 5071 | $worksheet->set_column('A:A', 40); |
| 5072 | |
| 5073 | |
| 5074 | $worksheet->write('A1', "This text is indented 1 level", $indent1); |
| 5075 | $worksheet->write('A2', "This text is indented 2 levels", $indent2); |
| 5076 | |
| 5077 | |
| 5078 | __END__ |
| 5079 | |
| 5080 | |
| 5081 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/indent.pl> |
| 5082 | |
| 5083 | =head2 Example: merge1.pl |
| 5084 | |
| 5085 | |
| 5086 | |
| 5087 | Simple example of merging cells using the Spreadsheet::WriteExcel module. |
| 5088 | |
| 5089 | This example merges three cells using the "Centre Across Selection" |
| 5090 | alignment which was the Excel 5 method of achieving a merge. For a more |
| 5091 | modern approach use the merge_range() worksheet method instead. |
| 5092 | See the merge3.pl - merge6.pl programs. |
| 5093 | |
| 5094 | |
| 5095 | |
| 5096 | =begin html |
| 5097 | |
| 5098 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge1.jpg" width="640" height="420" alt="Output from merge1.pl" /></center></p> |
| 5099 | |
| 5100 | =end html |
| 5101 | |
| 5102 | Source code for this example: |
| 5103 | |
| 5104 | #!/usr/bin/perl -w |
| 5105 | |
| 5106 | ############################################################################### |
| 5107 | # |
| 5108 | # Simple example of merging cells using the Spreadsheet::WriteExcel module. |
| 5109 | # |
| 5110 | # This example merges three cells using the "Centre Across Selection" |
| 5111 | # alignment which was the Excel 5 method of achieving a merge. For a more |
| 5112 | # modern approach use the merge_range() worksheet method instead. |
| 5113 | # See the merge3.pl - merge6.pl programs. |
| 5114 | # |
| 5115 | # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org |
| 5116 | # |
| 5117 | |
| 5118 | use strict; |
| 5119 | use Spreadsheet::WriteExcel; |
| 5120 | |
| 5121 | # Create a new workbook and add a worksheet |
| 5122 | my $workbook = Spreadsheet::WriteExcel->new("merge1.xls"); |
| 5123 | my $worksheet = $workbook->add_worksheet(); |
| 5124 | |
| 5125 | |
| 5126 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5127 | $worksheet->set_column('B:D', 20); |
| 5128 | $worksheet->set_row(2, 30); |
| 5129 | |
| 5130 | |
| 5131 | # Create a merge format |
| 5132 | my $format = $workbook->add_format(center_across => 1); |
| 5133 | |
| 5134 | |
| 5135 | # Only one cell should contain text, the others should be blank. |
| 5136 | $worksheet->write (2, 1, "Center across selection", $format); |
| 5137 | $worksheet->write_blank(2, 2, $format); |
| 5138 | $worksheet->write_blank(2, 3, $format); |
| 5139 | |
| 5140 | |
| 5141 | |
| 5142 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge1.pl> |
| 5143 | |
| 5144 | =head2 Example: merge2.pl |
| 5145 | |
| 5146 | |
| 5147 | |
| 5148 | Simple example of merging cells using the Spreadsheet::WriteExcel module |
| 5149 | |
| 5150 | This example merges three cells using the "Centre Across Selection" |
| 5151 | alignment which was the Excel 5 method of achieving a merge. For a more |
| 5152 | modern approach use the merge_range() worksheet method instead. |
| 5153 | See the merge3.pl - merge6.pl programs. |
| 5154 | |
| 5155 | |
| 5156 | |
| 5157 | =begin html |
| 5158 | |
| 5159 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge2.jpg" width="640" height="420" alt="Output from merge2.pl" /></center></p> |
| 5160 | |
| 5161 | =end html |
| 5162 | |
| 5163 | Source code for this example: |
| 5164 | |
| 5165 | #!/usr/bin/perl -w |
| 5166 | |
| 5167 | ############################################################################### |
| 5168 | # |
| 5169 | # Simple example of merging cells using the Spreadsheet::WriteExcel module |
| 5170 | # |
| 5171 | # This example merges three cells using the "Centre Across Selection" |
| 5172 | # alignment which was the Excel 5 method of achieving a merge. For a more |
| 5173 | # modern approach use the merge_range() worksheet method instead. |
| 5174 | # See the merge3.pl - merge6.pl programs. |
| 5175 | # |
| 5176 | # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org |
| 5177 | # |
| 5178 | |
| 5179 | use strict; |
| 5180 | use Spreadsheet::WriteExcel; |
| 5181 | |
| 5182 | # Create a new workbook and add a worksheet |
| 5183 | my $workbook = Spreadsheet::WriteExcel->new("merge2.xls"); |
| 5184 | my $worksheet = $workbook->add_worksheet(); |
| 5185 | |
| 5186 | |
| 5187 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5188 | $worksheet->set_column(1, 2, 30); |
| 5189 | $worksheet->set_row(2, 40); |
| 5190 | |
| 5191 | |
| 5192 | # Create a merged format |
| 5193 | my $format = $workbook->add_format( |
| 5194 | center_across => 1, |
| 5195 | bold => 1, |
| 5196 | size => 15, |
| 5197 | pattern => 1, |
| 5198 | border => 6, |
| 5199 | color => 'white', |
| 5200 | fg_color => 'green', |
| 5201 | border_color => 'yellow', |
| 5202 | align => 'vcenter', |
| 5203 | ); |
| 5204 | |
| 5205 | |
| 5206 | # Only one cell should contain text, the others should be blank. |
| 5207 | $worksheet->write (2, 1, "Center across selection", $format); |
| 5208 | $worksheet->write_blank(2, 2, $format); |
| 5209 | |
| 5210 | |
| 5211 | |
| 5212 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge2.pl> |
| 5213 | |
| 5214 | =head2 Example: merge3.pl |
| 5215 | |
| 5216 | |
| 5217 | |
| 5218 | Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a |
| 5219 | merged cell. There are two options write_url_range() with a standard merge |
| 5220 | format or merge_range(). |
| 5221 | |
| 5222 | |
| 5223 | |
| 5224 | =begin html |
| 5225 | |
| 5226 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge3.jpg" width="640" height="420" alt="Output from merge3.pl" /></center></p> |
| 5227 | |
| 5228 | =end html |
| 5229 | |
| 5230 | Source code for this example: |
| 5231 | |
| 5232 | #!/usr/bin/perl -w |
| 5233 | |
| 5234 | ############################################################################### |
| 5235 | # |
| 5236 | # Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a |
| 5237 | # merged cell. There are two options write_url_range() with a standard merge |
| 5238 | # format or merge_range(). |
| 5239 | # |
| 5240 | # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org |
| 5241 | # |
| 5242 | |
| 5243 | use strict; |
| 5244 | use Spreadsheet::WriteExcel; |
| 5245 | |
| 5246 | # Create a new workbook and add a worksheet |
| 5247 | my $workbook = Spreadsheet::WriteExcel->new('merge3.xls'); |
| 5248 | my $worksheet = $workbook->add_worksheet(); |
| 5249 | |
| 5250 | |
| 5251 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5252 | $worksheet->set_row($_, 30) for (1, 3, 6, 7); |
| 5253 | $worksheet->set_column('B:D', 20); |
| 5254 | |
| 5255 | |
| 5256 | ############################################################################### |
| 5257 | # |
| 5258 | # Example 1: Merge cells containing a hyperlink using write_url_range() |
| 5259 | # and the standard Excel 5+ merge property. |
| 5260 | # |
| 5261 | my $format1 = $workbook->add_format( |
| 5262 | center_across => 1, |
| 5263 | border => 1, |
| 5264 | underline => 1, |
| 5265 | color => 'blue', |
| 5266 | ); |
| 5267 | |
| 5268 | # Write the cells to be merged |
| 5269 | $worksheet->write_url_range('B2:D2', 'http://www.perl.com', $format1); |
| 5270 | $worksheet->write_blank('C2', $format1); |
| 5271 | $worksheet->write_blank('D2', $format1); |
| 5272 | |
| 5273 | |
| 5274 | |
| 5275 | ############################################################################### |
| 5276 | # |
| 5277 | # Example 2: Merge cells containing a hyperlink using merge_range(). |
| 5278 | # |
| 5279 | my $format2 = $workbook->add_format( |
| 5280 | border => 1, |
| 5281 | underline => 1, |
| 5282 | color => 'blue', |
| 5283 | align => 'center', |
| 5284 | valign => 'vcenter', |
| 5285 | ); |
| 5286 | |
| 5287 | # Merge 3 cells |
| 5288 | $worksheet->merge_range('B4:D4', 'http://www.perl.com', $format2); |
| 5289 | |
| 5290 | |
| 5291 | # Merge 3 cells over two rows |
| 5292 | $worksheet->merge_range('B7:D8', 'http://www.perl.com', $format2); |
| 5293 | |
| 5294 | |
| 5295 | |
| 5296 | |
| 5297 | |
| 5298 | |
| 5299 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge3.pl> |
| 5300 | |
| 5301 | =head2 Example: merge4.pl |
| 5302 | |
| 5303 | |
| 5304 | |
| 5305 | Example of how to use the Spreadsheet::WriteExcel merge_range() workbook |
| 5306 | method with complex formatting. |
| 5307 | |
| 5308 | |
| 5309 | |
| 5310 | =begin html |
| 5311 | |
| 5312 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge4.jpg" width="640" height="420" alt="Output from merge4.pl" /></center></p> |
| 5313 | |
| 5314 | =end html |
| 5315 | |
| 5316 | Source code for this example: |
| 5317 | |
| 5318 | #!/usr/bin/perl -w |
| 5319 | |
| 5320 | ############################################################################### |
| 5321 | # |
| 5322 | # Example of how to use the Spreadsheet::WriteExcel merge_range() workbook |
| 5323 | # method with complex formatting. |
| 5324 | # |
| 5325 | # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org |
| 5326 | # |
| 5327 | |
| 5328 | use strict; |
| 5329 | use Spreadsheet::WriteExcel; |
| 5330 | |
| 5331 | # Create a new workbook and add a worksheet |
| 5332 | my $workbook = Spreadsheet::WriteExcel->new('merge4.xls'); |
| 5333 | my $worksheet = $workbook->add_worksheet(); |
| 5334 | |
| 5335 | |
| 5336 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5337 | $worksheet->set_row($_, 30) for (1..11); |
| 5338 | $worksheet->set_column('B:D', 20); |
| 5339 | |
| 5340 | |
| 5341 | ############################################################################### |
| 5342 | # |
| 5343 | # Example 1: Text centered vertically and horizontally |
| 5344 | # |
| 5345 | my $format1 = $workbook->add_format( |
| 5346 | border => 6, |
| 5347 | bold => 1, |
| 5348 | color => 'red', |
| 5349 | valign => 'vcenter', |
| 5350 | align => 'center', |
| 5351 | ); |
| 5352 | |
| 5353 | |
| 5354 | |
| 5355 | $worksheet->merge_range('B2:D3', 'Vertical and horizontal', $format1); |
| 5356 | |
| 5357 | |
| 5358 | ############################################################################### |
| 5359 | # |
| 5360 | # Example 2: Text aligned to the top and left |
| 5361 | # |
| 5362 | my $format2 = $workbook->add_format( |
| 5363 | border => 6, |
| 5364 | bold => 1, |
| 5365 | color => 'red', |
| 5366 | valign => 'top', |
| 5367 | align => 'left', |
| 5368 | ); |
| 5369 | |
| 5370 | |
| 5371 | |
| 5372 | $worksheet->merge_range('B5:D6', 'Aligned to the top and left', $format2); |
| 5373 | |
| 5374 | |
| 5375 | ############################################################################### |
| 5376 | # |
| 5377 | # Example 3: Text aligned to the bottom and right |
| 5378 | # |
| 5379 | my $format3 = $workbook->add_format( |
| 5380 | border => 6, |
| 5381 | bold => 1, |
| 5382 | color => 'red', |
| 5383 | valign => 'bottom', |
| 5384 | align => 'right', |
| 5385 | ); |
| 5386 | |
| 5387 | |
| 5388 | |
| 5389 | $worksheet->merge_range('B8:D9', 'Aligned to the bottom and right', $format3); |
| 5390 | |
| 5391 | |
| 5392 | ############################################################################### |
| 5393 | # |
| 5394 | # Example 4: Text justified (i.e. wrapped) in the cell |
| 5395 | # |
| 5396 | my $format4 = $workbook->add_format( |
| 5397 | border => 6, |
| 5398 | bold => 1, |
| 5399 | color => 'red', |
| 5400 | valign => 'top', |
| 5401 | align => 'justify', |
| 5402 | ); |
| 5403 | |
| 5404 | |
| 5405 | |
| 5406 | $worksheet->merge_range('B11:D12', 'Justified: '.'so on and ' x18, $format4); |
| 5407 | |
| 5408 | |
| 5409 | |
| 5410 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge4.pl> |
| 5411 | |
| 5412 | =head2 Example: merge5.pl |
| 5413 | |
| 5414 | |
| 5415 | |
| 5416 | Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook |
| 5417 | method with complex formatting and rotation. |
| 5418 | |
| 5419 | |
| 5420 | |
| 5421 | |
| 5422 | =begin html |
| 5423 | |
| 5424 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge5.jpg" width="640" height="420" alt="Output from merge5.pl" /></center></p> |
| 5425 | |
| 5426 | =end html |
| 5427 | |
| 5428 | Source code for this example: |
| 5429 | |
| 5430 | #!/usr/bin/perl -w |
| 5431 | |
| 5432 | ############################################################################### |
| 5433 | # |
| 5434 | # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook |
| 5435 | # method with complex formatting and rotation. |
| 5436 | # |
| 5437 | # |
| 5438 | # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org |
| 5439 | # |
| 5440 | |
| 5441 | use strict; |
| 5442 | use Spreadsheet::WriteExcel; |
| 5443 | |
| 5444 | # Create a new workbook and add a worksheet |
| 5445 | my $workbook = Spreadsheet::WriteExcel->new('merge5.xls'); |
| 5446 | my $worksheet = $workbook->add_worksheet(); |
| 5447 | |
| 5448 | |
| 5449 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5450 | $worksheet->set_row($_, 36) for (3..8); |
| 5451 | $worksheet->set_column($_, $_ , 15) for (1,3,5); |
| 5452 | |
| 5453 | |
| 5454 | ############################################################################### |
| 5455 | # |
| 5456 | # Rotation 1, letters run from top to bottom |
| 5457 | # |
| 5458 | my $format1 = $workbook->add_format( |
| 5459 | border => 6, |
| 5460 | bold => 1, |
| 5461 | color => 'red', |
| 5462 | valign => 'vcentre', |
| 5463 | align => 'centre', |
| 5464 | rotation => 270, |
| 5465 | ); |
| 5466 | |
| 5467 | |
| 5468 | $worksheet->merge_range('B4:B9', 'Rotation 270', $format1); |
| 5469 | |
| 5470 | |
| 5471 | ############################################################################### |
| 5472 | # |
| 5473 | # Rotation 2, 90° anticlockwise |
| 5474 | # |
| 5475 | my $format2 = $workbook->add_format( |
| 5476 | border => 6, |
| 5477 | bold => 1, |
| 5478 | color => 'red', |
| 5479 | valign => 'vcentre', |
| 5480 | align => 'centre', |
| 5481 | rotation => 90, |
| 5482 | ); |
| 5483 | |
| 5484 | |
| 5485 | $worksheet->merge_range('D4:D9', 'Rotation 90°', $format2); |
| 5486 | |
| 5487 | |
| 5488 | |
| 5489 | ############################################################################### |
| 5490 | # |
| 5491 | # Rotation 3, 90° clockwise |
| 5492 | # |
| 5493 | my $format3 = $workbook->add_format( |
| 5494 | border => 6, |
| 5495 | bold => 1, |
| 5496 | color => 'red', |
| 5497 | valign => 'vcentre', |
| 5498 | align => 'centre', |
| 5499 | rotation => -90, |
| 5500 | ); |
| 5501 | |
| 5502 | |
| 5503 | $worksheet->merge_range('F4:F9', 'Rotation -90°', $format3); |
| 5504 | |
| 5505 | |
| 5506 | |
| 5507 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge5.pl> |
| 5508 | |
| 5509 | =head2 Example: merge6.pl |
| 5510 | |
| 5511 | |
| 5512 | |
| 5513 | Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook |
| 5514 | method with Unicode strings. |
| 5515 | |
| 5516 | |
| 5517 | |
| 5518 | |
| 5519 | =begin html |
| 5520 | |
| 5521 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge6.jpg" width="640" height="420" alt="Output from merge6.pl" /></center></p> |
| 5522 | |
| 5523 | =end html |
| 5524 | |
| 5525 | Source code for this example: |
| 5526 | |
| 5527 | #!/usr/bin/perl -w |
| 5528 | |
| 5529 | ############################################################################### |
| 5530 | # |
| 5531 | # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook |
| 5532 | # method with Unicode strings. |
| 5533 | # |
| 5534 | # |
| 5535 | # reverse('©'), December 2005, John McNamara, jmcnamara@cpan.org |
| 5536 | # |
| 5537 | |
| 5538 | use strict; |
| 5539 | use Spreadsheet::WriteExcel; |
| 5540 | |
| 5541 | # Create a new workbook and add a worksheet |
| 5542 | my $workbook = Spreadsheet::WriteExcel->new('merge6.xls'); |
| 5543 | my $worksheet = $workbook->add_worksheet(); |
| 5544 | |
| 5545 | |
| 5546 | # Increase the cell size of the merged cells to highlight the formatting. |
| 5547 | $worksheet->set_row($_, 36) for 2..9; |
| 5548 | $worksheet->set_column('B:D', 25); |
| 5549 | |
| 5550 | |
| 5551 | # Format for the merged cells. |
| 5552 | my $format = $workbook->add_format( |
| 5553 | border => 6, |
| 5554 | bold => 1, |
| 5555 | color => 'red', |
| 5556 | size => 20, |
| 5557 | valign => 'vcentre', |
| 5558 | align => 'left', |
| 5559 | indent => 1, |
| 5560 | ); |
| 5561 | |
| 5562 | |
| 5563 | |
| 5564 | |
| 5565 | ############################################################################### |
| 5566 | # |
| 5567 | # Write an Ascii string. |
| 5568 | # |
| 5569 | |
| 5570 | $worksheet->merge_range('B3:D4', 'ASCII: A simple string', $format); |
| 5571 | |
| 5572 | |
| 5573 | |
| 5574 | |
| 5575 | ############################################################################### |
| 5576 | # |
| 5577 | # Write a UTF-16 Unicode string. |
| 5578 | # |
| 5579 | |
| 5580 | # A phrase in Cyrillic encoded as UTF-16BE. |
| 5581 | my $utf16_str = pack "H*", '005500540046002d00310036003a0020'. |
| 5582 | '042d0442043e002004440440043004370430002004'. |
| 5583 | '3d043000200440044304410441043a043e043c0021'; |
| 5584 | |
| 5585 | # Note the extra parameter at the end to indicate UTF-16 encoding. |
| 5586 | $worksheet->merge_range('B6:D7', $utf16_str, $format, 1); |
| 5587 | |
| 5588 | |
| 5589 | |
| 5590 | |
| 5591 | ############################################################################### |
| 5592 | # |
| 5593 | # Write a UTF-8 Unicode string. |
| 5594 | # |
| 5595 | |
| 5596 | if ($] >= 5.008) { |
| 5597 | my $smiley = chr 0x263a; |
| 5598 | $worksheet->merge_range('B9:D10', "UTF-8: A Unicode smiley $smiley", |
| 5599 | $format); |
| 5600 | } |
| 5601 | else { |
| 5602 | $worksheet->merge_range('B9:D10', "UTF-8: Requires Perl 5.8", $format); |
| 5603 | } |
| 5604 | |
| 5605 | |
| 5606 | |
| 5607 | |
| 5608 | __END__ |
| 5609 | |
| 5610 | |
| 5611 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge6.pl> |
| 5612 | |
| 5613 | =head2 Example: mod_perl1.pl |
| 5614 | |
| 5615 | |
| 5616 | |
| 5617 | Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 5618 | file to a browser using mod_perl 1 and Apache |
| 5619 | |
| 5620 | This module ties *XLS directly to Apache, and with the correct |
| 5621 | content-disposition/types it will prompt the user to save |
| 5622 | the file, or open it at this location. |
| 5623 | |
| 5624 | This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. |
| 5625 | |
| 5626 | Change the name of this file to Cgi.pm. |
| 5627 | Change the package location to where ever you locate this package. |
| 5628 | In the example below it is located in the WriteExcel directory. |
| 5629 | |
| 5630 | Your httpd.conf entry for this module, should you choose to use it |
| 5631 | as a stand alone app, should look similar to the following: |
| 5632 | |
| 5633 | <Location /spreadsheet-test> |
| 5634 | SetHandler perl-script |
| 5635 | PerlHandler Spreadsheet::WriteExcel::Cgi |
| 5636 | PerlSendHeader On |
| 5637 | </Location> |
| 5638 | |
| 5639 | The PerlHandler name above and the package name below *have* to match. |
| 5640 | |
| 5641 | ############################################################################### |
| 5642 | # |
| 5643 | # Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 5644 | # file to a browser using mod_perl 1 and Apache |
| 5645 | # |
| 5646 | # This module ties *XLS directly to Apache, and with the correct |
| 5647 | # content-disposition/types it will prompt the user to save |
| 5648 | # the file, or open it at this location. |
| 5649 | # |
| 5650 | # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. |
| 5651 | # |
| 5652 | # Change the name of this file to Cgi.pm. |
| 5653 | # Change the package location to where ever you locate this package. |
| 5654 | # In the example below it is located in the WriteExcel directory. |
| 5655 | # |
| 5656 | # Your httpd.conf entry for this module, should you choose to use it |
| 5657 | # as a stand alone app, should look similar to the following: |
| 5658 | # |
| 5659 | # <Location /spreadsheet-test> |
| 5660 | # SetHandler perl-script |
| 5661 | # PerlHandler Spreadsheet::WriteExcel::Cgi |
| 5662 | # PerlSendHeader On |
| 5663 | # </Location> |
| 5664 | # |
| 5665 | # The PerlHandler name above and the package name below *have* to match. |
| 5666 | |
| 5667 | # Apr 2001, Thomas Sullivan, webmaster@860.org |
| 5668 | # Feb 2001, John McNamara, jmcnamara@cpan.org |
| 5669 | |
| 5670 | package Spreadsheet::WriteExcel::Cgi; |
| 5671 | |
| 5672 | ########################################## |
| 5673 | # Pragma Definitions |
| 5674 | ########################################## |
| 5675 | use strict; |
| 5676 | |
| 5677 | ########################################## |
| 5678 | # Required Modules |
| 5679 | ########################################## |
| 5680 | use Apache::Constants qw(:common); |
| 5681 | use Apache::Request; |
| 5682 | use Apache::URI; # This may not be needed |
| 5683 | use Spreadsheet::WriteExcel; |
| 5684 | |
| 5685 | ########################################## |
| 5686 | # Main App Body |
| 5687 | ########################################## |
| 5688 | sub handler { |
| 5689 | # New apache object |
| 5690 | # Should you decide to use it. |
| 5691 | my $r = Apache::Request->new(shift); |
| 5692 | |
| 5693 | # Set the filename and send the content type |
| 5694 | # This will appear when they save the spreadsheet |
| 5695 | my $filename ="cgitest.xls"; |
| 5696 | |
| 5697 | #################################################### |
| 5698 | ## Send the content type headers |
| 5699 | #################################################### |
| 5700 | print "Content-disposition: attachment;filename=$filename\n"; |
| 5701 | print "Content-type: application/vnd.ms-excel\n\n"; |
| 5702 | |
| 5703 | #################################################### |
| 5704 | # Tie a filehandle to Apache's STDOUT. |
| 5705 | # Create a new workbook and add a worksheet. |
| 5706 | #################################################### |
| 5707 | tie *XLS => 'Apache'; |
| 5708 | binmode(*XLS); |
| 5709 | |
| 5710 | my $workbook = Spreadsheet::WriteExcel->new(\*XLS); |
| 5711 | my $worksheet = $workbook->add_worksheet(); |
| 5712 | |
| 5713 | |
| 5714 | # Set the column width for column 1 |
| 5715 | $worksheet->set_column(0, 0, 20); |
| 5716 | |
| 5717 | |
| 5718 | # Create a format |
| 5719 | my $format = $workbook->add_format(); |
| 5720 | $format->set_bold(); |
| 5721 | $format->set_size(15); |
| 5722 | $format->set_color('blue'); |
| 5723 | |
| 5724 | |
| 5725 | # Write to the workbook |
| 5726 | $worksheet->write(0, 0, "Hi Excel!", $format); |
| 5727 | |
| 5728 | # You must close the workbook for Content-disposition |
| 5729 | $workbook->close(); |
| 5730 | } |
| 5731 | |
| 5732 | 1; |
| 5733 | |
| 5734 | |
| 5735 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/mod_perl1.pl> |
| 5736 | |
| 5737 | =head2 Example: mod_perl2.pl |
| 5738 | |
| 5739 | |
| 5740 | |
| 5741 | Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 5742 | file to a browser using mod_perl 2 and Apache. |
| 5743 | |
| 5744 | This module ties *XLS directly to Apache, and with the correct |
| 5745 | content-disposition/types it will prompt the user to save |
| 5746 | the file, or open it at this location. |
| 5747 | |
| 5748 | This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. |
| 5749 | |
| 5750 | Change the name of this file to MP2Test.pm. |
| 5751 | Change the package location to where ever you locate this package. |
| 5752 | In the example below it is located in the WriteExcel directory. |
| 5753 | |
| 5754 | Your httpd.conf entry for this module, should you choose to use it |
| 5755 | as a stand alone app, should look similar to the following: |
| 5756 | |
| 5757 | PerlModule Apache2::RequestRec |
| 5758 | PerlModule APR::Table |
| 5759 | PerlModule Apache2::RequestIO |
| 5760 | |
| 5761 | <Location /spreadsheet-test> |
| 5762 | SetHandler perl-script |
| 5763 | PerlResponseHandler Spreadsheet::WriteExcel::MP2Test |
| 5764 | </Location> |
| 5765 | |
| 5766 | The PerlResponseHandler must match the package name below. |
| 5767 | |
| 5768 | ############################################################################### |
| 5769 | # |
| 5770 | # Example of how to use the Spreadsheet::WriteExcel module to send an Excel |
| 5771 | # file to a browser using mod_perl 2 and Apache. |
| 5772 | # |
| 5773 | # This module ties *XLS directly to Apache, and with the correct |
| 5774 | # content-disposition/types it will prompt the user to save |
| 5775 | # the file, or open it at this location. |
| 5776 | # |
| 5777 | # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. |
| 5778 | # |
| 5779 | # Change the name of this file to MP2Test.pm. |
| 5780 | # Change the package location to where ever you locate this package. |
| 5781 | # In the example below it is located in the WriteExcel directory. |
| 5782 | # |
| 5783 | # Your httpd.conf entry for this module, should you choose to use it |
| 5784 | # as a stand alone app, should look similar to the following: |
| 5785 | # |
| 5786 | # PerlModule Apache2::RequestRec |
| 5787 | # PerlModule APR::Table |
| 5788 | # PerlModule Apache2::RequestIO |
| 5789 | # |
| 5790 | # <Location /spreadsheet-test> |
| 5791 | # SetHandler perl-script |
| 5792 | # PerlResponseHandler Spreadsheet::WriteExcel::MP2Test |
| 5793 | # </Location> |
| 5794 | # |
| 5795 | # The PerlResponseHandler must match the package name below. |
| 5796 | |
| 5797 | # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version) |
| 5798 | # Apr 2001, Thomas Sullivan, webmaster@860.org |
| 5799 | # Feb 2001, John McNamara, jmcnamara@cpan.org |
| 5800 | |
| 5801 | package Spreadsheet::WriteExcel::MP2Test; |
| 5802 | |
| 5803 | ########################################## |
| 5804 | # Pragma Definitions |
| 5805 | ########################################## |
| 5806 | use strict; |
| 5807 | |
| 5808 | ########################################## |
| 5809 | # Required Modules |
| 5810 | ########################################## |
| 5811 | use Apache2::Const -compile => qw( :common ); |
| 5812 | use Spreadsheet::WriteExcel; |
| 5813 | |
| 5814 | ########################################## |
| 5815 | # Main App Body |
| 5816 | ########################################## |
| 5817 | sub handler { |
| 5818 | my($r) = @_; # Apache request object is passed to handler in mod_perl 2 |
| 5819 | |
| 5820 | # Set the filename and send the content type |
| 5821 | # This will appear when they save the spreadsheet |
| 5822 | my $filename ="mod_perl2_test.xls"; |
| 5823 | |
| 5824 | #################################################### |
| 5825 | ## Send the content type headers the mod_perl 2 way |
| 5826 | #################################################### |
| 5827 | $r->headers_out->{'Content-Disposition'} = "attachment;filename=$filename"; |
| 5828 | $r->content_type('application/vnd.ms-excel'); |
| 5829 | |
| 5830 | #################################################### |
| 5831 | # Tie a filehandle to Apache's STDOUT. |
| 5832 | # Create a new workbook and add a worksheet. |
| 5833 | #################################################### |
| 5834 | tie *XLS => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object |
| 5835 | binmode(*XLS); |
| 5836 | |
| 5837 | my $workbook = Spreadsheet::WriteExcel->new(\*XLS); |
| 5838 | my $worksheet = $workbook->add_worksheet(); |
| 5839 | |
| 5840 | |
| 5841 | # Set the column width for column 1 |
| 5842 | $worksheet->set_column(0, 0, 20); |
| 5843 | |
| 5844 | |
| 5845 | # Create a format |
| 5846 | my $format = $workbook->add_format(); |
| 5847 | $format->set_bold(); |
| 5848 | $format->set_size(15); |
| 5849 | $format->set_color('blue'); |
| 5850 | |
| 5851 | |
| 5852 | # Write to the workbook |
| 5853 | $worksheet->write(0, 0, 'Hi Excel! from ' . $r->hostname , $format); |
| 5854 | |
| 5855 | # You must close the workbook for Content-disposition |
| 5856 | $workbook->close(); |
| 5857 | return Apache2::Const::OK; |
| 5858 | } |
| 5859 | |
| 5860 | 1; |
| 5861 | |
| 5862 | |
| 5863 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/mod_perl2.pl> |
| 5864 | |
| 5865 | =head2 Example: outline.pl |
| 5866 | |
| 5867 | |
| 5868 | |
| 5869 | Example of how use Spreadsheet::WriteExcel to generate Excel outlines and |
| 5870 | grouping. |
| 5871 | |
| 5872 | |
| 5873 | Excel allows you to group rows or columns so that they can be hidden or |
| 5874 | displayed with a single mouse click. This feature is referred to as outlines. |
| 5875 | |
| 5876 | Outlines can reduce complex data down to a few salient sub-totals or |
| 5877 | summaries. |
| 5878 | |
| 5879 | This feature is best viewed in Excel but the following is an ASCII |
| 5880 | representation of what a worksheet with three outlines might look like. |
| 5881 | Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at |
| 5882 | level 1. The lines at the left hand side are called outline level bars. |
| 5883 | |
| 5884 | |
| 5885 | ------------------------------------------ |
| 5886 | 1 2 3 | | A | B | C | D | ... |
| 5887 | ------------------------------------------ |
| 5888 | _ | 1 | A | | | | ... |
| 5889 | | _ | 2 | B | | | | ... |
| 5890 | | | | 3 | (C) | | | | ... |
| 5891 | | | | 4 | (D) | | | | ... |
| 5892 | | - | 5 | E | | | | ... |
| 5893 | | _ | 6 | F | | | | ... |
| 5894 | | | | 7 | (G) | | | | ... |
| 5895 | | | | 8 | (H) | | | | ... |
| 5896 | | - | 9 | I | | | | ... |
| 5897 | - | . | ... | ... | ... | ... | ... |
| 5898 | |
| 5899 | |
| 5900 | Clicking the minus sign on each of the level 2 outlines will collapse and |
| 5901 | hide the data as shown in the next figure. The minus sign changes to a plus |
| 5902 | sign to indicate that the data in the outline is hidden. |
| 5903 | |
| 5904 | ------------------------------------------ |
| 5905 | 1 2 3 | | A | B | C | D | ... |
| 5906 | ------------------------------------------ |
| 5907 | _ | 1 | A | | | | ... |
| 5908 | | | 2 | B | | | | ... |
| 5909 | | + | 5 | E | | | | ... |
| 5910 | | | 6 | F | | | | ... |
| 5911 | | + | 9 | I | | | | ... |
| 5912 | - | . | ... | ... | ... | ... | ... |
| 5913 | |
| 5914 | |
| 5915 | Clicking on the minus sign on the level 1 outline will collapse the remaining |
| 5916 | rows as follows: |
| 5917 | |
| 5918 | ------------------------------------------ |
| 5919 | 1 2 3 | | A | B | C | D | ... |
| 5920 | ------------------------------------------ |
| 5921 | | 1 | A | | | | ... |
| 5922 | + | . | ... | ... | ... | ... | ... |
| 5923 | |
| 5924 | See the main Spreadsheet::WriteExcel documentation for more information. |
| 5925 | |
| 5926 | |
| 5927 | |
| 5928 | =begin html |
| 5929 | |
| 5930 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/outline.jpg" width="640" height="420" alt="Output from outline.pl" /></center></p> |
| 5931 | |
| 5932 | =end html |
| 5933 | |
| 5934 | Source code for this example: |
| 5935 | |
| 5936 | #!/usr/bin/perl -w |
| 5937 | |
| 5938 | ############################################################################### |
| 5939 | # |
| 5940 | # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and |
| 5941 | # grouping. |
| 5942 | # |
| 5943 | # |
| 5944 | # Excel allows you to group rows or columns so that they can be hidden or |
| 5945 | # displayed with a single mouse click. This feature is referred to as outlines. |
| 5946 | # |
| 5947 | # Outlines can reduce complex data down to a few salient sub-totals or |
| 5948 | # summaries. |
| 5949 | # |
| 5950 | # This feature is best viewed in Excel but the following is an ASCII |
| 5951 | # representation of what a worksheet with three outlines might look like. |
| 5952 | # Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at |
| 5953 | # level 1. The lines at the left hand side are called outline level bars. |
| 5954 | # |
| 5955 | # |
| 5956 | # ------------------------------------------ |
| 5957 | # 1 2 3 | | A | B | C | D | ... |
| 5958 | # ------------------------------------------ |
| 5959 | # _ | 1 | A | | | | ... |
| 5960 | # | _ | 2 | B | | | | ... |
| 5961 | # | | | 3 | (C) | | | | ... |
| 5962 | # | | | 4 | (D) | | | | ... |
| 5963 | # | - | 5 | E | | | | ... |
| 5964 | # | _ | 6 | F | | | | ... |
| 5965 | # | | | 7 | (G) | | | | ... |
| 5966 | # | | | 8 | (H) | | | | ... |
| 5967 | # | - | 9 | I | | | | ... |
| 5968 | # - | . | ... | ... | ... | ... | ... |
| 5969 | # |
| 5970 | # |
| 5971 | # Clicking the minus sign on each of the level 2 outlines will collapse and |
| 5972 | # hide the data as shown in the next figure. The minus sign changes to a plus |
| 5973 | # sign to indicate that the data in the outline is hidden. |
| 5974 | # |
| 5975 | # ------------------------------------------ |
| 5976 | # 1 2 3 | | A | B | C | D | ... |
| 5977 | # ------------------------------------------ |
| 5978 | # _ | 1 | A | | | | ... |
| 5979 | # | | 2 | B | | | | ... |
| 5980 | # | + | 5 | E | | | | ... |
| 5981 | # | | 6 | F | | | | ... |
| 5982 | # | + | 9 | I | | | | ... |
| 5983 | # - | . | ... | ... | ... | ... | ... |
| 5984 | # |
| 5985 | # |
| 5986 | # Clicking on the minus sign on the level 1 outline will collapse the remaining |
| 5987 | # rows as follows: |
| 5988 | # |
| 5989 | # ------------------------------------------ |
| 5990 | # 1 2 3 | | A | B | C | D | ... |
| 5991 | # ------------------------------------------ |
| 5992 | # | 1 | A | | | | ... |
| 5993 | # + | . | ... | ... | ... | ... | ... |
| 5994 | # |
| 5995 | # See the main Spreadsheet::WriteExcel documentation for more information. |
| 5996 | # |
| 5997 | # reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org |
| 5998 | # |
| 5999 | |
| 6000 | |
| 6001 | |
| 6002 | use strict; |
| 6003 | use Spreadsheet::WriteExcel; |
| 6004 | |
| 6005 | # Create a new workbook and add some worksheets |
| 6006 | my $workbook = Spreadsheet::WriteExcel->new('outline.xls'); |
| 6007 | my $worksheet1 = $workbook->add_worksheet('Outlined Rows'); |
| 6008 | my $worksheet2 = $workbook->add_worksheet('Collapsed Rows'); |
| 6009 | my $worksheet3 = $workbook->add_worksheet('Outline Columns'); |
| 6010 | my $worksheet4 = $workbook->add_worksheet('Outline levels'); |
| 6011 | |
| 6012 | # Add a general format |
| 6013 | my $bold = $workbook->add_format(bold => 1); |
| 6014 | |
| 6015 | |
| 6016 | |
| 6017 | ############################################################################### |
| 6018 | # |
| 6019 | # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() |
| 6020 | # functions so that it looks like the type of automatic outlines that are |
| 6021 | # generated when you use the Excel Data->SubTotals menu item. |
| 6022 | # |
| 6023 | |
| 6024 | |
| 6025 | # For outlines the important parameters are $hidden and $level. Rows with the |
| 6026 | # same $level are grouped together. The group will be collapsed if $hidden is |
| 6027 | # non-zero. $height and $XF are assigned default values if they are undef. |
| 6028 | # |
| 6029 | # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) |
| 6030 | # |
| 6031 | $worksheet1->set_row(1, undef, undef, 0, 2); |
| 6032 | $worksheet1->set_row(2, undef, undef, 0, 2); |
| 6033 | $worksheet1->set_row(3, undef, undef, 0, 2); |
| 6034 | $worksheet1->set_row(4, undef, undef, 0, 2); |
| 6035 | $worksheet1->set_row(5, undef, undef, 0, 1); |
| 6036 | |
| 6037 | $worksheet1->set_row(6, undef, undef, 0, 2); |
| 6038 | $worksheet1->set_row(7, undef, undef, 0, 2); |
| 6039 | $worksheet1->set_row(8, undef, undef, 0, 2); |
| 6040 | $worksheet1->set_row(9, undef, undef, 0, 2); |
| 6041 | $worksheet1->set_row(10, undef, undef, 0, 1); |
| 6042 | |
| 6043 | |
| 6044 | # Add a column format for clarity |
| 6045 | $worksheet1->set_column('A:A', 20); |
| 6046 | |
| 6047 | # Add the data, labels and formulas |
| 6048 | $worksheet1->write('A1', 'Region', $bold); |
| 6049 | $worksheet1->write('A2', 'North'); |
| 6050 | $worksheet1->write('A3', 'North'); |
| 6051 | $worksheet1->write('A4', 'North'); |
| 6052 | $worksheet1->write('A5', 'North'); |
| 6053 | $worksheet1->write('A6', 'North Total', $bold); |
| 6054 | |
| 6055 | $worksheet1->write('B1', 'Sales', $bold); |
| 6056 | $worksheet1->write('B2', 1000); |
| 6057 | $worksheet1->write('B3', 1200); |
| 6058 | $worksheet1->write('B4', 900); |
| 6059 | $worksheet1->write('B5', 1200); |
| 6060 | $worksheet1->write('B6', '=SUBTOTAL(9,B2:B5)', $bold); |
| 6061 | |
| 6062 | $worksheet1->write('A7', 'South'); |
| 6063 | $worksheet1->write('A8', 'South'); |
| 6064 | $worksheet1->write('A9', 'South'); |
| 6065 | $worksheet1->write('A10', 'South'); |
| 6066 | $worksheet1->write('A11', 'South Total', $bold); |
| 6067 | |
| 6068 | $worksheet1->write('B7', 400); |
| 6069 | $worksheet1->write('B8', 600); |
| 6070 | $worksheet1->write('B9', 500); |
| 6071 | $worksheet1->write('B10', 600); |
| 6072 | $worksheet1->write('B11', '=SUBTOTAL(9,B7:B10)', $bold); |
| 6073 | |
| 6074 | $worksheet1->write('A12', 'Grand Total', $bold); |
| 6075 | $worksheet1->write('B12', '=SUBTOTAL(9,B2:B10)', $bold); |
| 6076 | |
| 6077 | |
| 6078 | ############################################################################### |
| 6079 | # |
| 6080 | # Example 2: Create a worksheet with outlined rows. This is the same as the |
| 6081 | # previous example except that the rows are collapsed. |
| 6082 | # Note: We need to indicate the row that contains the collapsed symbol '+' |
| 6083 | # with the optional parameter, $collapsed. |
| 6084 | |
| 6085 | # The group will be collapsed if $hidden is non-zero. |
| 6086 | # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) |
| 6087 | # |
| 6088 | $worksheet2->set_row(1, undef, undef, 1, 2); |
| 6089 | $worksheet2->set_row(2, undef, undef, 1, 2); |
| 6090 | $worksheet2->set_row(3, undef, undef, 1, 2); |
| 6091 | $worksheet2->set_row(4, undef, undef, 1, 2); |
| 6092 | $worksheet2->set_row(5, undef, undef, 1, 1); |
| 6093 | |
| 6094 | $worksheet2->set_row(6, undef, undef, 1, 2); |
| 6095 | $worksheet2->set_row(7, undef, undef, 1, 2); |
| 6096 | $worksheet2->set_row(8, undef, undef, 1, 2); |
| 6097 | $worksheet2->set_row(9, undef, undef, 1, 2); |
| 6098 | $worksheet2->set_row(10, undef, undef, 1, 1); |
| 6099 | $worksheet2->set_row(11, undef, undef, 0, 0, 1); |
| 6100 | |
| 6101 | |
| 6102 | # Add a column format for clarity |
| 6103 | $worksheet2->set_column('A:A', 20); |
| 6104 | |
| 6105 | # Add the data, labels and formulas |
| 6106 | $worksheet2->write('A1', 'Region', $bold); |
| 6107 | $worksheet2->write('A2', 'North'); |
| 6108 | $worksheet2->write('A3', 'North'); |
| 6109 | $worksheet2->write('A4', 'North'); |
| 6110 | $worksheet2->write('A5', 'North'); |
| 6111 | $worksheet2->write('A6', 'North Total', $bold); |
| 6112 | |
| 6113 | $worksheet2->write('B1', 'Sales', $bold); |
| 6114 | $worksheet2->write('B2', 1000); |
| 6115 | $worksheet2->write('B3', 1200); |
| 6116 | $worksheet2->write('B4', 900); |
| 6117 | $worksheet2->write('B5', 1200); |
| 6118 | $worksheet2->write('B6', '=SUBTOTAL(9,B2:B5)', $bold); |
| 6119 | |
| 6120 | $worksheet2->write('A7', 'South'); |
| 6121 | $worksheet2->write('A8', 'South'); |
| 6122 | $worksheet2->write('A9', 'South'); |
| 6123 | $worksheet2->write('A10', 'South'); |
| 6124 | $worksheet2->write('A11', 'South Total', $bold); |
| 6125 | |
| 6126 | $worksheet2->write('B7', 400); |
| 6127 | $worksheet2->write('B8', 600); |
| 6128 | $worksheet2->write('B9', 500); |
| 6129 | $worksheet2->write('B10', 600); |
| 6130 | $worksheet2->write('B11', '=SUBTOTAL(9,B7:B10)', $bold); |
| 6131 | |
| 6132 | $worksheet2->write('A12', 'Grand Total', $bold); |
| 6133 | $worksheet2->write('B12', '=SUBTOTAL(9,B2:B10)', $bold); |
| 6134 | |
| 6135 | |
| 6136 | |
| 6137 | ############################################################################### |
| 6138 | # |
| 6139 | # Example 3: Create a worksheet with outlined columns. |
| 6140 | # |
| 6141 | my $data = [ |
| 6142 | ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'], |
| 6143 | ['North', 50, 20, 15, 25, 65, 80, ,'=SUM(B2:G2)'], |
| 6144 | ['South', 10, 20, 30, 50, 50, 50, ,'=SUM(B3:G3)'], |
| 6145 | ['East', 45, 75, 50, 15, 75, 100, ,'=SUM(B4:G4)'], |
| 6146 | ['West', 15, 15, 55, 35, 20, 50, ,'=SUM(B5:G6)'], |
| 6147 | ]; |
| 6148 | |
| 6149 | # Add bold format to the first row |
| 6150 | $worksheet3->set_row(0, undef, $bold); |
| 6151 | |
| 6152 | # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) |
| 6153 | $worksheet3->set_column('A:A', 10, $bold ); |
| 6154 | $worksheet3->set_column('B:G', 5, undef, 0, 1); |
| 6155 | $worksheet3->set_column('H:H', 10); |
| 6156 | |
| 6157 | # Write the data and a formula |
| 6158 | $worksheet3->write_col('A1', $data); |
| 6159 | $worksheet3->write('H6', '=SUM(H2:H5)', $bold); |
| 6160 | |
| 6161 | |
| 6162 | |
| 6163 | ############################################################################### |
| 6164 | # |
| 6165 | # Example 4: Show all possible outline levels. |
| 6166 | # |
| 6167 | my $levels = ["Level 1", "Level 2", "Level 3", "Level 4", |
| 6168 | "Level 5", "Level 6", "Level 7", "Level 6", |
| 6169 | "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"]; |
| 6170 | |
| 6171 | |
| 6172 | $worksheet4->write_col('A1', $levels); |
| 6173 | |
| 6174 | $worksheet4->set_row(0, undef, undef, undef, 1); |
| 6175 | $worksheet4->set_row(1, undef, undef, undef, 2); |
| 6176 | $worksheet4->set_row(2, undef, undef, undef, 3); |
| 6177 | $worksheet4->set_row(3, undef, undef, undef, 4); |
| 6178 | $worksheet4->set_row(4, undef, undef, undef, 5); |
| 6179 | $worksheet4->set_row(5, undef, undef, undef, 6); |
| 6180 | $worksheet4->set_row(6, undef, undef, undef, 7); |
| 6181 | $worksheet4->set_row(7, undef, undef, undef, 6); |
| 6182 | $worksheet4->set_row(8, undef, undef, undef, 5); |
| 6183 | $worksheet4->set_row(9, undef, undef, undef, 4); |
| 6184 | $worksheet4->set_row(10, undef, undef, undef, 3); |
| 6185 | $worksheet4->set_row(11, undef, undef, undef, 2); |
| 6186 | $worksheet4->set_row(12, undef, undef, undef, 1); |
| 6187 | |
| 6188 | |
| 6189 | |
| 6190 | __END__ |
| 6191 | |
| 6192 | |
| 6193 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/outline.pl> |
| 6194 | |
| 6195 | =head2 Example: outline_collapsed.pl |
| 6196 | |
| 6197 | |
| 6198 | |
| 6199 | Example of how use Spreadsheet::WriteExcel to generate Excel outlines and |
| 6200 | grouping. |
| 6201 | |
| 6202 | These example focus mainly on collapsed outlines. See also the |
| 6203 | outlines.pl example program for more general examples. |
| 6204 | |
| 6205 | |
| 6206 | |
| 6207 | =begin html |
| 6208 | |
| 6209 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/outline_collapsed.jpg" width="640" height="420" alt="Output from outline_collapsed.pl" /></center></p> |
| 6210 | |
| 6211 | =end html |
| 6212 | |
| 6213 | Source code for this example: |
| 6214 | |
| 6215 | #!/usr/bin/perl -w |
| 6216 | |
| 6217 | ############################################################################### |
| 6218 | # |
| 6219 | # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and |
| 6220 | # grouping. |
| 6221 | # |
| 6222 | # These example focus mainly on collapsed outlines. See also the |
| 6223 | # outlines.pl example program for more general examples. |
| 6224 | # |
| 6225 | # reverse('©'), March 2008, John McNamara, jmcnamara@cpan.org |
| 6226 | # |
| 6227 | |
| 6228 | use strict; |
| 6229 | use Spreadsheet::WriteExcel; |
| 6230 | |
| 6231 | # Create a new workbook and add some worksheets |
| 6232 | my $workbook = Spreadsheet::WriteExcel->new('outline_collapsed.xls'); |
| 6233 | my $worksheet1 = $workbook->add_worksheet('Outlined Rows'); |
| 6234 | my $worksheet2 = $workbook->add_worksheet('Collapsed Rows 1'); |
| 6235 | my $worksheet3 = $workbook->add_worksheet('Collapsed Rows 2'); |
| 6236 | my $worksheet4 = $workbook->add_worksheet('Collapsed Rows 3'); |
| 6237 | my $worksheet5 = $workbook->add_worksheet('Outline Columns'); |
| 6238 | my $worksheet6 = $workbook->add_worksheet('Collapsed Columns'); |
| 6239 | |
| 6240 | |
| 6241 | # Add a general format |
| 6242 | my $bold = $workbook->add_format(bold => 1); |
| 6243 | |
| 6244 | |
| 6245 | # |
| 6246 | # This function will generate the same data and sub-totals on each worksheet. |
| 6247 | # |
| 6248 | sub create_sub_totals { |
| 6249 | |
| 6250 | my $worksheet = $_[0]; |
| 6251 | |
| 6252 | # Add a column format for clarity |
| 6253 | $worksheet->set_column('A:A', 20); |
| 6254 | |
| 6255 | # Add the data, labels and formulas |
| 6256 | $worksheet->write('A1', 'Region', $bold); |
| 6257 | $worksheet->write('A2', 'North'); |
| 6258 | $worksheet->write('A3', 'North'); |
| 6259 | $worksheet->write('A4', 'North'); |
| 6260 | $worksheet->write('A5', 'North'); |
| 6261 | $worksheet->write('A6', 'North Total', $bold); |
| 6262 | |
| 6263 | $worksheet->write('B1', 'Sales', $bold); |
| 6264 | $worksheet->write('B2', 1000); |
| 6265 | $worksheet->write('B3', 1200); |
| 6266 | $worksheet->write('B4', 900); |
| 6267 | $worksheet->write('B5', 1200); |
| 6268 | $worksheet->write('B6', '=SUBTOTAL(9,B2:B5)', $bold); |
| 6269 | |
| 6270 | $worksheet->write('A7', 'South'); |
| 6271 | $worksheet->write('A8', 'South'); |
| 6272 | $worksheet->write('A9', 'South'); |
| 6273 | $worksheet->write('A10', 'South'); |
| 6274 | $worksheet->write('A11', 'South Total', $bold); |
| 6275 | |
| 6276 | $worksheet->write('B7', 400); |
| 6277 | $worksheet->write('B8', 600); |
| 6278 | $worksheet->write('B9', 500); |
| 6279 | $worksheet->write('B10', 600); |
| 6280 | $worksheet->write('B11', '=SUBTOTAL(9,B7:B10)', $bold); |
| 6281 | |
| 6282 | $worksheet->write('A12', 'Grand Total', $bold); |
| 6283 | $worksheet->write('B12', '=SUBTOTAL(9,B2:B10)', $bold); |
| 6284 | |
| 6285 | } |
| 6286 | |
| 6287 | |
| 6288 | ############################################################################### |
| 6289 | # |
| 6290 | # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() |
| 6291 | # functions so that it looks like the type of automatic outlines that are |
| 6292 | # generated when you use the Excel Data->SubTotals menu item. |
| 6293 | # |
| 6294 | |
| 6295 | # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) |
| 6296 | $worksheet1->set_row(1, undef, undef, 0, 2); |
| 6297 | $worksheet1->set_row(2, undef, undef, 0, 2); |
| 6298 | $worksheet1->set_row(3, undef, undef, 0, 2); |
| 6299 | $worksheet1->set_row(4, undef, undef, 0, 2); |
| 6300 | $worksheet1->set_row(5, undef, undef, 0, 1); |
| 6301 | |
| 6302 | $worksheet1->set_row(6, undef, undef, 0, 2); |
| 6303 | $worksheet1->set_row(7, undef, undef, 0, 2); |
| 6304 | $worksheet1->set_row(8, undef, undef, 0, 2); |
| 6305 | $worksheet1->set_row(9, undef, undef, 0, 2); |
| 6306 | $worksheet1->set_row(10, undef, undef, 0, 1); |
| 6307 | |
| 6308 | # Write the sub-total data that is common to the row examples. |
| 6309 | create_sub_totals($worksheet1); |
| 6310 | |
| 6311 | |
| 6312 | ############################################################################### |
| 6313 | # |
| 6314 | # Example 2: Create a worksheet with collapsed outlined rows. |
| 6315 | # This is the same as the example 1 except that the all rows are collapsed. |
| 6316 | # Note: We need to indicate the row that contains the collapsed symbol '+' with |
| 6317 | # the optional parameter, $collapsed. |
| 6318 | |
| 6319 | $worksheet2->set_row(1, undef, undef, 1, 2); |
| 6320 | $worksheet2->set_row(2, undef, undef, 1, 2); |
| 6321 | $worksheet2->set_row(3, undef, undef, 1, 2); |
| 6322 | $worksheet2->set_row(4, undef, undef, 1, 2); |
| 6323 | $worksheet2->set_row(5, undef, undef, 1, 1); |
| 6324 | |
| 6325 | $worksheet2->set_row(6, undef, undef, 1, 2); |
| 6326 | $worksheet2->set_row(7, undef, undef, 1, 2); |
| 6327 | $worksheet2->set_row(8, undef, undef, 1, 2); |
| 6328 | $worksheet2->set_row(9, undef, undef, 1, 2); |
| 6329 | $worksheet2->set_row(10, undef, undef, 1, 1); |
| 6330 | |
| 6331 | $worksheet2->set_row(11, undef, undef, 0, 0, 1); |
| 6332 | |
| 6333 | # Write the sub-total data that is common to the row examples. |
| 6334 | create_sub_totals($worksheet2); |
| 6335 | |
| 6336 | |
| 6337 | ############################################################################### |
| 6338 | # |
| 6339 | # Example 3: Create a worksheet with collapsed outlined rows. |
| 6340 | # Same as the example 1 except that the two sub-totals are collapsed. |
| 6341 | |
| 6342 | $worksheet3->set_row(1, undef, undef, 1, 2); |
| 6343 | $worksheet3->set_row(2, undef, undef, 1, 2); |
| 6344 | $worksheet3->set_row(3, undef, undef, 1, 2); |
| 6345 | $worksheet3->set_row(4, undef, undef, 1, 2); |
| 6346 | $worksheet3->set_row(5, undef, undef, 0, 1, 1); |
| 6347 | |
| 6348 | $worksheet3->set_row(6, undef, undef, 1, 2); |
| 6349 | $worksheet3->set_row(7, undef, undef, 1, 2); |
| 6350 | $worksheet3->set_row(8, undef, undef, 1, 2); |
| 6351 | $worksheet3->set_row(9, undef, undef, 1, 2); |
| 6352 | $worksheet3->set_row(10, undef, undef, 0, 1, 1); |
| 6353 | |
| 6354 | |
| 6355 | # Write the sub-total data that is common to the row examples. |
| 6356 | create_sub_totals($worksheet3); |
| 6357 | |
| 6358 | |
| 6359 | ############################################################################### |
| 6360 | # |
| 6361 | # Example 4: Create a worksheet with outlined rows. |
| 6362 | # Same as the example 1 except that the two sub-totals are collapsed. |
| 6363 | |
| 6364 | $worksheet4->set_row(1, undef, undef, 1, 2); |
| 6365 | $worksheet4->set_row(2, undef, undef, 1, 2); |
| 6366 | $worksheet4->set_row(3, undef, undef, 1, 2); |
| 6367 | $worksheet4->set_row(4, undef, undef, 1, 2); |
| 6368 | $worksheet4->set_row(5, undef, undef, 1, 1, 1); |
| 6369 | |
| 6370 | $worksheet4->set_row(6, undef, undef, 1, 2); |
| 6371 | $worksheet4->set_row(7, undef, undef, 1, 2); |
| 6372 | $worksheet4->set_row(8, undef, undef, 1, 2); |
| 6373 | $worksheet4->set_row(9, undef, undef, 1, 2); |
| 6374 | $worksheet4->set_row(10, undef, undef, 1, 1, 1); |
| 6375 | |
| 6376 | $worksheet4->set_row(11, undef, undef, 0, 0, 1); |
| 6377 | |
| 6378 | # Write the sub-total data that is common to the row examples. |
| 6379 | create_sub_totals($worksheet4); |
| 6380 | |
| 6381 | |
| 6382 | |
| 6383 | ############################################################################### |
| 6384 | # |
| 6385 | # Example 5: Create a worksheet with outlined columns. |
| 6386 | # |
| 6387 | my $data = [ |
| 6388 | ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'], |
| 6389 | ['North', 50, 20, 15, 25, 65, 80, ,'=SUM(B2:G2)'], |
| 6390 | ['South', 10, 20, 30, 50, 50, 50, ,'=SUM(B3:G3)'], |
| 6391 | ['East', 45, 75, 50, 15, 75, 100, ,'=SUM(B4:G4)'], |
| 6392 | ['West', 15, 15, 55, 35, 20, 50, ,'=SUM(B5:G6)'], |
| 6393 | ]; |
| 6394 | |
| 6395 | # Add bold format to the first row |
| 6396 | $worksheet5->set_row(0, undef, $bold); |
| 6397 | |
| 6398 | # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) |
| 6399 | $worksheet5->set_column('A:A', 10, $bold ); |
| 6400 | $worksheet5->set_column('B:G', 5, undef, 0, 1); |
| 6401 | $worksheet5->set_column('H:H', 10 ); |
| 6402 | |
| 6403 | # Write the data and a formula |
| 6404 | $worksheet5->write_col('A1', $data); |
| 6405 | $worksheet5->write('H6', '=SUM(H2:H5)', $bold); |
| 6406 | |
| 6407 | |
| 6408 | ############################################################################### |
| 6409 | # |
| 6410 | # Example 6: Create a worksheet with collapsed outlined columns. |
| 6411 | # This is the same as the previous example except collapsed columns. |
| 6412 | |
| 6413 | # Add bold format to the first row |
| 6414 | $worksheet6->set_row(0, undef, $bold); |
| 6415 | |
| 6416 | # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) |
| 6417 | $worksheet6->set_column('A:A', 10, $bold ); |
| 6418 | $worksheet6->set_column('B:G', 5, undef, 1, 1 ); |
| 6419 | $worksheet6->set_column('H:H', 10, undef, 0, 0, 1); |
| 6420 | |
| 6421 | # Write the data and a formula |
| 6422 | $worksheet6->write_col('A1', $data); |
| 6423 | $worksheet6->write('H6', '=SUM(H2:H5)', $bold); |
| 6424 | |
| 6425 | |
| 6426 | __END__ |
| 6427 | |
| 6428 | |
| 6429 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/outline_collapsed.pl> |
| 6430 | |
| 6431 | =head2 Example: panes.pl |
| 6432 | |
| 6433 | |
| 6434 | |
| 6435 | Example of using the WriteExcel module to create worksheet panes. |
| 6436 | |
| 6437 | |
| 6438 | |
| 6439 | =begin html |
| 6440 | |
| 6441 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/panes.jpg" width="640" height="420" alt="Output from panes.pl" /></center></p> |
| 6442 | |
| 6443 | =end html |
| 6444 | |
| 6445 | Source code for this example: |
| 6446 | |
| 6447 | #!/usr/bin/perl -w |
| 6448 | |
| 6449 | ####################################################################### |
| 6450 | # |
| 6451 | # Example of using the WriteExcel module to create worksheet panes. |
| 6452 | # |
| 6453 | # reverse('©'), May 2001, John McNamara, jmcnamara@cpan.org |
| 6454 | # |
| 6455 | |
| 6456 | use strict; |
| 6457 | use Spreadsheet::WriteExcel; |
| 6458 | |
| 6459 | my $workbook = Spreadsheet::WriteExcel->new("panes.xls"); |
| 6460 | |
| 6461 | my $worksheet1 = $workbook->add_worksheet('Panes 1'); |
| 6462 | my $worksheet2 = $workbook->add_worksheet('Panes 2'); |
| 6463 | my $worksheet3 = $workbook->add_worksheet('Panes 3'); |
| 6464 | my $worksheet4 = $workbook->add_worksheet('Panes 4'); |
| 6465 | |
| 6466 | # Freeze panes |
| 6467 | $worksheet1->freeze_panes(1, 0); # 1 row |
| 6468 | |
| 6469 | $worksheet2->freeze_panes(0, 1); # 1 column |
| 6470 | $worksheet3->freeze_panes(1, 1); # 1 row and column |
| 6471 | |
| 6472 | # Split panes. |
| 6473 | # The divisions must be specified in terms of row and column dimensions. |
| 6474 | # The default row height is 12.75 and the default column width is 8.43 |
| 6475 | # |
| 6476 | $worksheet4->split_panes(12.75, 8.43, 1, 1); # 1 row and column |
| 6477 | |
| 6478 | |
| 6479 | ####################################################################### |
| 6480 | # |
| 6481 | # Set up some formatting and text to highlight the panes |
| 6482 | # |
| 6483 | |
| 6484 | my $header = $workbook->add_format(); |
| 6485 | $header->set_color('white'); |
| 6486 | $header->set_align('center'); |
| 6487 | $header->set_align('vcenter'); |
| 6488 | $header->set_pattern(); |
| 6489 | $header->set_fg_color('green'); |
| 6490 | |
| 6491 | my $center = $workbook->add_format(); |
| 6492 | $center->set_align('center'); |
| 6493 | |
| 6494 | |
| 6495 | ####################################################################### |
| 6496 | # |
| 6497 | # Sheet 1 |
| 6498 | # |
| 6499 | |
| 6500 | $worksheet1->set_column('A:I', 16); |
| 6501 | $worksheet1->set_row(0, 20); |
| 6502 | $worksheet1->set_selection('C3'); |
| 6503 | |
| 6504 | for my $i (0..8){ |
| 6505 | $worksheet1->write(0, $i, 'Scroll down', $header); |
| 6506 | } |
| 6507 | |
| 6508 | for my $i (1..100){ |
| 6509 | for my $j (0..8){ |
| 6510 | $worksheet1->write($i, $j, $i+1, $center); |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | |
| 6515 | ####################################################################### |
| 6516 | # |
| 6517 | # Sheet 2 |
| 6518 | # |
| 6519 | |
| 6520 | $worksheet2->set_column('A:A', 16); |
| 6521 | $worksheet2->set_selection('C3'); |
| 6522 | |
| 6523 | for my $i (0..49){ |
| 6524 | $worksheet2->set_row($i, 15); |
| 6525 | $worksheet2->write($i, 0, 'Scroll right', $header); |
| 6526 | } |
| 6527 | |
| 6528 | for my $i (0..49){ |
| 6529 | for my $j (1..25){ |
| 6530 | $worksheet2->write($i, $j, $j, $center); |
| 6531 | } |
| 6532 | } |
| 6533 | |
| 6534 | |
| 6535 | ####################################################################### |
| 6536 | # |
| 6537 | # Sheet 3 |
| 6538 | # |
| 6539 | |
| 6540 | $worksheet3->set_column('A:Z', 16); |
| 6541 | $worksheet3->set_selection('C3'); |
| 6542 | |
| 6543 | for my $i (1..25){ |
| 6544 | $worksheet3->write(0, $i, 'Scroll down', $header); |
| 6545 | } |
| 6546 | |
| 6547 | for my $i (1..49){ |
| 6548 | $worksheet3->write($i, 0, 'Scroll right', $header); |
| 6549 | } |
| 6550 | |
| 6551 | for my $i (1..49){ |
| 6552 | for my $j (1..25){ |
| 6553 | $worksheet3->write($i, $j, $j, $center); |
| 6554 | } |
| 6555 | } |
| 6556 | |
| 6557 | |
| 6558 | ####################################################################### |
| 6559 | # |
| 6560 | # Sheet 4 |
| 6561 | # |
| 6562 | |
| 6563 | $worksheet4->set_selection('C3'); |
| 6564 | |
| 6565 | for my $i (1..25){ |
| 6566 | $worksheet4->write(0, $i, 'Scroll', $center); |
| 6567 | } |
| 6568 | |
| 6569 | for my $i (1..49){ |
| 6570 | $worksheet4->write($i, 0, 'Scroll', $center); |
| 6571 | } |
| 6572 | |
| 6573 | for my $i (1..49){ |
| 6574 | for my $j (1..25){ |
| 6575 | $worksheet4->write($i, $j, $j, $center); |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | |
| 6580 | |
| 6581 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/panes.pl> |
| 6582 | |
| 6583 | =head2 Example: properties.pl |
| 6584 | |
| 6585 | |
| 6586 | |
| 6587 | An example of adding document properties to a Spreadsheet::WriteExcel file. |
| 6588 | |
| 6589 | |
| 6590 | |
| 6591 | =begin html |
| 6592 | |
| 6593 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/properties.jpg" width="640" height="420" alt="Output from properties.pl" /></center></p> |
| 6594 | |
| 6595 | =end html |
| 6596 | |
| 6597 | Source code for this example: |
| 6598 | |
| 6599 | #!/usr/bin/perl -w |
| 6600 | |
| 6601 | ############################################################################## |
| 6602 | # |
| 6603 | # An example of adding document properties to a Spreadsheet::WriteExcel file. |
| 6604 | # |
| 6605 | # reverse('©'), August 2008, John McNamara, jmcnamara@cpan.org |
| 6606 | # |
| 6607 | |
| 6608 | use strict; |
| 6609 | use Spreadsheet::WriteExcel; |
| 6610 | |
| 6611 | my $workbook = Spreadsheet::WriteExcel->new('properties.xls'); |
| 6612 | my $worksheet = $workbook->add_worksheet(); |
| 6613 | |
| 6614 | |
| 6615 | $workbook->set_properties( |
| 6616 | title => 'This is an example spreadsheet', |
| 6617 | subject => 'With document properties', |
| 6618 | author => 'John McNamara', |
| 6619 | manager => 'Dr. Heinz Doofenshmirtz ', |
| 6620 | company => 'of Wolves', |
| 6621 | category => 'Example spreadsheets', |
| 6622 | keywords => 'Sample, Example, Properties', |
| 6623 | comments => 'Created with Perl and Spreadsheet::WriteExcel', |
| 6624 | ); |
| 6625 | |
| 6626 | |
| 6627 | $worksheet->set_column('A:A', 50); |
| 6628 | $worksheet->write('A1', 'Select File->Properties to see the file properties'); |
| 6629 | |
| 6630 | |
| 6631 | __END__ |
| 6632 | |
| 6633 | |
| 6634 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/properties.pl> |
| 6635 | |
| 6636 | =head2 Example: protection.pl |
| 6637 | |
| 6638 | |
| 6639 | |
| 6640 | Example of cell locking and formula hiding in an Excel worksheet via |
| 6641 | the Spreadsheet::WriteExcel module. |
| 6642 | |
| 6643 | |
| 6644 | |
| 6645 | =begin html |
| 6646 | |
| 6647 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/protection.jpg" width="640" height="420" alt="Output from protection.pl" /></center></p> |
| 6648 | |
| 6649 | =end html |
| 6650 | |
| 6651 | Source code for this example: |
| 6652 | |
| 6653 | #!/usr/bin/perl -w |
| 6654 | |
| 6655 | ######################################################################## |
| 6656 | # |
| 6657 | # Example of cell locking and formula hiding in an Excel worksheet via |
| 6658 | # the Spreadsheet::WriteExcel module. |
| 6659 | # |
| 6660 | # reverse('©'), August 2001, John McNamara, jmcnamara@cpan.org |
| 6661 | # |
| 6662 | |
| 6663 | use strict; |
| 6664 | use Spreadsheet::WriteExcel; |
| 6665 | |
| 6666 | my $workbook = Spreadsheet::WriteExcel->new("protection.xls"); |
| 6667 | my $worksheet = $workbook->add_worksheet(); |
| 6668 | |
| 6669 | # Create some format objects |
| 6670 | my $locked = $workbook->add_format(locked => 1); |
| 6671 | my $unlocked = $workbook->add_format(locked => 0); |
| 6672 | my $hidden = $workbook->add_format(hidden => 1); |
| 6673 | |
| 6674 | # Format the columns |
| 6675 | $worksheet->set_column('A:A', 42); |
| 6676 | $worksheet->set_selection('B3:B3'); |
| 6677 | |
| 6678 | # Protect the worksheet |
| 6679 | $worksheet->protect(); |
| 6680 | |
| 6681 | # Examples of cell locking and hiding |
| 6682 | $worksheet->write('A1', 'Cell B1 is locked. It cannot be edited.'); |
| 6683 | $worksheet->write('B1', '=1+2', $locked); |
| 6684 | |
| 6685 | $worksheet->write('A2', 'Cell B2 is unlocked. It can be edited.'); |
| 6686 | $worksheet->write('B2', '=1+2', $unlocked); |
| 6687 | |
| 6688 | $worksheet->write('A3', "Cell B3 is hidden. The formula isn't visible."); |
| 6689 | $worksheet->write('B3', '=1+2', $hidden); |
| 6690 | |
| 6691 | $worksheet->write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet'); |
| 6692 | $worksheet->write('A6', 'to remove the worksheet protection. '); |
| 6693 | |
| 6694 | |
| 6695 | |
| 6696 | |
| 6697 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/protection.pl> |
| 6698 | |
| 6699 | =head2 Example: repeat.pl |
| 6700 | |
| 6701 | |
| 6702 | |
| 6703 | Example of writing repeated formulas. |
| 6704 | |
| 6705 | |
| 6706 | |
| 6707 | =begin html |
| 6708 | |
| 6709 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/repeat.jpg" width="640" height="420" alt="Output from repeat.pl" /></center></p> |
| 6710 | |
| 6711 | =end html |
| 6712 | |
| 6713 | Source code for this example: |
| 6714 | |
| 6715 | #!/usr/bin/perl -w |
| 6716 | |
| 6717 | ###################################################################### |
| 6718 | # |
| 6719 | # Example of writing repeated formulas. |
| 6720 | # |
| 6721 | # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org |
| 6722 | # |
| 6723 | |
| 6724 | use strict; |
| 6725 | use Spreadsheet::WriteExcel; |
| 6726 | |
| 6727 | my $workbook = Spreadsheet::WriteExcel->new("repeat.xls"); |
| 6728 | my $worksheet = $workbook->add_worksheet(); |
| 6729 | |
| 6730 | |
| 6731 | my $limit = 1000; |
| 6732 | |
| 6733 | # Write a column of numbers |
| 6734 | for my $row (0..$limit) { |
| 6735 | $worksheet->write($row, 0, $row); |
| 6736 | } |
| 6737 | |
| 6738 | |
| 6739 | # Store a formula |
| 6740 | my $formula = $worksheet->store_formula('=A1*5+4'); |
| 6741 | |
| 6742 | |
| 6743 | # Write a column of formulas based on the stored formula |
| 6744 | for my $row (0..$limit) { |
| 6745 | $worksheet->repeat_formula($row, 1, $formula, undef, |
| 6746 | qr/^A1$/, 'A'.($row+1)); |
| 6747 | } |
| 6748 | |
| 6749 | |
| 6750 | # Direct formula writing. As a speed comparison uncomment the |
| 6751 | # following and run the program again |
| 6752 | |
| 6753 | #for my $row (0..$limit) { |
| 6754 | # $worksheet->write_formula($row, 2, '=A'.($row+1).'*5+4'); |
| 6755 | #} |
| 6756 | |
| 6757 | |
| 6758 | |
| 6759 | __END__ |
| 6760 | |
| 6761 | |
| 6762 | |
| 6763 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/repeat.pl> |
| 6764 | |
| 6765 | =head2 Example: right_to_left.pl |
| 6766 | |
| 6767 | |
| 6768 | |
| 6769 | Example of how to change the default worksheet direction from |
| 6770 | left-to-right to right-to-left as required by some eastern verions |
| 6771 | of Excel. |
| 6772 | |
| 6773 | |
| 6774 | |
| 6775 | =begin html |
| 6776 | |
| 6777 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/right_to_left.jpg" width="640" height="420" alt="Output from right_to_left.pl" /></center></p> |
| 6778 | |
| 6779 | =end html |
| 6780 | |
| 6781 | Source code for this example: |
| 6782 | |
| 6783 | #!/usr/bin/perl -w |
| 6784 | |
| 6785 | ####################################################################### |
| 6786 | # |
| 6787 | # Example of how to change the default worksheet direction from |
| 6788 | # left-to-right to right-to-left as required by some eastern verions |
| 6789 | # of Excel. |
| 6790 | # |
| 6791 | # reverse('©'), January 2006, John McNamara, jmcnamara@cpan.org |
| 6792 | # |
| 6793 | |
| 6794 | use strict; |
| 6795 | use Spreadsheet::WriteExcel; |
| 6796 | |
| 6797 | my $workbook = Spreadsheet::WriteExcel->new("right_to_left.xls"); |
| 6798 | my $worksheet1 = $workbook->add_worksheet(); |
| 6799 | my $worksheet2 = $workbook->add_worksheet(); |
| 6800 | |
| 6801 | $worksheet2->right_to_left(); |
| 6802 | |
| 6803 | $worksheet1->write(0, 0, 'Hello'); # A1, B1, C1, ... |
| 6804 | $worksheet2->write(0, 0, 'Hello'); # ..., C1, B1, A1 |
| 6805 | |
| 6806 | |
| 6807 | |
| 6808 | |
| 6809 | |
| 6810 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/right_to_left.pl> |
| 6811 | |
| 6812 | =head2 Example: row_wrap.pl |
| 6813 | |
| 6814 | |
| 6815 | |
| 6816 | Demonstrates how to wrap data from one worksheet onto another. |
| 6817 | |
| 6818 | Excel has a row limit of 65536 rows. Sometimes the amount of row data to be |
| 6819 | written to a file is greater than this limit. In this case it is a useful |
| 6820 | technique to wrap the data from one worksheet onto the next so that we get |
| 6821 | something like the following: |
| 6822 | |
| 6823 | Sheet1 Row 1 - 65536 |
| 6824 | Sheet2 Row 65537 - 131072 |
| 6825 | Sheet3 Row 131073 - ... |
| 6826 | |
| 6827 | In order to achieve this we use a single worksheet reference and |
| 6828 | reinitialise it to point to a new worksheet when required. |
| 6829 | |
| 6830 | |
| 6831 | |
| 6832 | =begin html |
| 6833 | |
| 6834 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/row_wrap.jpg" width="640" height="420" alt="Output from row_wrap.pl" /></center></p> |
| 6835 | |
| 6836 | =end html |
| 6837 | |
| 6838 | Source code for this example: |
| 6839 | |
| 6840 | #!/usr/bin/perl -w |
| 6841 | |
| 6842 | ############################################################################## |
| 6843 | # |
| 6844 | # Demonstrates how to wrap data from one worksheet onto another. |
| 6845 | # |
| 6846 | # Excel has a row limit of 65536 rows. Sometimes the amount of row data to be |
| 6847 | # written to a file is greater than this limit. In this case it is a useful |
| 6848 | # technique to wrap the data from one worksheet onto the next so that we get |
| 6849 | # something like the following: |
| 6850 | # |
| 6851 | # Sheet1 Row 1 - 65536 |
| 6852 | # Sheet2 Row 65537 - 131072 |
| 6853 | # Sheet3 Row 131073 - ... |
| 6854 | # |
| 6855 | # In order to achieve this we use a single worksheet reference and |
| 6856 | # reinitialise it to point to a new worksheet when required. |
| 6857 | # |
| 6858 | # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org |
| 6859 | # |
| 6860 | |
| 6861 | |
| 6862 | use strict; |
| 6863 | use Spreadsheet::WriteExcel; |
| 6864 | |
| 6865 | my $workbook = Spreadsheet::WriteExcel->new('row_wrap.xls'); |
| 6866 | my $worksheet = $workbook->add_worksheet(); |
| 6867 | |
| 6868 | |
| 6869 | # Worksheet formatting. |
| 6870 | $worksheet->set_column('A:A', 20); |
| 6871 | |
| 6872 | |
| 6873 | # For the sake of this example we will use a small row limit. In order to use |
| 6874 | # the entire row range set the $row_limit to 65536. |
| 6875 | my $row_limit = 10; |
| 6876 | my $row = 0; |
| 6877 | |
| 6878 | for my $count (1 .. 2 * $row_limit +10) { |
| 6879 | |
| 6880 | # When we hit the row limit we redirect the output |
| 6881 | # to a new worksheet and reset the row number. |
| 6882 | if ($row == $row_limit) { |
| 6883 | $worksheet = $workbook->add_worksheet(); |
| 6884 | $row = 0; |
| 6885 | |
| 6886 | # Repeat any worksheet formatting. |
| 6887 | $worksheet->set_column('A:A', 20); |
| 6888 | } |
| 6889 | |
| 6890 | $worksheet->write($row, 0, "This is row $count"); |
| 6891 | $row++; |
| 6892 | } |
| 6893 | |
| 6894 | |
| 6895 | |
| 6896 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/row_wrap.pl> |
| 6897 | |
| 6898 | =head2 Example: sales.pl |
| 6899 | |
| 6900 | |
| 6901 | |
| 6902 | Example of a sales worksheet to demonstrate several different features. |
| 6903 | Also uses functions from the L<Spreadsheet::WriteExcel::Utility> module. |
| 6904 | |
| 6905 | |
| 6906 | |
| 6907 | =begin html |
| 6908 | |
| 6909 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/sales.jpg" width="640" height="420" alt="Output from sales.pl" /></center></p> |
| 6910 | |
| 6911 | =end html |
| 6912 | |
| 6913 | Source code for this example: |
| 6914 | |
| 6915 | #!/usr/bin/perl -w |
| 6916 | |
| 6917 | ############################################################################### |
| 6918 | # |
| 6919 | # Example of a sales worksheet to demonstrate several different features. |
| 6920 | # Also uses functions from the L<Spreadsheet::WriteExcel::Utility> module. |
| 6921 | # |
| 6922 | # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org |
| 6923 | # |
| 6924 | |
| 6925 | use strict; |
| 6926 | use Spreadsheet::WriteExcel; |
| 6927 | use Spreadsheet::WriteExcel::Utility; |
| 6928 | |
| 6929 | # Create a new workbook and add a worksheet |
| 6930 | my $workbook = Spreadsheet::WriteExcel->new("sales.xls"); |
| 6931 | my $worksheet = $workbook->add_worksheet('May Sales'); |
| 6932 | |
| 6933 | |
| 6934 | # Set up some formats |
| 6935 | my %heading = ( |
| 6936 | bold => 1, |
| 6937 | pattern => 1, |
| 6938 | fg_color => 19, |
| 6939 | border => 1, |
| 6940 | align => 'center', |
| 6941 | ); |
| 6942 | |
| 6943 | my %total = ( |
| 6944 | bold => 1, |
| 6945 | top => 1, |
| 6946 | num_format => '$#,##0.00' |
| 6947 | ); |
| 6948 | |
| 6949 | my $heading = $workbook->add_format(%heading); |
| 6950 | my $total_format = $workbook->add_format(%total); |
| 6951 | my $price_format = $workbook->add_format(num_format => '$#,##0.00'); |
| 6952 | my $date_format = $workbook->add_format(num_format => 'mmm d yyy'); |
| 6953 | |
| 6954 | |
| 6955 | # Write the main headings |
| 6956 | $worksheet->freeze_panes(1); # Freeze the first row |
| 6957 | $worksheet->write('A1', 'Item', $heading); |
| 6958 | $worksheet->write('B1', 'Quantity', $heading); |
| 6959 | $worksheet->write('C1', 'Price', $heading); |
| 6960 | $worksheet->write('D1', 'Total', $heading); |
| 6961 | $worksheet->write('E1', 'Date', $heading); |
| 6962 | |
| 6963 | # Set the column widths |
| 6964 | $worksheet->set_column('A:A', 25); |
| 6965 | $worksheet->set_column('B:B', 10); |
| 6966 | $worksheet->set_column('C:E', 16); |
| 6967 | |
| 6968 | |
| 6969 | # Extract the sales data from the __DATA__ section at the end of the file. |
| 6970 | # In reality this information would probably come from a database |
| 6971 | my @sales; |
| 6972 | |
| 6973 | foreach my $line (<DATA>) { |
| 6974 | chomp $line; |
| 6975 | next if $line eq ''; |
| 6976 | # Simple-minded processing of CSV data. Refer to the Text::CSV_XS |
| 6977 | # and Text::xSV modules for a more complete CSV handling. |
| 6978 | my @items = split /,/, $line; |
| 6979 | push @sales, \@items; |
| 6980 | } |
| 6981 | |
| 6982 | |
| 6983 | # Write out the items from each row |
| 6984 | my $row = 1; |
| 6985 | foreach my $sale (@sales) { |
| 6986 | |
| 6987 | $worksheet->write($row, 0, @$sale[0]); |
| 6988 | $worksheet->write($row, 1, @$sale[1]); |
| 6989 | $worksheet->write($row, 2, @$sale[2], $price_format); |
| 6990 | |
| 6991 | # Create a formula like '=B2*C2' |
| 6992 | my $formula = '=' |
| 6993 | . xl_rowcol_to_cell($row, 1) |
| 6994 | . "*" |
| 6995 | . xl_rowcol_to_cell($row, 2); |
| 6996 | |
| 6997 | $worksheet->write($row, 3, $formula, $price_format); |
| 6998 | |
| 6999 | # Parse the date |
| 7000 | my $date = xl_decode_date_US(@$sale[3]); |
| 7001 | $worksheet->write($row, 4, $date, $date_format); |
| 7002 | $row++; |
| 7003 | } |
| 7004 | |
| 7005 | # Create a formula to sum the totals, like '=SUM(D2:D6)' |
| 7006 | my $total = '=SUM(D2:' |
| 7007 | . xl_rowcol_to_cell($row-1, 3) |
| 7008 | . ")"; |
| 7009 | |
| 7010 | $worksheet->write($row, 3, $total, $total_format); |
| 7011 | |
| 7012 | |
| 7013 | |
| 7014 | __DATA__ |
| 7015 | 586 card,20,125.50,5/12/01 |
| 7016 | Flat Screen Monitor,1,1300.00,5/12/01 |
| 7017 | 64 MB dimms,45,49.99,5/13/01 |
| 7018 | 15 GB HD,12,300.00,5/13/01 |
| 7019 | Speakers (pair),5,15.50,5/14/01 |
| 7020 | |
| 7021 | |
| 7022 | |
| 7023 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/sales.pl> |
| 7024 | |
| 7025 | =head2 Example: sendmail.pl |
| 7026 | |
| 7027 | |
| 7028 | |
| 7029 | Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel |
| 7030 | file as an attachment. |
| 7031 | |
| 7032 | The main thing is to ensure that you close() the Worbook before you send it. |
| 7033 | |
| 7034 | See the L<Mail::Sender> module for further details. |
| 7035 | |
| 7036 | |
| 7037 | |
| 7038 | #!/usr/bin/perl -w |
| 7039 | |
| 7040 | ############################################################################### |
| 7041 | # |
| 7042 | # Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel |
| 7043 | # file as an attachment. |
| 7044 | # |
| 7045 | # The main thing is to ensure that you close() the Worbook before you send it. |
| 7046 | # |
| 7047 | # See the L<Mail::Sender> module for further details. |
| 7048 | # |
| 7049 | # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org |
| 7050 | # |
| 7051 | |
| 7052 | |
| 7053 | use strict; |
| 7054 | use Spreadsheet::WriteExcel; |
| 7055 | use Mail::Sender; |
| 7056 | |
| 7057 | # Create an Excel file |
| 7058 | my $workbook = Spreadsheet::WriteExcel->new("sendmail.xls"); |
| 7059 | my $worksheet = $workbook->add_worksheet; |
| 7060 | |
| 7061 | $worksheet->write('A1', "Hello World!"); |
| 7062 | |
| 7063 | $workbook->close(); # Must close before sending |
| 7064 | |
| 7065 | |
| 7066 | |
| 7067 | # Send the file. Change all variables to suit |
| 7068 | my $sender = new Mail::Sender |
| 7069 | { |
| 7070 | smtp => '123.123.123.123', |
| 7071 | from => 'Someone' |
| 7072 | }; |
| 7073 | |
| 7074 | $sender->MailFile( |
| 7075 | { |
| 7076 | to => 'another@mail.com', |
| 7077 | subject => 'Excel file', |
| 7078 | msg => "Here is the data.\n", |
| 7079 | file => 'mail.xls', |
| 7080 | }); |
| 7081 | |
| 7082 | |
| 7083 | |
| 7084 | |
| 7085 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/sendmail.pl> |
| 7086 | |
| 7087 | =head2 Example: stats_ext.pl |
| 7088 | |
| 7089 | |
| 7090 | |
| 7091 | Example of formatting using the Spreadsheet::WriteExcel module |
| 7092 | |
| 7093 | This is a simple example of how to use functions that reference cells in |
| 7094 | other worksheets within the same workbook. |
| 7095 | |
| 7096 | |
| 7097 | |
| 7098 | =begin html |
| 7099 | |
| 7100 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stats_ext.jpg" width="640" height="420" alt="Output from stats_ext.pl" /></center></p> |
| 7101 | |
| 7102 | =end html |
| 7103 | |
| 7104 | Source code for this example: |
| 7105 | |
| 7106 | #!/usr/bin/perl -w |
| 7107 | |
| 7108 | ############################################################################### |
| 7109 | # |
| 7110 | # Example of formatting using the Spreadsheet::WriteExcel module |
| 7111 | # |
| 7112 | # This is a simple example of how to use functions that reference cells in |
| 7113 | # other worksheets within the same workbook. |
| 7114 | # |
| 7115 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 7116 | # |
| 7117 | |
| 7118 | use strict; |
| 7119 | use Spreadsheet::WriteExcel; |
| 7120 | |
| 7121 | # Create a new workbook and add a worksheet |
| 7122 | my $workbook = Spreadsheet::WriteExcel->new("stats_ext.xls"); |
| 7123 | my $worksheet1 = $workbook->add_worksheet('Test results'); |
| 7124 | my $worksheet2 = $workbook->add_worksheet('Data'); |
| 7125 | |
| 7126 | # Set the column width for columns 1 |
| 7127 | $worksheet1->set_column('A:A', 20); |
| 7128 | |
| 7129 | |
| 7130 | # Create a format for the headings |
| 7131 | my $heading = $workbook->add_format(); |
| 7132 | $heading->set_bold(); |
| 7133 | |
| 7134 | # Create a numerical format |
| 7135 | my $numformat = $workbook->add_format(); |
| 7136 | $numformat->set_num_format('0.00'); |
| 7137 | |
| 7138 | |
| 7139 | |
| 7140 | |
| 7141 | # Write some statistical functions |
| 7142 | $worksheet1->write('A1', 'Count', $heading); |
| 7143 | $worksheet1->write('B1', '=COUNT(Data!B2:B9)'); |
| 7144 | |
| 7145 | $worksheet1->write('A2', 'Sum', $heading); |
| 7146 | $worksheet1->write('B2', '=SUM(Data!B2:B9)'); |
| 7147 | |
| 7148 | $worksheet1->write('A3', 'Average', $heading); |
| 7149 | $worksheet1->write('B3', '=AVERAGE(Data!B2:B9)'); |
| 7150 | |
| 7151 | $worksheet1->write('A4', 'Min', $heading); |
| 7152 | $worksheet1->write('B4', '=MIN(Data!B2:B9)'); |
| 7153 | |
| 7154 | $worksheet1->write('A5', 'Max', $heading); |
| 7155 | $worksheet1->write('B5', '=MAX(Data!B2:B9)'); |
| 7156 | |
| 7157 | $worksheet1->write('A6', 'Standard Deviation', $heading); |
| 7158 | $worksheet1->write('B6', '=STDEV(Data!B2:B9)'); |
| 7159 | |
| 7160 | $worksheet1->write('A7', 'Kurtosis', $heading); |
| 7161 | $worksheet1->write('B7', '=KURT(Data!B2:B9)'); |
| 7162 | |
| 7163 | |
| 7164 | # Write the sample data |
| 7165 | $worksheet2->write('A1', 'Sample', $heading); |
| 7166 | $worksheet2->write('A2', 1); |
| 7167 | $worksheet2->write('A3', 2); |
| 7168 | $worksheet2->write('A4', 3); |
| 7169 | $worksheet2->write('A5', 4); |
| 7170 | $worksheet2->write('A6', 5); |
| 7171 | $worksheet2->write('A7', 6); |
| 7172 | $worksheet2->write('A8', 7); |
| 7173 | $worksheet2->write('A9', 8); |
| 7174 | |
| 7175 | $worksheet2->write('B1', 'Length', $heading); |
| 7176 | $worksheet2->write('B2', 25.4, $numformat); |
| 7177 | $worksheet2->write('B3', 25.4, $numformat); |
| 7178 | $worksheet2->write('B4', 24.8, $numformat); |
| 7179 | $worksheet2->write('B5', 25.0, $numformat); |
| 7180 | $worksheet2->write('B6', 25.3, $numformat); |
| 7181 | $worksheet2->write('B7', 24.9, $numformat); |
| 7182 | $worksheet2->write('B8', 25.2, $numformat); |
| 7183 | $worksheet2->write('B9', 24.8, $numformat); |
| 7184 | |
| 7185 | |
| 7186 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stats_ext.pl> |
| 7187 | |
| 7188 | =head2 Example: stocks.pl |
| 7189 | |
| 7190 | |
| 7191 | |
| 7192 | Example of formatting using the Spreadsheet::WriteExcel module |
| 7193 | |
| 7194 | This example shows how to use a conditional numerical format |
| 7195 | with colours to indicate if a share price has gone up or down. |
| 7196 | |
| 7197 | |
| 7198 | |
| 7199 | =begin html |
| 7200 | |
| 7201 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stocks.jpg" width="640" height="420" alt="Output from stocks.pl" /></center></p> |
| 7202 | |
| 7203 | =end html |
| 7204 | |
| 7205 | Source code for this example: |
| 7206 | |
| 7207 | #!/usr/bin/perl -w |
| 7208 | |
| 7209 | ############################################################################### |
| 7210 | # |
| 7211 | # Example of formatting using the Spreadsheet::WriteExcel module |
| 7212 | # |
| 7213 | # This example shows how to use a conditional numerical format |
| 7214 | # with colours to indicate if a share price has gone up or down. |
| 7215 | # |
| 7216 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 7217 | # |
| 7218 | |
| 7219 | use strict; |
| 7220 | use Spreadsheet::WriteExcel; |
| 7221 | |
| 7222 | # Create a new workbook and add a worksheet |
| 7223 | my $workbook = Spreadsheet::WriteExcel->new("stocks.xls"); |
| 7224 | my $worksheet = $workbook->add_worksheet(); |
| 7225 | |
| 7226 | # Set the column width for columns 1, 2, 3 and 4 |
| 7227 | $worksheet->set_column(0, 3, 15); |
| 7228 | |
| 7229 | |
| 7230 | # Create a format for the column headings |
| 7231 | my $header = $workbook->add_format(); |
| 7232 | $header->set_bold(); |
| 7233 | $header->set_size(12); |
| 7234 | $header->set_color('blue'); |
| 7235 | |
| 7236 | |
| 7237 | # Create a format for the stock price |
| 7238 | my $f_price = $workbook->add_format(); |
| 7239 | $f_price->set_align('left'); |
| 7240 | $f_price->set_num_format('$0.00'); |
| 7241 | |
| 7242 | |
| 7243 | # Create a format for the stock volume |
| 7244 | my $f_volume = $workbook->add_format(); |
| 7245 | $f_volume->set_align('left'); |
| 7246 | $f_volume->set_num_format('#,##0'); |
| 7247 | |
| 7248 | |
| 7249 | # Create a format for the price change. This is an example of a conditional |
| 7250 | # format. The number is formatted as a percentage. If it is positive it is |
| 7251 | # formatted in green, if it is negative it is formatted in red and if it is |
| 7252 | # zero it is formatted as the default font colour (in this case black). |
| 7253 | # Note: the [Green] format produces an unappealing lime green. Try |
| 7254 | # [Color 10] instead for a dark green. |
| 7255 | # |
| 7256 | my $f_change = $workbook->add_format(); |
| 7257 | $f_change->set_align('left'); |
| 7258 | $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%'); |
| 7259 | |
| 7260 | |
| 7261 | # Write out the data |
| 7262 | $worksheet->write(0, 0, 'Company', $header); |
| 7263 | $worksheet->write(0, 1, 'Price', $header); |
| 7264 | $worksheet->write(0, 2, 'Volume', $header); |
| 7265 | $worksheet->write(0, 3, 'Change', $header); |
| 7266 | |
| 7267 | $worksheet->write(1, 0, 'Damage Inc.' ); |
| 7268 | $worksheet->write(1, 1, 30.25, $f_price); # $30.25 |
| 7269 | $worksheet->write(1, 2, 1234567, $f_volume); # 1,234,567 |
| 7270 | $worksheet->write(1, 3, 0.085, $f_change); # 8.5% in green |
| 7271 | |
| 7272 | $worksheet->write(2, 0, 'Dump Corp.' ); |
| 7273 | $worksheet->write(2, 1, 1.56, $f_price); # $1.56 |
| 7274 | $worksheet->write(2, 2, 7564, $f_volume); # 7,564 |
| 7275 | $worksheet->write(2, 3, -0.015, $f_change); # -1.5% in red |
| 7276 | |
| 7277 | $worksheet->write(3, 0, 'Rev Ltd.' ); |
| 7278 | $worksheet->write(3, 1, 0.13, $f_price); # $0.13 |
| 7279 | $worksheet->write(3, 2, 321, $f_volume); # 321 |
| 7280 | $worksheet->write(3, 3, 0, $f_change); # 0 in the font color (black) |
| 7281 | |
| 7282 | |
| 7283 | |
| 7284 | |
| 7285 | |
| 7286 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stocks.pl> |
| 7287 | |
| 7288 | =head2 Example: tab_colors.pl |
| 7289 | |
| 7290 | |
| 7291 | |
| 7292 | Example of how to set Excel worksheet tab colours. |
| 7293 | |
| 7294 | |
| 7295 | |
| 7296 | =begin html |
| 7297 | |
| 7298 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/tab_colors.jpg" width="640" height="420" alt="Output from tab_colors.pl" /></center></p> |
| 7299 | |
| 7300 | =end html |
| 7301 | |
| 7302 | Source code for this example: |
| 7303 | |
| 7304 | #!/usr/bin/perl -w |
| 7305 | |
| 7306 | ####################################################################### |
| 7307 | # |
| 7308 | # Example of how to set Excel worksheet tab colours. |
| 7309 | # |
| 7310 | # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org |
| 7311 | # |
| 7312 | |
| 7313 | use strict; |
| 7314 | use Spreadsheet::WriteExcel; |
| 7315 | |
| 7316 | |
| 7317 | my $workbook = Spreadsheet::WriteExcel->new('tab_colors.xls'); |
| 7318 | |
| 7319 | my $worksheet1 = $workbook->add_worksheet(); |
| 7320 | my $worksheet2 = $workbook->add_worksheet(); |
| 7321 | my $worksheet3 = $workbook->add_worksheet(); |
| 7322 | my $worksheet4 = $workbook->add_worksheet(); |
| 7323 | |
| 7324 | # Worksheet1 will have the default tab colour. |
| 7325 | $worksheet2->set_tab_color('red'); |
| 7326 | $worksheet3->set_tab_color('green'); |
| 7327 | $worksheet4->set_tab_color(0x35); # Orange |
| 7328 | |
| 7329 | |
| 7330 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/tab_colors.pl> |
| 7331 | |
| 7332 | =head2 Example: textwrap.pl |
| 7333 | |
| 7334 | |
| 7335 | |
| 7336 | Example of formatting using the Spreadsheet::WriteExcel module |
| 7337 | |
| 7338 | This example shows how to wrap text in a cell. There are two alternatives, |
| 7339 | vertical justification and text wrap. |
| 7340 | |
| 7341 | With vertical justification the text is wrapped automatically to fit the |
| 7342 | column width. With text wrap you must specify a newline with an embedded \n. |
| 7343 | |
| 7344 | |
| 7345 | |
| 7346 | =begin html |
| 7347 | |
| 7348 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/textwrap.jpg" width="640" height="420" alt="Output from textwrap.pl" /></center></p> |
| 7349 | |
| 7350 | =end html |
| 7351 | |
| 7352 | Source code for this example: |
| 7353 | |
| 7354 | #!/usr/bin/perl -w |
| 7355 | |
| 7356 | ############################################################################### |
| 7357 | # |
| 7358 | # Example of formatting using the Spreadsheet::WriteExcel module |
| 7359 | # |
| 7360 | # This example shows how to wrap text in a cell. There are two alternatives, |
| 7361 | # vertical justification and text wrap. |
| 7362 | # |
| 7363 | # With vertical justification the text is wrapped automatically to fit the |
| 7364 | # column width. With text wrap you must specify a newline with an embedded \n. |
| 7365 | # |
| 7366 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 7367 | # |
| 7368 | |
| 7369 | use strict; |
| 7370 | use Spreadsheet::WriteExcel; |
| 7371 | |
| 7372 | # Create a new workbook and add a worksheet |
| 7373 | my $workbook = Spreadsheet::WriteExcel->new("textwrap.xls"); |
| 7374 | my $worksheet = $workbook->add_worksheet(); |
| 7375 | |
| 7376 | # Set the column width for columns 1, 2 and 3 |
| 7377 | $worksheet->set_column(1, 1, 24); |
| 7378 | $worksheet->set_column(2, 2, 34); |
| 7379 | $worksheet->set_column(3, 3, 34); |
| 7380 | |
| 7381 | # Set the row height for rows 1, 4, and 6. The height of row 2 will adjust |
| 7382 | # automatically to fit the text. |
| 7383 | # |
| 7384 | $worksheet->set_row(0, 30); |
| 7385 | $worksheet->set_row(3, 40); |
| 7386 | $worksheet->set_row(5, 80); |
| 7387 | |
| 7388 | |
| 7389 | # No newlines |
| 7390 | my $str1 = "For whatever we lose (like a you or a me) "; |
| 7391 | $str1 .= "it's always ourselves we find in the sea"; |
| 7392 | |
| 7393 | # Embedded newlines |
| 7394 | my $str2 = "For whatever we lose\n(like a you or a me)\n"; |
| 7395 | $str2 .= "it's always ourselves\nwe find in the sea"; |
| 7396 | |
| 7397 | |
| 7398 | # Create a format for the column headings |
| 7399 | my $header = $workbook->add_format(); |
| 7400 | $header->set_bold(); |
| 7401 | $header->set_font("Courier New"); |
| 7402 | $header->set_align('center'); |
| 7403 | $header->set_align('vcenter'); |
| 7404 | |
| 7405 | # Create a "vertical justification" format |
| 7406 | my $format1 = $workbook->add_format(); |
| 7407 | $format1->set_align('vjustify'); |
| 7408 | |
| 7409 | # Create a "text wrap" format |
| 7410 | my $format2 = $workbook->add_format(); |
| 7411 | $format2->set_text_wrap(); |
| 7412 | |
| 7413 | # Write the headers |
| 7414 | $worksheet->write(0, 1, "set_align('vjustify')", $header); |
| 7415 | $worksheet->write(0, 2, "set_align('vjustify')", $header); |
| 7416 | $worksheet->write(0, 3, "set_text_wrap()", $header); |
| 7417 | |
| 7418 | # Write some examples |
| 7419 | $worksheet->write(1, 1, $str1, $format1); |
| 7420 | $worksheet->write(1, 2, $str1, $format1); |
| 7421 | $worksheet->write(1, 3, $str2, $format2); |
| 7422 | |
| 7423 | $worksheet->write(3, 1, $str1, $format1); |
| 7424 | $worksheet->write(3, 2, $str1, $format1); |
| 7425 | $worksheet->write(3, 3, $str2, $format2); |
| 7426 | |
| 7427 | $worksheet->write(5, 1, $str1, $format1); |
| 7428 | $worksheet->write(5, 2, $str1, $format1); |
| 7429 | $worksheet->write(5, 3, $str2, $format2); |
| 7430 | |
| 7431 | |
| 7432 | |
| 7433 | |
| 7434 | |
| 7435 | |
| 7436 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/textwrap.pl> |
| 7437 | |
| 7438 | =head2 Example: win32ole.pl |
| 7439 | |
| 7440 | |
| 7441 | |
| 7442 | This is a simple example of how to create an Excel file using the |
| 7443 | Win32::OLE module for the sake of comparison. |
| 7444 | |
| 7445 | |
| 7446 | |
| 7447 | #!/usr/bin/perl -w |
| 7448 | |
| 7449 | ############################################################################### |
| 7450 | # |
| 7451 | # This is a simple example of how to create an Excel file using the |
| 7452 | # Win32::OLE module for the sake of comparison. |
| 7453 | # |
| 7454 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 7455 | # |
| 7456 | |
| 7457 | use strict; |
| 7458 | use Cwd; |
| 7459 | use Win32::OLE; |
| 7460 | use Win32::OLE::Const 'Microsoft Excel'; |
| 7461 | |
| 7462 | |
| 7463 | my $application = Win32::OLE->new("Excel.Application"); |
| 7464 | my $workbook = $application->Workbooks->Add; |
| 7465 | my $worksheet = $workbook->Worksheets(1); |
| 7466 | |
| 7467 | $worksheet->Cells(1,1)->{Value} = "Hello World"; |
| 7468 | $worksheet->Cells(2,1)->{Value} = "One"; |
| 7469 | $worksheet->Cells(3,1)->{Value} = "Two"; |
| 7470 | $worksheet->Cells(4,1)->{Value} = 3; |
| 7471 | $worksheet->Cells(5,1)->{Value} = 4.0000001; |
| 7472 | |
| 7473 | # Add some formatting |
| 7474 | $worksheet->Cells(1,1)->Font->{Bold} = "True"; |
| 7475 | $worksheet->Cells(1,1)->Font->{Size} = 16; |
| 7476 | $worksheet->Cells(1,1)->Font->{ColorIndex} = 3; |
| 7477 | $worksheet->Columns("A:A")->{ColumnWidth} = 25; |
| 7478 | |
| 7479 | # Write a hyperlink |
| 7480 | my $range = $worksheet->Range("A7:A7"); |
| 7481 | $worksheet->Hyperlinks->Add({ Anchor => $range, Address => "http://www.perl.com/"}); |
| 7482 | |
| 7483 | # Get current directory using Cwd.pm |
| 7484 | my $dir = cwd(); |
| 7485 | |
| 7486 | $workbook->SaveAs({ |
| 7487 | FileName => $dir . '/win32ole.xls', |
| 7488 | FileFormat => xlNormal, |
| 7489 | }); |
| 7490 | $workbook->Close; |
| 7491 | |
| 7492 | |
| 7493 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/win32ole.pl> |
| 7494 | |
| 7495 | =head2 Example: write_arrays.pl |
| 7496 | |
| 7497 | |
| 7498 | |
| 7499 | Example of how to use the Spreadsheet::WriteExcel module to |
| 7500 | write 1D and 2D arrays of data. |
| 7501 | |
| 7502 | To find out more about array references refer(!!) to the perlref and |
| 7503 | perlreftut manpages. To find out more about 2D arrays or "list of |
| 7504 | lists" refer to the perllol manpage. |
| 7505 | |
| 7506 | |
| 7507 | |
| 7508 | =begin html |
| 7509 | |
| 7510 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_arrays.jpg" width="640" height="420" alt="Output from write_arrays.pl" /></center></p> |
| 7511 | |
| 7512 | =end html |
| 7513 | |
| 7514 | Source code for this example: |
| 7515 | |
| 7516 | #!/usr/bin/perl -w |
| 7517 | |
| 7518 | ####################################################################### |
| 7519 | # |
| 7520 | # Example of how to use the Spreadsheet::WriteExcel module to |
| 7521 | # write 1D and 2D arrays of data. |
| 7522 | # |
| 7523 | # To find out more about array references refer(!!) to the perlref and |
| 7524 | # perlreftut manpages. To find out more about 2D arrays or "list of |
| 7525 | # lists" refer to the perllol manpage. |
| 7526 | # |
| 7527 | # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org |
| 7528 | # |
| 7529 | |
| 7530 | |
| 7531 | use strict; |
| 7532 | use Spreadsheet::WriteExcel; |
| 7533 | |
| 7534 | |
| 7535 | my $workbook = Spreadsheet::WriteExcel->new("write_arrays.xls"); |
| 7536 | my $worksheet1 = $workbook->add_worksheet('Example 1'); |
| 7537 | my $worksheet2 = $workbook->add_worksheet('Example 2'); |
| 7538 | my $worksheet3 = $workbook->add_worksheet('Example 3'); |
| 7539 | my $worksheet4 = $workbook->add_worksheet('Example 4'); |
| 7540 | my $worksheet5 = $workbook->add_worksheet('Example 5'); |
| 7541 | my $worksheet6 = $workbook->add_worksheet('Example 6'); |
| 7542 | my $worksheet7 = $workbook->add_worksheet('Example 7'); |
| 7543 | my $worksheet8 = $workbook->add_worksheet('Example 8'); |
| 7544 | |
| 7545 | my $format = $workbook->add_format(color => 'red', bold => 1); |
| 7546 | |
| 7547 | |
| 7548 | # Data arrays used in the following examples. |
| 7549 | # undef values are written as blank cells (with format if specified). |
| 7550 | # |
| 7551 | my @array = ( 'one', 'two', undef, 'four' ); |
| 7552 | |
| 7553 | my @array2d = ( |
| 7554 | ['maggie', 'milly', 'molly', 'may' ], |
| 7555 | [13, 14, 15, 16 ], |
| 7556 | ['shell', 'star', 'crab', 'stone'], |
| 7557 | ); |
| 7558 | |
| 7559 | |
| 7560 | # 1. Write a row of data using an array reference. |
| 7561 | $worksheet1->write('A1', \@array); |
| 7562 | |
| 7563 | # 2. Same as 1. above using an anonymous array ref. |
| 7564 | $worksheet2->write('A1', [ @array ]); |
| 7565 | |
| 7566 | # 3. Write a row of data using an explicit write_row() method call. |
| 7567 | # This is the same as calling write() in Ex. 1 above. |
| 7568 | # |
| 7569 | $worksheet3->write_row('A1', \@array); |
| 7570 | |
| 7571 | # 4. Write a column of data using the write_col() method call. |
| 7572 | $worksheet4->write_col('A1', \@array); |
| 7573 | |
| 7574 | # 5. Write a column of data using a ref to an array ref, i.e. a 2D array. |
| 7575 | $worksheet5->write('A1', [ \@array ]); |
| 7576 | |
| 7577 | # 6. Write a 2D array in col-row order. |
| 7578 | $worksheet6->write('A1', \@array2d); |
| 7579 | |
| 7580 | # 7. Write a 2D array in row-col order. |
| 7581 | $worksheet7->write_col('A1', \@array2d); |
| 7582 | |
| 7583 | # 8. Write a row of data with formatting. The blank cell is also formatted. |
| 7584 | $worksheet8->write('A1', \@array, $format); |
| 7585 | |
| 7586 | |
| 7587 | |
| 7588 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_arrays.pl> |
| 7589 | |
| 7590 | =head2 Example: write_handler1.pl |
| 7591 | |
| 7592 | |
| 7593 | |
| 7594 | Example of how to add a user defined data handler to the Spreadsheet:: |
| 7595 | WriteExcel write() method. |
| 7596 | |
| 7597 | The following example shows how to add a handler for a 7 digit ID number. |
| 7598 | |
| 7599 | |
| 7600 | |
| 7601 | |
| 7602 | =begin html |
| 7603 | |
| 7604 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler1.jpg" width="640" height="420" alt="Output from write_handler1.pl" /></center></p> |
| 7605 | |
| 7606 | =end html |
| 7607 | |
| 7608 | Source code for this example: |
| 7609 | |
| 7610 | #!/usr/bin/perl -w |
| 7611 | |
| 7612 | ############################################################################### |
| 7613 | # |
| 7614 | # Example of how to add a user defined data handler to the Spreadsheet:: |
| 7615 | # WriteExcel write() method. |
| 7616 | # |
| 7617 | # The following example shows how to add a handler for a 7 digit ID number. |
| 7618 | # |
| 7619 | # |
| 7620 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 7621 | # |
| 7622 | |
| 7623 | use strict; |
| 7624 | use Spreadsheet::WriteExcel; |
| 7625 | |
| 7626 | |
| 7627 | my $workbook = Spreadsheet::WriteExcel->new("write_handler1.xls"); |
| 7628 | my $worksheet = $workbook->add_worksheet(); |
| 7629 | |
| 7630 | |
| 7631 | ############################################################################### |
| 7632 | # |
| 7633 | # Add a handler for 7 digit id numbers. This is useful when you want a string |
| 7634 | # such as 0000001 written as a string instead of a number and thus preserve |
| 7635 | # the leading zeroes. |
| 7636 | # |
| 7637 | # Note: you can get the same effect using the keep_leading_zeros() method but |
| 7638 | # this serves as a simple example. |
| 7639 | # |
| 7640 | $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id); |
| 7641 | |
| 7642 | |
| 7643 | ############################################################################### |
| 7644 | # |
| 7645 | # The following function processes the data when a match is found. |
| 7646 | # |
| 7647 | sub write_my_id { |
| 7648 | |
| 7649 | my $worksheet = shift; |
| 7650 | |
| 7651 | return $worksheet->write_string(@_); |
| 7652 | } |
| 7653 | |
| 7654 | |
| 7655 | # This format maintains the cell as text even if it is edited. |
| 7656 | my $id_format = $workbook->add_format(num_format => '@'); |
| 7657 | |
| 7658 | |
| 7659 | # Write some numbers in the user defined format |
| 7660 | $worksheet->write('A1', '0000000', $id_format); |
| 7661 | $worksheet->write('A2', '0000001', $id_format); |
| 7662 | $worksheet->write('A3', '0004000', $id_format); |
| 7663 | $worksheet->write('A4', '1234567', $id_format); |
| 7664 | |
| 7665 | # Write some numbers that don't match the defined format |
| 7666 | $worksheet->write('A6', '000000', $id_format); |
| 7667 | $worksheet->write('A7', '000001', $id_format); |
| 7668 | $worksheet->write('A8', '004000', $id_format); |
| 7669 | $worksheet->write('A9', '123456', $id_format); |
| 7670 | |
| 7671 | |
| 7672 | __END__ |
| 7673 | |
| 7674 | |
| 7675 | |
| 7676 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler1.pl> |
| 7677 | |
| 7678 | =head2 Example: write_handler2.pl |
| 7679 | |
| 7680 | |
| 7681 | |
| 7682 | Example of how to add a user defined data handler to the Spreadsheet:: |
| 7683 | WriteExcel write() method. |
| 7684 | |
| 7685 | The following example shows how to add a handler for a 7 digit ID number. |
| 7686 | It adds an additional constraint to the write_handler1.pl in that it only |
| 7687 | filters data that isn't in the third column. |
| 7688 | |
| 7689 | |
| 7690 | |
| 7691 | |
| 7692 | =begin html |
| 7693 | |
| 7694 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler2.jpg" width="640" height="420" alt="Output from write_handler2.pl" /></center></p> |
| 7695 | |
| 7696 | =end html |
| 7697 | |
| 7698 | Source code for this example: |
| 7699 | |
| 7700 | #!/usr/bin/perl -w |
| 7701 | |
| 7702 | ############################################################################### |
| 7703 | # |
| 7704 | # Example of how to add a user defined data handler to the Spreadsheet:: |
| 7705 | # WriteExcel write() method. |
| 7706 | # |
| 7707 | # The following example shows how to add a handler for a 7 digit ID number. |
| 7708 | # It adds an additional constraint to the write_handler1.pl in that it only |
| 7709 | # filters data that isn't in the third column. |
| 7710 | # |
| 7711 | # |
| 7712 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 7713 | # |
| 7714 | |
| 7715 | use strict; |
| 7716 | use Spreadsheet::WriteExcel; |
| 7717 | |
| 7718 | |
| 7719 | my $workbook = Spreadsheet::WriteExcel->new("write_handler2.xls"); |
| 7720 | my $worksheet = $workbook->add_worksheet(); |
| 7721 | |
| 7722 | |
| 7723 | ############################################################################### |
| 7724 | # |
| 7725 | # Add a handler for 7 digit id numbers. This is useful when you want a string |
| 7726 | # such as 0000001 written as a string instead of a number and thus preserve |
| 7727 | # the leading zeroes. |
| 7728 | # |
| 7729 | # Note: you can get the same effect using the keep_leading_zeros() method but |
| 7730 | # this serves as a simple example. |
| 7731 | # |
| 7732 | $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id); |
| 7733 | |
| 7734 | |
| 7735 | ############################################################################### |
| 7736 | # |
| 7737 | # The following function processes the data when a match is found. The handler |
| 7738 | # is set up so that it only filters data if it is in the third column. |
| 7739 | # |
| 7740 | sub write_my_id { |
| 7741 | |
| 7742 | my $worksheet = shift; |
| 7743 | my $col = $_[1]; |
| 7744 | |
| 7745 | # col is zero based |
| 7746 | if ($col != 2) { |
| 7747 | return $worksheet->write_string(@_); |
| 7748 | } |
| 7749 | else { |
| 7750 | # Reject the match and return control to write() |
| 7751 | return undef; |
| 7752 | } |
| 7753 | |
| 7754 | } |
| 7755 | |
| 7756 | |
| 7757 | # This format maintains the cell as text even if it is edited. |
| 7758 | my $id_format = $workbook->add_format(num_format => '@'); |
| 7759 | |
| 7760 | |
| 7761 | # Write some numbers in the user defined format |
| 7762 | $worksheet->write('A1', '0000000', $id_format); |
| 7763 | $worksheet->write('B1', '0000001', $id_format); |
| 7764 | $worksheet->write('C1', '0000002', $id_format); |
| 7765 | $worksheet->write('D1', '0000003', $id_format); |
| 7766 | |
| 7767 | |
| 7768 | |
| 7769 | __END__ |
| 7770 | |
| 7771 | |
| 7772 | |
| 7773 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler2.pl> |
| 7774 | |
| 7775 | =head2 Example: write_handler3.pl |
| 7776 | |
| 7777 | |
| 7778 | |
| 7779 | Example of how to add a user defined data handler to the Spreadsheet:: |
| 7780 | WriteExcel write() method. |
| 7781 | |
| 7782 | The following example shows how to add a handler for dates in a specific |
| 7783 | format. |
| 7784 | |
| 7785 | See write_handler4.pl for a more rigorous example with error handling. |
| 7786 | |
| 7787 | |
| 7788 | |
| 7789 | =begin html |
| 7790 | |
| 7791 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler3.jpg" width="640" height="420" alt="Output from write_handler3.pl" /></center></p> |
| 7792 | |
| 7793 | =end html |
| 7794 | |
| 7795 | Source code for this example: |
| 7796 | |
| 7797 | #!/usr/bin/perl -w |
| 7798 | |
| 7799 | ############################################################################### |
| 7800 | # |
| 7801 | # Example of how to add a user defined data handler to the Spreadsheet:: |
| 7802 | # WriteExcel write() method. |
| 7803 | # |
| 7804 | # The following example shows how to add a handler for dates in a specific |
| 7805 | # format. |
| 7806 | # |
| 7807 | # See write_handler4.pl for a more rigorous example with error handling. |
| 7808 | # |
| 7809 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 7810 | # |
| 7811 | |
| 7812 | use strict; |
| 7813 | use Spreadsheet::WriteExcel; |
| 7814 | |
| 7815 | |
| 7816 | my $workbook = Spreadsheet::WriteExcel->new("write_handler3.xls"); |
| 7817 | my $worksheet = $workbook->add_worksheet(); |
| 7818 | my $date_format = $workbook->add_format(num_format => 'dd/mm/yy'); |
| 7819 | |
| 7820 | |
| 7821 | ############################################################################### |
| 7822 | # |
| 7823 | # Add a handler to match dates in the following format: d/m/yyyy |
| 7824 | # |
| 7825 | # The day and month can be single or double digits. |
| 7826 | # |
| 7827 | $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{4}$], \&write_my_date); |
| 7828 | |
| 7829 | |
| 7830 | ############################################################################### |
| 7831 | # |
| 7832 | # The following function processes the data when a match is found. |
| 7833 | # See write_handler4.pl for a more rigorous example with error handling. |
| 7834 | # |
| 7835 | sub write_my_date { |
| 7836 | |
| 7837 | my $worksheet = shift; |
| 7838 | my @args = @_; |
| 7839 | |
| 7840 | my $token = $args[2]; |
| 7841 | $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$]; |
| 7842 | |
| 7843 | # Change to the date format required by write_date_time(). |
| 7844 | my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1; |
| 7845 | |
| 7846 | $args[2] = $date; |
| 7847 | |
| 7848 | return $worksheet->write_date_time(@args); |
| 7849 | } |
| 7850 | |
| 7851 | |
| 7852 | # Write some dates in the user defined format |
| 7853 | $worksheet->write('A1', '22/12/2004', $date_format); |
| 7854 | $worksheet->write('A2', '1/1/1995', $date_format); |
| 7855 | $worksheet->write('A3', '01/01/1995', $date_format); |
| 7856 | |
| 7857 | |
| 7858 | |
| 7859 | __END__ |
| 7860 | |
| 7861 | |
| 7862 | |
| 7863 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler3.pl> |
| 7864 | |
| 7865 | =head2 Example: write_handler4.pl |
| 7866 | |
| 7867 | |
| 7868 | |
| 7869 | Example of how to add a user defined data handler to the Spreadsheet:: |
| 7870 | WriteExcel write() method. |
| 7871 | |
| 7872 | The following example shows how to add a handler for dates in a specific |
| 7873 | format. |
| 7874 | |
| 7875 | This is a more rigorous version of write_handler3.pl. |
| 7876 | |
| 7877 | |
| 7878 | |
| 7879 | =begin html |
| 7880 | |
| 7881 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler4.jpg" width="640" height="420" alt="Output from write_handler4.pl" /></center></p> |
| 7882 | |
| 7883 | =end html |
| 7884 | |
| 7885 | Source code for this example: |
| 7886 | |
| 7887 | #!/usr/bin/perl -w |
| 7888 | |
| 7889 | ############################################################################### |
| 7890 | # |
| 7891 | # Example of how to add a user defined data handler to the Spreadsheet:: |
| 7892 | # WriteExcel write() method. |
| 7893 | # |
| 7894 | # The following example shows how to add a handler for dates in a specific |
| 7895 | # format. |
| 7896 | # |
| 7897 | # This is a more rigorous version of write_handler3.pl. |
| 7898 | # |
| 7899 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 7900 | # |
| 7901 | |
| 7902 | use strict; |
| 7903 | use Spreadsheet::WriteExcel; |
| 7904 | |
| 7905 | |
| 7906 | my $workbook = Spreadsheet::WriteExcel->new("write_handler4.xls"); |
| 7907 | my $worksheet = $workbook->add_worksheet(); |
| 7908 | my $date_format = $workbook->add_format(num_format => 'dd/mm/yy'); |
| 7909 | |
| 7910 | |
| 7911 | ############################################################################### |
| 7912 | # |
| 7913 | # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy |
| 7914 | # |
| 7915 | # The day and month can be single or double digits and the year can be 2 or 4 |
| 7916 | # digits. |
| 7917 | # |
| 7918 | $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{2,4}$], \&write_my_date); |
| 7919 | |
| 7920 | |
| 7921 | ############################################################################### |
| 7922 | # |
| 7923 | # The following function processes the data when a match is found. |
| 7924 | # |
| 7925 | sub write_my_date { |
| 7926 | |
| 7927 | my $worksheet = shift; |
| 7928 | my @args = @_; |
| 7929 | |
| 7930 | my $token = $args[2]; |
| 7931 | |
| 7932 | if ($token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{2,4})$]) { |
| 7933 | |
| 7934 | my $day = $1; |
| 7935 | my $mon = $2; |
| 7936 | my $year = $3; |
| 7937 | |
| 7938 | # Use a window for 2 digit dates. This will keep some ragged Perl |
| 7939 | # programmer employed in thirty years time. :-) |
| 7940 | if (length $year == 2) { |
| 7941 | if ($year < 50) { |
| 7942 | $year += 2000; |
| 7943 | } |
| 7944 | else { |
| 7945 | $year += 1900; |
| 7946 | } |
| 7947 | } |
| 7948 | |
| 7949 | my $date = sprintf "%4d-%02d-%02dT", $year, $mon, $day; |
| 7950 | |
| 7951 | # Convert the ISO ISO8601 style string to an Excel date |
| 7952 | $date = $worksheet->convert_date_time($date); |
| 7953 | |
| 7954 | if (defined $date) { |
| 7955 | # Date was valid |
| 7956 | $args[2] = $date; |
| 7957 | return $worksheet->write_number(@args); |
| 7958 | } |
| 7959 | else { |
| 7960 | # Not a valid date therefore write as a string |
| 7961 | return $worksheet->write_string(@args); |
| 7962 | } |
| 7963 | } |
| 7964 | else { |
| 7965 | # Shouldn't happen if the same match is used in the re and sub. |
| 7966 | return undef; |
| 7967 | } |
| 7968 | } |
| 7969 | |
| 7970 | |
| 7971 | # Write some dates in the user defined format |
| 7972 | $worksheet->write('A1', '22/12/2004', $date_format); |
| 7973 | $worksheet->write('A2', '22/12/04', $date_format); |
| 7974 | $worksheet->write('A3', '2/12/04', $date_format); |
| 7975 | $worksheet->write('A4', '2/5/04', $date_format); |
| 7976 | $worksheet->write('A5', '2/5/95', $date_format); |
| 7977 | $worksheet->write('A6', '2/5/1995', $date_format); |
| 7978 | |
| 7979 | # Some erroneous dates |
| 7980 | $worksheet->write('A8', '2/5/1895', $date_format); # Date out of Excel range |
| 7981 | $worksheet->write('A9', '29/2/2003', $date_format); # Invalid leap day |
| 7982 | $worksheet->write('A10','50/50/50', $date_format); # Matches but isn't a date |
| 7983 | |
| 7984 | |
| 7985 | __END__ |
| 7986 | |
| 7987 | |
| 7988 | |
| 7989 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler4.pl> |
| 7990 | |
| 7991 | =head2 Example: write_to_scalar.pl |
| 7992 | |
| 7993 | |
| 7994 | |
| 7995 | An example of writing an Excel file to a Perl scalar using Spreadsheet:: |
| 7996 | WriteExcel and the new features of perl 5.8. |
| 7997 | |
| 7998 | For an examples of how to write to a scalar in versions prior to perl 5.8 |
| 7999 | see the filehandle.pl program and IO:Scalar. |
| 8000 | |
| 8001 | |
| 8002 | |
| 8003 | #!/usr/bin/perl -w |
| 8004 | |
| 8005 | ############################################################################## |
| 8006 | # |
| 8007 | # An example of writing an Excel file to a Perl scalar using Spreadsheet:: |
| 8008 | # WriteExcel and the new features of perl 5.8. |
| 8009 | # |
| 8010 | # For an examples of how to write to a scalar in versions prior to perl 5.8 |
| 8011 | # see the filehandle.pl program and IO:Scalar. |
| 8012 | # |
| 8013 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8014 | # |
| 8015 | |
| 8016 | use strict; |
| 8017 | use Spreadsheet::WriteExcel; |
| 8018 | |
| 8019 | require 5.008; |
| 8020 | |
| 8021 | |
| 8022 | # Use perl 5.8's feature of using a scalar as a filehandle. |
| 8023 | my $fh; |
| 8024 | my $str = ''; |
| 8025 | open $fh, '>', \$str or die "Failed to open filehandle: $!";; |
| 8026 | |
| 8027 | |
| 8028 | # Or replace the previous three lines with this: |
| 8029 | # open my $fh, '>', \my $str or die "Failed to open filehandle: $!"; |
| 8030 | |
| 8031 | |
| 8032 | # Spreadsheet::WriteExce accepts filehandle as well as file names. |
| 8033 | my $workbook = Spreadsheet::WriteExcel->new($fh); |
| 8034 | my $worksheet = $workbook->add_worksheet(); |
| 8035 | |
| 8036 | $worksheet->write(0, 0, "Hi Excel!"); |
| 8037 | |
| 8038 | $workbook->close(); |
| 8039 | |
| 8040 | |
| 8041 | # The Excel file in now in $str. Remember to binmode() the output |
| 8042 | # filehandle before printing it. |
| 8043 | binmode STDOUT; |
| 8044 | print $str; |
| 8045 | |
| 8046 | |
| 8047 | __END__ |
| 8048 | |
| 8049 | |
| 8050 | |
| 8051 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_to_scalar.pl> |
| 8052 | |
| 8053 | =head2 Example: unicode_utf16.pl |
| 8054 | |
| 8055 | |
| 8056 | |
| 8057 | A simple example of writing some Unicode text with Spreadsheet::WriteExcel. |
| 8058 | |
| 8059 | This example shows UTF16 encoding. With perl 5.8 it is also possible to use |
| 8060 | utf8 without modification. |
| 8061 | |
| 8062 | |
| 8063 | |
| 8064 | =begin html |
| 8065 | |
| 8066 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_utf16.jpg" width="640" height="420" alt="Output from unicode_utf16.pl" /></center></p> |
| 8067 | |
| 8068 | =end html |
| 8069 | |
| 8070 | Source code for this example: |
| 8071 | |
| 8072 | #!/usr/bin/perl -w |
| 8073 | |
| 8074 | ############################################################################## |
| 8075 | # |
| 8076 | # A simple example of writing some Unicode text with Spreadsheet::WriteExcel. |
| 8077 | # |
| 8078 | # This example shows UTF16 encoding. With perl 5.8 it is also possible to use |
| 8079 | # utf8 without modification. |
| 8080 | # |
| 8081 | # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org |
| 8082 | # |
| 8083 | |
| 8084 | |
| 8085 | use strict; |
| 8086 | use Spreadsheet::WriteExcel; |
| 8087 | |
| 8088 | |
| 8089 | my $workbook = Spreadsheet::WriteExcel->new('unicode_utf16.xls'); |
| 8090 | my $worksheet = $workbook->add_worksheet(); |
| 8091 | |
| 8092 | |
| 8093 | # Write the Unicode smiley face (with increased font for legibility) |
| 8094 | my $smiley = pack "n", 0x263a; |
| 8095 | my $big_font = $workbook->add_format(size => 40); |
| 8096 | |
| 8097 | $worksheet->write_utf16be_string('A3', $smiley, $big_font); |
| 8098 | |
| 8099 | |
| 8100 | # Write a phrase in Cyrillic |
| 8101 | my $uni_str = pack "H*", "042d0442043e002004440440043004370430002004". |
| 8102 | "3d043000200440044304410441043a043e043c0021"; |
| 8103 | |
| 8104 | $worksheet->write_utf16be_string('A5', $uni_str); |
| 8105 | |
| 8106 | |
| 8107 | $worksheet->write_utf16be_string('A7', pack "H*", "0074006500730074"); |
| 8108 | |
| 8109 | |
| 8110 | |
| 8111 | |
| 8112 | |
| 8113 | __END__ |
| 8114 | |
| 8115 | |
| 8116 | |
| 8117 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_utf16.pl> |
| 8118 | |
| 8119 | =head2 Example: unicode_utf16_japan.pl |
| 8120 | |
| 8121 | |
| 8122 | |
| 8123 | A simple example of writing some Unicode text with Spreadsheet::WriteExcel. |
| 8124 | |
| 8125 | This creates an Excel file with the word Nippon in 3 character sets. |
| 8126 | |
| 8127 | This example shows UTF16 encoding. With perl 5.8 it is also possible to use |
| 8128 | utf8 without modification. |
| 8129 | |
| 8130 | See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples. |
| 8131 | |
| 8132 | |
| 8133 | |
| 8134 | =begin html |
| 8135 | |
| 8136 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_utf16_japan.jpg" width="640" height="420" alt="Output from unicode_utf16_japan.pl" /></center></p> |
| 8137 | |
| 8138 | =end html |
| 8139 | |
| 8140 | Source code for this example: |
| 8141 | |
| 8142 | #!/usr/bin/perl -w |
| 8143 | |
| 8144 | ############################################################################## |
| 8145 | # |
| 8146 | # A simple example of writing some Unicode text with Spreadsheet::WriteExcel. |
| 8147 | # |
| 8148 | # This creates an Excel file with the word Nippon in 3 character sets. |
| 8149 | # |
| 8150 | # This example shows UTF16 encoding. With perl 5.8 it is also possible to use |
| 8151 | # utf8 without modification. |
| 8152 | # |
| 8153 | # See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples. |
| 8154 | # |
| 8155 | # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org |
| 8156 | # |
| 8157 | |
| 8158 | |
| 8159 | use strict; |
| 8160 | use Spreadsheet::WriteExcel; |
| 8161 | |
| 8162 | |
| 8163 | my $workbook = Spreadsheet::WriteExcel->new('unicode_utf16_japan.xls'); |
| 8164 | my $worksheet = $workbook->add_worksheet(); |
| 8165 | |
| 8166 | |
| 8167 | # Set a Unicode font. |
| 8168 | my $uni_font = $workbook->add_format(font => 'Arial Unicode MS'); |
| 8169 | |
| 8170 | |
| 8171 | # Create some UTF-16BE Unicode text. |
| 8172 | my $kanji = pack 'n*', 0x65e5, 0x672c; |
| 8173 | my $katakana = pack 'n*', 0xff86, 0xff8e, 0xff9d; |
| 8174 | my $hiragana = pack 'n*', 0x306b, 0x307b, 0x3093; |
| 8175 | |
| 8176 | |
| 8177 | |
| 8178 | $worksheet->write_utf16be_string('A1', $kanji, $uni_font); |
| 8179 | $worksheet->write_utf16be_string('A2', $katakana, $uni_font); |
| 8180 | $worksheet->write_utf16be_string('A3', $hiragana, $uni_font); |
| 8181 | |
| 8182 | |
| 8183 | $worksheet->write('B1', 'Kanji'); |
| 8184 | $worksheet->write('B2', 'Katakana'); |
| 8185 | $worksheet->write('B3', 'Hiragana'); |
| 8186 | |
| 8187 | |
| 8188 | __END__ |
| 8189 | |
| 8190 | |
| 8191 | |
| 8192 | |
| 8193 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_utf16_japan.pl> |
| 8194 | |
| 8195 | =head2 Example: unicode_cyrillic.pl |
| 8196 | |
| 8197 | |
| 8198 | |
| 8199 | A simple example of writing some Russian cyrillic text using |
| 8200 | Spreadsheet::WriteExcel and perl 5.8. |
| 8201 | |
| 8202 | |
| 8203 | |
| 8204 | =begin html |
| 8205 | |
| 8206 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cyrillic.jpg" width="640" height="420" alt="Output from unicode_cyrillic.pl" /></center></p> |
| 8207 | |
| 8208 | =end html |
| 8209 | |
| 8210 | Source code for this example: |
| 8211 | |
| 8212 | #!/usr/bin/perl -w |
| 8213 | |
| 8214 | ############################################################################## |
| 8215 | # |
| 8216 | # A simple example of writing some Russian cyrillic text using |
| 8217 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8218 | # |
| 8219 | # reverse('©'), March 2005, John McNamara, jmcnamara@cpan.org |
| 8220 | # |
| 8221 | |
| 8222 | |
| 8223 | |
| 8224 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8225 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8226 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8227 | # |
| 8228 | require 5.008; |
| 8229 | |
| 8230 | use strict; |
| 8231 | use Spreadsheet::WriteExcel; |
| 8232 | |
| 8233 | |
| 8234 | # In this example we generate utf8 strings from character data but in a |
| 8235 | # real application we would expect them to come from an external source. |
| 8236 | # |
| 8237 | |
| 8238 | |
| 8239 | # Create a Russian worksheet name in utf8. |
| 8240 | my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438, |
| 8241 | 0x0446, 0x0430; |
| 8242 | |
| 8243 | |
| 8244 | # Create a Russian string. |
| 8245 | my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, |
| 8246 | 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C, |
| 8247 | 0x0438, 0x0440, 0x0021; |
| 8248 | |
| 8249 | |
| 8250 | |
| 8251 | my $workbook = Spreadsheet::WriteExcel->new("unicode_cyrillic.xls"); |
| 8252 | my $worksheet = $workbook->add_worksheet($sheet . '1'); |
| 8253 | |
| 8254 | $worksheet->set_column('A:A', 18); |
| 8255 | $worksheet->write('A1', $str); |
| 8256 | |
| 8257 | |
| 8258 | __END__ |
| 8259 | |
| 8260 | |
| 8261 | |
| 8262 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cyrillic.pl> |
| 8263 | |
| 8264 | =head2 Example: unicode_list.pl |
| 8265 | |
| 8266 | |
| 8267 | |
| 8268 | A simple example using Spreadsheet::WriteExcel to display all available |
| 8269 | Unicode characters in a font. |
| 8270 | |
| 8271 | |
| 8272 | |
| 8273 | =begin html |
| 8274 | |
| 8275 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_list.jpg" width="640" height="420" alt="Output from unicode_list.pl" /></center></p> |
| 8276 | |
| 8277 | =end html |
| 8278 | |
| 8279 | Source code for this example: |
| 8280 | |
| 8281 | #!/usr/bin/perl -w |
| 8282 | |
| 8283 | ############################################################################## |
| 8284 | # |
| 8285 | # A simple example using Spreadsheet::WriteExcel to display all available |
| 8286 | # Unicode characters in a font. |
| 8287 | # |
| 8288 | # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org |
| 8289 | # |
| 8290 | |
| 8291 | use strict; |
| 8292 | use Spreadsheet::WriteExcel; |
| 8293 | |
| 8294 | |
| 8295 | my $workbook = Spreadsheet::WriteExcel->new('unicode_list.xls'); |
| 8296 | my $worksheet = $workbook->add_worksheet(); |
| 8297 | |
| 8298 | |
| 8299 | # Set a Unicode font. |
| 8300 | my $uni_font = $workbook->add_format(font => 'Arial Unicode MS'); |
| 8301 | |
| 8302 | # Ascii font for labels. |
| 8303 | my $courier = $workbook->add_format(font => 'Courier New'); |
| 8304 | |
| 8305 | |
| 8306 | my $char = 0; |
| 8307 | |
| 8308 | # Loop through all 32768 UTF-16BE characters. |
| 8309 | # |
| 8310 | for my $row (0 .. 2 ** 12 -1) { |
| 8311 | for my $col (0 .. 31) { |
| 8312 | |
| 8313 | last if $char == 0xffff; |
| 8314 | |
| 8315 | if ($col % 2 == 0){ |
| 8316 | $worksheet->write_string($row, $col, |
| 8317 | sprintf('0x%04X', $char), $courier); |
| 8318 | } |
| 8319 | else { |
| 8320 | $worksheet->write_utf16be_string($row, $col, |
| 8321 | pack('n', $char++), $uni_font); |
| 8322 | } |
| 8323 | } |
| 8324 | } |
| 8325 | |
| 8326 | |
| 8327 | |
| 8328 | __END__ |
| 8329 | |
| 8330 | |
| 8331 | |
| 8332 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_list.pl> |
| 8333 | |
| 8334 | =head2 Example: unicode_2022_jp.pl |
| 8335 | |
| 8336 | |
| 8337 | |
| 8338 | A simple example of converting some Unicode text to an Excel file using |
| 8339 | Spreadsheet::WriteExcel and perl 5.8. |
| 8340 | |
| 8341 | This example generates some Japanese from a file with ISO-2022-JP |
| 8342 | encoded text. |
| 8343 | |
| 8344 | |
| 8345 | |
| 8346 | =begin html |
| 8347 | |
| 8348 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_2022_jp.jpg" width="640" height="420" alt="Output from unicode_2022_jp.pl" /></center></p> |
| 8349 | |
| 8350 | =end html |
| 8351 | |
| 8352 | Source code for this example: |
| 8353 | |
| 8354 | #!/usr/bin/perl -w |
| 8355 | |
| 8356 | ############################################################################## |
| 8357 | # |
| 8358 | # A simple example of converting some Unicode text to an Excel file using |
| 8359 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8360 | # |
| 8361 | # This example generates some Japanese from a file with ISO-2022-JP |
| 8362 | # encoded text. |
| 8363 | # |
| 8364 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8365 | # |
| 8366 | |
| 8367 | |
| 8368 | |
| 8369 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8370 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8371 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8372 | # |
| 8373 | require 5.008; |
| 8374 | |
| 8375 | use strict; |
| 8376 | use Spreadsheet::WriteExcel; |
| 8377 | |
| 8378 | |
| 8379 | my $workbook = Spreadsheet::WriteExcel->new("unicode_2022_jp.xls"); |
| 8380 | my $worksheet = $workbook->add_worksheet(); |
| 8381 | $worksheet->set_column('A:A', 50); |
| 8382 | |
| 8383 | |
| 8384 | my $file = 'unicode_2022_jp.txt'; |
| 8385 | |
| 8386 | open FH, '<:encoding(iso-2022-jp)', $file or die "Couldn't open $file: $!\n"; |
| 8387 | |
| 8388 | my $row = 0; |
| 8389 | |
| 8390 | while (<FH>) { |
| 8391 | next if /^#/; # Ignore the comments in the sample file. |
| 8392 | chomp; |
| 8393 | $worksheet->write($row++, 0, $_); |
| 8394 | } |
| 8395 | |
| 8396 | |
| 8397 | __END__ |
| 8398 | |
| 8399 | |
| 8400 | |
| 8401 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_2022_jp.pl> |
| 8402 | |
| 8403 | =head2 Example: unicode_8859_11.pl |
| 8404 | |
| 8405 | |
| 8406 | |
| 8407 | A simple example of converting some Unicode text to an Excel file using |
| 8408 | Spreadsheet::WriteExcel and perl 5.8. |
| 8409 | |
| 8410 | This example generates some Thai from a file with ISO-8859-11 encoded text. |
| 8411 | |
| 8412 | |
| 8413 | |
| 8414 | |
| 8415 | =begin html |
| 8416 | |
| 8417 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_8859_11.jpg" width="640" height="420" alt="Output from unicode_8859_11.pl" /></center></p> |
| 8418 | |
| 8419 | =end html |
| 8420 | |
| 8421 | Source code for this example: |
| 8422 | |
| 8423 | #!/usr/bin/perl -w |
| 8424 | |
| 8425 | ############################################################################## |
| 8426 | # |
| 8427 | # A simple example of converting some Unicode text to an Excel file using |
| 8428 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8429 | # |
| 8430 | # This example generates some Thai from a file with ISO-8859-11 encoded text. |
| 8431 | # |
| 8432 | # |
| 8433 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8434 | # |
| 8435 | |
| 8436 | |
| 8437 | |
| 8438 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8439 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8440 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8441 | # |
| 8442 | require 5.008; |
| 8443 | |
| 8444 | use strict; |
| 8445 | use Spreadsheet::WriteExcel; |
| 8446 | |
| 8447 | |
| 8448 | my $workbook = Spreadsheet::WriteExcel->new("unicode_8859_11.xls"); |
| 8449 | my $worksheet = $workbook->add_worksheet(); |
| 8450 | $worksheet->set_column('A:A', 50); |
| 8451 | |
| 8452 | |
| 8453 | my $file = 'unicode_8859_11.txt'; |
| 8454 | |
| 8455 | open FH, '<:encoding(iso-8859-11)', $file or die "Couldn't open $file: $!\n"; |
| 8456 | |
| 8457 | my $row = 0; |
| 8458 | |
| 8459 | while (<FH>) { |
| 8460 | next if /^#/; # Ignore the comments in the sample file. |
| 8461 | chomp; |
| 8462 | $worksheet->write($row++, 0, $_); |
| 8463 | } |
| 8464 | |
| 8465 | |
| 8466 | __END__ |
| 8467 | |
| 8468 | |
| 8469 | |
| 8470 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_8859_11.pl> |
| 8471 | |
| 8472 | =head2 Example: unicode_8859_7.pl |
| 8473 | |
| 8474 | |
| 8475 | |
| 8476 | A simple example of converting some Unicode text to an Excel file using |
| 8477 | Spreadsheet::WriteExcel and perl 5.8. |
| 8478 | |
| 8479 | This example generates some Greek from a file with ISO-8859-7 encoded text. |
| 8480 | |
| 8481 | |
| 8482 | |
| 8483 | |
| 8484 | =begin html |
| 8485 | |
| 8486 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_8859_7.jpg" width="640" height="420" alt="Output from unicode_8859_7.pl" /></center></p> |
| 8487 | |
| 8488 | =end html |
| 8489 | |
| 8490 | Source code for this example: |
| 8491 | |
| 8492 | #!/usr/bin/perl -w |
| 8493 | |
| 8494 | ############################################################################## |
| 8495 | # |
| 8496 | # A simple example of converting some Unicode text to an Excel file using |
| 8497 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8498 | # |
| 8499 | # This example generates some Greek from a file with ISO-8859-7 encoded text. |
| 8500 | # |
| 8501 | # |
| 8502 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8503 | # |
| 8504 | |
| 8505 | |
| 8506 | |
| 8507 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8508 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8509 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8510 | # |
| 8511 | require 5.008; |
| 8512 | |
| 8513 | use strict; |
| 8514 | use Spreadsheet::WriteExcel; |
| 8515 | |
| 8516 | |
| 8517 | my $workbook = Spreadsheet::WriteExcel->new("unicode_8859_7.xls"); |
| 8518 | my $worksheet = $workbook->add_worksheet(); |
| 8519 | $worksheet->set_column('A:A', 50); |
| 8520 | |
| 8521 | |
| 8522 | my $file = 'unicode_8859_7.txt'; |
| 8523 | |
| 8524 | open FH, '<:encoding(iso-8859-7)', $file or die "Couldn't open $file: $!\n"; |
| 8525 | |
| 8526 | my $row = 0; |
| 8527 | |
| 8528 | while (<FH>) { |
| 8529 | next if /^#/; # Ignore the comments in the sample file. |
| 8530 | chomp; |
| 8531 | $worksheet->write($row++, 0, $_); |
| 8532 | } |
| 8533 | |
| 8534 | |
| 8535 | __END__ |
| 8536 | |
| 8537 | |
| 8538 | |
| 8539 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_8859_7.pl> |
| 8540 | |
| 8541 | =head2 Example: unicode_big5.pl |
| 8542 | |
| 8543 | |
| 8544 | |
| 8545 | A simple example of converting some Unicode text to an Excel file using |
| 8546 | Spreadsheet::WriteExcel and perl 5.8. |
| 8547 | |
| 8548 | This example generates some Chinese from a file with BIG5 encoded text. |
| 8549 | |
| 8550 | |
| 8551 | |
| 8552 | |
| 8553 | =begin html |
| 8554 | |
| 8555 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_big5.jpg" width="640" height="420" alt="Output from unicode_big5.pl" /></center></p> |
| 8556 | |
| 8557 | =end html |
| 8558 | |
| 8559 | Source code for this example: |
| 8560 | |
| 8561 | #!/usr/bin/perl -w |
| 8562 | |
| 8563 | ############################################################################## |
| 8564 | # |
| 8565 | # A simple example of converting some Unicode text to an Excel file using |
| 8566 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8567 | # |
| 8568 | # This example generates some Chinese from a file with BIG5 encoded text. |
| 8569 | # |
| 8570 | # |
| 8571 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8572 | # |
| 8573 | |
| 8574 | |
| 8575 | |
| 8576 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8577 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8578 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8579 | # |
| 8580 | require 5.008; |
| 8581 | |
| 8582 | use strict; |
| 8583 | use Spreadsheet::WriteExcel; |
| 8584 | |
| 8585 | |
| 8586 | my $workbook = Spreadsheet::WriteExcel->new("unicode_big5.xls"); |
| 8587 | my $worksheet = $workbook->add_worksheet(); |
| 8588 | $worksheet->set_column('A:A', 80); |
| 8589 | |
| 8590 | |
| 8591 | my $file = 'unicode_big5.txt'; |
| 8592 | |
| 8593 | open FH, '<:encoding(big5)', $file or die "Couldn't open $file: $!\n"; |
| 8594 | |
| 8595 | my $row = 0; |
| 8596 | |
| 8597 | while (<FH>) { |
| 8598 | next if /^#/; # Ignore the comments in the sample file. |
| 8599 | chomp; |
| 8600 | $worksheet->write($row++, 0, $_); |
| 8601 | } |
| 8602 | |
| 8603 | |
| 8604 | __END__ |
| 8605 | |
| 8606 | |
| 8607 | |
| 8608 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_big5.pl> |
| 8609 | |
| 8610 | =head2 Example: unicode_cp1251.pl |
| 8611 | |
| 8612 | |
| 8613 | |
| 8614 | A simple example of converting some Unicode text to an Excel file using |
| 8615 | Spreadsheet::WriteExcel and perl 5.8. |
| 8616 | |
| 8617 | This example generates some Russian from a file with CP1251 encoded text. |
| 8618 | |
| 8619 | |
| 8620 | |
| 8621 | |
| 8622 | =begin html |
| 8623 | |
| 8624 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cp1251.jpg" width="640" height="420" alt="Output from unicode_cp1251.pl" /></center></p> |
| 8625 | |
| 8626 | =end html |
| 8627 | |
| 8628 | Source code for this example: |
| 8629 | |
| 8630 | #!/usr/bin/perl -w |
| 8631 | |
| 8632 | ############################################################################## |
| 8633 | # |
| 8634 | # A simple example of converting some Unicode text to an Excel file using |
| 8635 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8636 | # |
| 8637 | # This example generates some Russian from a file with CP1251 encoded text. |
| 8638 | # |
| 8639 | # |
| 8640 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8641 | # |
| 8642 | |
| 8643 | |
| 8644 | |
| 8645 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8646 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8647 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8648 | # |
| 8649 | require 5.008; |
| 8650 | |
| 8651 | use strict; |
| 8652 | use Spreadsheet::WriteExcel; |
| 8653 | |
| 8654 | |
| 8655 | my $workbook = Spreadsheet::WriteExcel->new("unicode_cp1251.xls"); |
| 8656 | my $worksheet = $workbook->add_worksheet(); |
| 8657 | $worksheet->set_column('A:A', 50); |
| 8658 | |
| 8659 | |
| 8660 | my $file = 'unicode_cp1251.txt'; |
| 8661 | |
| 8662 | open FH, '<:encoding(cp1251)', $file or die "Couldn't open $file: $!\n"; |
| 8663 | |
| 8664 | my $row = 0; |
| 8665 | |
| 8666 | while (<FH>) { |
| 8667 | next if /^#/; # Ignore the comments in the sample file. |
| 8668 | chomp; |
| 8669 | $worksheet->write($row++, 0, $_); |
| 8670 | } |
| 8671 | |
| 8672 | |
| 8673 | __END__ |
| 8674 | |
| 8675 | |
| 8676 | |
| 8677 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cp1251.pl> |
| 8678 | |
| 8679 | =head2 Example: unicode_cp1256.pl |
| 8680 | |
| 8681 | |
| 8682 | |
| 8683 | A simple example of converting some Unicode text to an Excel file using |
| 8684 | Spreadsheet::WriteExcel and perl 5.8. |
| 8685 | |
| 8686 | This example generates some Arabic text from a CP-1256 encoded file. |
| 8687 | |
| 8688 | |
| 8689 | |
| 8690 | |
| 8691 | =begin html |
| 8692 | |
| 8693 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cp1256.jpg" width="640" height="420" alt="Output from unicode_cp1256.pl" /></center></p> |
| 8694 | |
| 8695 | =end html |
| 8696 | |
| 8697 | Source code for this example: |
| 8698 | |
| 8699 | #!/usr/bin/perl -w |
| 8700 | |
| 8701 | ############################################################################## |
| 8702 | # |
| 8703 | # A simple example of converting some Unicode text to an Excel file using |
| 8704 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8705 | # |
| 8706 | # This example generates some Arabic text from a CP-1256 encoded file. |
| 8707 | # |
| 8708 | # |
| 8709 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8710 | # |
| 8711 | |
| 8712 | |
| 8713 | |
| 8714 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8715 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8716 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8717 | # |
| 8718 | require 5.008; |
| 8719 | |
| 8720 | use strict; |
| 8721 | use Spreadsheet::WriteExcel; |
| 8722 | |
| 8723 | |
| 8724 | my $workbook = Spreadsheet::WriteExcel->new("unicode_cp1256.xls"); |
| 8725 | my $worksheet = $workbook->add_worksheet(); |
| 8726 | $worksheet->set_column('A:A', 50); |
| 8727 | |
| 8728 | |
| 8729 | my $file = 'unicode_cp1256.txt'; |
| 8730 | |
| 8731 | open FH, '<:encoding(cp1256)', $file or die "Couldn't open $file: $!\n"; |
| 8732 | |
| 8733 | my $row = 0; |
| 8734 | |
| 8735 | while (<FH>) { |
| 8736 | next if /^#/; # Ignore the comments in the sample file. |
| 8737 | chomp; |
| 8738 | $worksheet->write($row++, 0, $_); |
| 8739 | } |
| 8740 | |
| 8741 | |
| 8742 | __END__ |
| 8743 | |
| 8744 | |
| 8745 | |
| 8746 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cp1256.pl> |
| 8747 | |
| 8748 | =head2 Example: unicode_koi8r.pl |
| 8749 | |
| 8750 | |
| 8751 | |
| 8752 | A simple example of converting some Unicode text to an Excel file using |
| 8753 | Spreadsheet::WriteExcel and perl 5.8. |
| 8754 | |
| 8755 | This example generates some Russian from a file with KOI8-R encoded text. |
| 8756 | |
| 8757 | |
| 8758 | |
| 8759 | |
| 8760 | =begin html |
| 8761 | |
| 8762 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_koi8r.jpg" width="640" height="420" alt="Output from unicode_koi8r.pl" /></center></p> |
| 8763 | |
| 8764 | =end html |
| 8765 | |
| 8766 | Source code for this example: |
| 8767 | |
| 8768 | #!/usr/bin/perl -w |
| 8769 | |
| 8770 | ############################################################################## |
| 8771 | # |
| 8772 | # A simple example of converting some Unicode text to an Excel file using |
| 8773 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8774 | # |
| 8775 | # This example generates some Russian from a file with KOI8-R encoded text. |
| 8776 | # |
| 8777 | # |
| 8778 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8779 | # |
| 8780 | |
| 8781 | |
| 8782 | |
| 8783 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8784 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8785 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8786 | # |
| 8787 | require 5.008; |
| 8788 | |
| 8789 | use strict; |
| 8790 | use Spreadsheet::WriteExcel; |
| 8791 | |
| 8792 | |
| 8793 | my $workbook = Spreadsheet::WriteExcel->new("unicode_koi8r.xls"); |
| 8794 | my $worksheet = $workbook->add_worksheet(); |
| 8795 | $worksheet->set_column('A:A', 50); |
| 8796 | |
| 8797 | |
| 8798 | my $file = 'unicode_koi8r.txt'; |
| 8799 | |
| 8800 | open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n"; |
| 8801 | |
| 8802 | my $row = 0; |
| 8803 | |
| 8804 | while (<FH>) { |
| 8805 | next if /^#/; # Ignore the comments in the sample file. |
| 8806 | chomp; |
| 8807 | $worksheet->write($row++, 0, $_); |
| 8808 | } |
| 8809 | |
| 8810 | |
| 8811 | __END__ |
| 8812 | |
| 8813 | |
| 8814 | |
| 8815 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_koi8r.pl> |
| 8816 | |
| 8817 | =head2 Example: unicode_polish_utf8.pl |
| 8818 | |
| 8819 | |
| 8820 | |
| 8821 | A simple example of converting some Unicode text to an Excel file using |
| 8822 | Spreadsheet::WriteExcel and perl 5.8. |
| 8823 | |
| 8824 | This example generates some Polish from a file with UTF8 encoded text. |
| 8825 | |
| 8826 | |
| 8827 | |
| 8828 | |
| 8829 | =begin html |
| 8830 | |
| 8831 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_polish_utf8.jpg" width="640" height="420" alt="Output from unicode_polish_utf8.pl" /></center></p> |
| 8832 | |
| 8833 | =end html |
| 8834 | |
| 8835 | Source code for this example: |
| 8836 | |
| 8837 | #!/usr/bin/perl -w |
| 8838 | |
| 8839 | ############################################################################## |
| 8840 | # |
| 8841 | # A simple example of converting some Unicode text to an Excel file using |
| 8842 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8843 | # |
| 8844 | # This example generates some Polish from a file with UTF8 encoded text. |
| 8845 | # |
| 8846 | # |
| 8847 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8848 | # |
| 8849 | |
| 8850 | |
| 8851 | |
| 8852 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8853 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8854 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8855 | # |
| 8856 | require 5.008; |
| 8857 | |
| 8858 | use strict; |
| 8859 | use Spreadsheet::WriteExcel; |
| 8860 | |
| 8861 | |
| 8862 | my $workbook = Spreadsheet::WriteExcel->new("unicode_polish_utf8.xls"); |
| 8863 | my $worksheet = $workbook->add_worksheet(); |
| 8864 | $worksheet->set_column('A:A', 50); |
| 8865 | |
| 8866 | |
| 8867 | my $file = 'unicode_polish_utf8.txt'; |
| 8868 | |
| 8869 | open FH, '<:encoding(utf8)', $file or die "Couldn't open $file: $!\n"; |
| 8870 | |
| 8871 | my $row = 0; |
| 8872 | |
| 8873 | while (<FH>) { |
| 8874 | next if /^#/; # Ignore the comments in the sample file. |
| 8875 | chomp; |
| 8876 | $worksheet->write($row++, 0, $_); |
| 8877 | } |
| 8878 | |
| 8879 | |
| 8880 | __END__ |
| 8881 | |
| 8882 | |
| 8883 | |
| 8884 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_polish_utf8.pl> |
| 8885 | |
| 8886 | =head2 Example: unicode_shift_jis.pl |
| 8887 | |
| 8888 | |
| 8889 | |
| 8890 | A simple example of converting some Unicode text to an Excel file using |
| 8891 | Spreadsheet::WriteExcel and perl 5.8. |
| 8892 | |
| 8893 | This example generates some Japenese text from a file with Shift-JIS |
| 8894 | encoded text. |
| 8895 | |
| 8896 | |
| 8897 | |
| 8898 | =begin html |
| 8899 | |
| 8900 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_shift_jis.jpg" width="640" height="420" alt="Output from unicode_shift_jis.pl" /></center></p> |
| 8901 | |
| 8902 | =end html |
| 8903 | |
| 8904 | Source code for this example: |
| 8905 | |
| 8906 | #!/usr/bin/perl -w |
| 8907 | |
| 8908 | ############################################################################## |
| 8909 | # |
| 8910 | # A simple example of converting some Unicode text to an Excel file using |
| 8911 | # Spreadsheet::WriteExcel and perl 5.8. |
| 8912 | # |
| 8913 | # This example generates some Japenese text from a file with Shift-JIS |
| 8914 | # encoded text. |
| 8915 | # |
| 8916 | # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org |
| 8917 | # |
| 8918 | |
| 8919 | |
| 8920 | |
| 8921 | # Perl 5.8 or later is required for proper utf8 handling. For older perl |
| 8922 | # versions you should use UTF16 and the write_utf16be_string() method. |
| 8923 | # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. |
| 8924 | # |
| 8925 | require 5.008; |
| 8926 | |
| 8927 | use strict; |
| 8928 | use Spreadsheet::WriteExcel; |
| 8929 | |
| 8930 | |
| 8931 | my $workbook = Spreadsheet::WriteExcel->new("unicode_shift_jis.xls"); |
| 8932 | my $worksheet = $workbook->add_worksheet(); |
| 8933 | $worksheet->set_column('A:A', 50); |
| 8934 | |
| 8935 | |
| 8936 | my $file = 'unicode_shift_jis.txt'; |
| 8937 | |
| 8938 | open FH, '<:encoding(shiftjis)', $file or die "Couldn't open $file: $!\n"; |
| 8939 | |
| 8940 | my $row = 0; |
| 8941 | |
| 8942 | while (<FH>) { |
| 8943 | next if /^#/; # Ignore the comments in the sample file. |
| 8944 | chomp; |
| 8945 | $worksheet->write($row++, 0, $_); |
| 8946 | } |
| 8947 | |
| 8948 | |
| 8949 | __END__ |
| 8950 | |
| 8951 | |
| 8952 | |
| 8953 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_shift_jis.pl> |
| 8954 | |
| 8955 | =head2 Example: csv2xls.pl |
| 8956 | |
| 8957 | |
| 8958 | |
| 8959 | Example of how to use the WriteExcel module |
| 8960 | |
| 8961 | Simple program to convert a CSV comma-separated value file to an Excel file. |
| 8962 | This is more or less an non-op since Excel can read CSV files. |
| 8963 | The program uses Text::CSV_XS to parse the CSV. |
| 8964 | |
| 8965 | Usage: csv2xls.pl file.csv newfile.xls |
| 8966 | |
| 8967 | |
| 8968 | NOTE: This is only a simple conversion utility for illustrative purposes. |
| 8969 | For converting a CSV or Tab separated or any other type of delimited |
| 8970 | text file to Excel I recommend the more rigorous csv2xls program that is |
| 8971 | part of H.Merijn Brand's Text::CSV_XS module distro. |
| 8972 | |
| 8973 | See the examples/csv2xls link here: |
| 8974 | L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST> |
| 8975 | |
| 8976 | |
| 8977 | |
| 8978 | #!/usr/bin/perl -w |
| 8979 | |
| 8980 | ############################################################################### |
| 8981 | # |
| 8982 | # Example of how to use the WriteExcel module |
| 8983 | # |
| 8984 | # Simple program to convert a CSV comma-separated value file to an Excel file. |
| 8985 | # This is more or less an non-op since Excel can read CSV files. |
| 8986 | # The program uses Text::CSV_XS to parse the CSV. |
| 8987 | # |
| 8988 | # Usage: csv2xls.pl file.csv newfile.xls |
| 8989 | # |
| 8990 | # |
| 8991 | # NOTE: This is only a simple conversion utility for illustrative purposes. |
| 8992 | # For converting a CSV or Tab separated or any other type of delimited |
| 8993 | # text file to Excel I recommend the more rigorous csv2xls program that is |
| 8994 | # part of H.Merijn Brand's Text::CSV_XS module distro. |
| 8995 | # |
| 8996 | # See the examples/csv2xls link here: |
| 8997 | # L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST> |
| 8998 | # |
| 8999 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 9000 | # |
| 9001 | |
| 9002 | use strict; |
| 9003 | use Spreadsheet::WriteExcel; |
| 9004 | use Text::CSV_XS; |
| 9005 | |
| 9006 | # Check for valid number of arguments |
| 9007 | if (($#ARGV < 1) || ($#ARGV > 2)) { |
| 9008 | die("Usage: csv2xls csvfile.txt newfile.xls\n"); |
| 9009 | }; |
| 9010 | |
| 9011 | # Open the Comma Separated Variable file |
| 9012 | open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!"; |
| 9013 | |
| 9014 | # Create a new Excel workbook |
| 9015 | my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]); |
| 9016 | my $worksheet = $workbook->add_worksheet(); |
| 9017 | |
| 9018 | # Create a new CSV parsing object |
| 9019 | my $csv = Text::CSV_XS->new; |
| 9020 | |
| 9021 | # Row and column are zero indexed |
| 9022 | my $row = 0; |
| 9023 | |
| 9024 | |
| 9025 | while (<CSVFILE>) { |
| 9026 | if ($csv->parse($_)) { |
| 9027 | my @Fld = $csv->fields; |
| 9028 | |
| 9029 | my $col = 0; |
| 9030 | foreach my $token (@Fld) { |
| 9031 | $worksheet->write($row, $col, $token); |
| 9032 | $col++; |
| 9033 | } |
| 9034 | $row++; |
| 9035 | } |
| 9036 | else { |
| 9037 | my $err = $csv->error_input; |
| 9038 | print "Text::CSV_XS parse() failed on argument: ", $err, "\n"; |
| 9039 | } |
| 9040 | } |
| 9041 | |
| 9042 | |
| 9043 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/csv2xls.pl> |
| 9044 | |
| 9045 | =head2 Example: tab2xls.pl |
| 9046 | |
| 9047 | |
| 9048 | |
| 9049 | Example of how to use the WriteExcel module |
| 9050 | |
| 9051 | The following converts a tab separated file into an Excel file |
| 9052 | |
| 9053 | Usage: tab2xls.pl tabfile.txt newfile.xls |
| 9054 | |
| 9055 | |
| 9056 | NOTE: This is only a simple conversion utility for illustrative purposes. |
| 9057 | For converting a CSV or Tab separated or any other type of delimited |
| 9058 | text file to Excel I recommend the more rigorous csv2xls program that is |
| 9059 | part of H.Merijn Brand's Text::CSV_XS module distro. |
| 9060 | |
| 9061 | See the examples/csv2xls link here: |
| 9062 | L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST> |
| 9063 | |
| 9064 | |
| 9065 | |
| 9066 | #!/usr/bin/perl -w |
| 9067 | |
| 9068 | ############################################################################### |
| 9069 | # |
| 9070 | # Example of how to use the WriteExcel module |
| 9071 | # |
| 9072 | # The following converts a tab separated file into an Excel file |
| 9073 | # |
| 9074 | # Usage: tab2xls.pl tabfile.txt newfile.xls |
| 9075 | # |
| 9076 | # |
| 9077 | # NOTE: This is only a simple conversion utility for illustrative purposes. |
| 9078 | # For converting a CSV or Tab separated or any other type of delimited |
| 9079 | # text file to Excel I recommend the more rigorous csv2xls program that is |
| 9080 | # part of H.Merijn Brand's Text::CSV_XS module distro. |
| 9081 | # |
| 9082 | # See the examples/csv2xls link here: |
| 9083 | # L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST> |
| 9084 | # |
| 9085 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 9086 | # |
| 9087 | |
| 9088 | |
| 9089 | use strict; |
| 9090 | use Spreadsheet::WriteExcel; |
| 9091 | |
| 9092 | |
| 9093 | # Check for valid number of arguments |
| 9094 | if (($#ARGV < 1) || ($#ARGV > 2)) { |
| 9095 | die("Usage: tab2xls tabfile.txt newfile.xls\n"); |
| 9096 | }; |
| 9097 | |
| 9098 | |
| 9099 | # Open the tab delimited file |
| 9100 | open (TABFILE, $ARGV[0]) or die "$ARGV[0]: $!"; |
| 9101 | |
| 9102 | |
| 9103 | # Create a new Excel workbook |
| 9104 | my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]); |
| 9105 | my $worksheet = $workbook->add_worksheet(); |
| 9106 | |
| 9107 | # Row and column are zero indexed |
| 9108 | my $row = 0; |
| 9109 | |
| 9110 | while (<TABFILE>) { |
| 9111 | chomp; |
| 9112 | # Split on single tab |
| 9113 | my @Fld = split('\t', $_); |
| 9114 | |
| 9115 | my $col = 0; |
| 9116 | foreach my $token (@Fld) { |
| 9117 | $worksheet->write($row, $col, $token); |
| 9118 | $col++; |
| 9119 | } |
| 9120 | $row++; |
| 9121 | } |
| 9122 | |
| 9123 | |
| 9124 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/tab2xls.pl> |
| 9125 | |
| 9126 | =head2 Example: datecalc1.pl |
| 9127 | |
| 9128 | |
| 9129 | |
| 9130 | NOTE: An easier way of writing dates and times is to use the newer |
| 9131 | write_date_time() Worksheet method. See the date_time.pl example. |
| 9132 | |
| 9133 | |
| 9134 | |
| 9135 | Demonstration of writing date/time cells to Excel spreadsheets, |
| 9136 | using UNIX/Perl time as source of date/time. |
| 9137 | |
| 9138 | |
| 9139 | |
| 9140 | UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970) |
| 9141 | measured in seconds. |
| 9142 | |
| 9143 | An Excel file can use exactly one of two different date/time systems. |
| 9144 | In these systems, a floating point number represents the number of days |
| 9145 | (and fractional parts of the day) since a start point. The floating point |
| 9146 | number is referred to as a 'serial'. |
| 9147 | |
| 9148 | The two systems ('1900' and '1904') use different starting points: |
| 9149 | |
| 9150 | '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as |
| 9151 | a leap year - see: |
| 9152 | http://support.microsoft.com/support/kb/articles/Q181/3/70.asp |
| 9153 | for the excuse^H^H^H^H^H^Hreason. |
| 9154 | '1904'; '1.00' is 2 Jan 1904. |
| 9155 | |
| 9156 | The '1904' system is the default for Apple Macs. Windows versions of |
| 9157 | Excel have the option to use the '1904' system. |
| 9158 | |
| 9159 | Note that Visual Basic's "DateSerial" function does NOT erroneously |
| 9160 | regard 1900 as a leap year, and thus its serials do not agree with |
| 9161 | the 1900 serials of Excel for dates before 1 Mar 1900. |
| 9162 | |
| 9163 | Note that StarOffice (at least at version 5.2) does NOT erroneously |
| 9164 | regard 1900 as a leap year, and thus its serials do not agree with |
| 9165 | the 1900 serials of Excel for dates before 1 Mar 1900. |
| 9166 | |
| 9167 | |
| 9168 | #!/usr/bin/perl -w |
| 9169 | |
| 9170 | |
| 9171 | ###################################################################### |
| 9172 | # |
| 9173 | # NOTE: An easier way of writing dates and times is to use the newer |
| 9174 | # write_date_time() Worksheet method. See the date_time.pl example. |
| 9175 | # |
| 9176 | ###################################################################### |
| 9177 | # |
| 9178 | # Demonstration of writing date/time cells to Excel spreadsheets, |
| 9179 | # using UNIX/Perl time as source of date/time. |
| 9180 | # |
| 9181 | ###################################################################### |
| 9182 | # |
| 9183 | # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970) |
| 9184 | # measured in seconds. |
| 9185 | # |
| 9186 | # An Excel file can use exactly one of two different date/time systems. |
| 9187 | # In these systems, a floating point number represents the number of days |
| 9188 | # (and fractional parts of the day) since a start point. The floating point |
| 9189 | # number is referred to as a 'serial'. |
| 9190 | # |
| 9191 | # The two systems ('1900' and '1904') use different starting points: |
| 9192 | # |
| 9193 | # '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as |
| 9194 | # a leap year - see: |
| 9195 | # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp |
| 9196 | # for the excuse^H^H^H^H^H^Hreason. |
| 9197 | # '1904'; '1.00' is 2 Jan 1904. |
| 9198 | # |
| 9199 | # The '1904' system is the default for Apple Macs. Windows versions of |
| 9200 | # Excel have the option to use the '1904' system. |
| 9201 | # |
| 9202 | # Note that Visual Basic's "DateSerial" function does NOT erroneously |
| 9203 | # regard 1900 as a leap year, and thus its serials do not agree with |
| 9204 | # the 1900 serials of Excel for dates before 1 Mar 1900. |
| 9205 | # |
| 9206 | # Note that StarOffice (at least at version 5.2) does NOT erroneously |
| 9207 | # regard 1900 as a leap year, and thus its serials do not agree with |
| 9208 | # the 1900 serials of Excel for dates before 1 Mar 1900. |
| 9209 | # |
| 9210 | |
| 9211 | # Copyright 2000, Andrew Benham, adsb@bigfoot.com |
| 9212 | # |
| 9213 | |
| 9214 | ###################################################################### |
| 9215 | # |
| 9216 | # Calculation description |
| 9217 | # ======================= |
| 9218 | # |
| 9219 | # 1900 system |
| 9220 | # ----------- |
| 9221 | # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900. |
| 9222 | # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68) |
| 9223 | # were leap years with an extra day. |
| 9224 | # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and |
| 9225 | # 1 Jan 1970. |
| 9226 | # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year |
| 9227 | # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'. |
| 9228 | # |
| 9229 | # 1904 system |
| 9230 | # ----------- |
| 9231 | # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904. |
| 9232 | # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68) |
| 9233 | # were leap years with an extra day. |
| 9234 | # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and |
| 9235 | # 1 Jan 1970. |
| 9236 | # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'. |
| 9237 | # |
| 9238 | ###################################################################### |
| 9239 | # |
| 9240 | # Copyright (c) 2000, Andrew Benham. |
| 9241 | # This program is free software. It may be used, redistributed and/or |
| 9242 | # modified under the same terms as Perl itself. |
| 9243 | # |
| 9244 | # Andrew Benham, adsb@bigfoot.com |
| 9245 | # London, United Kingdom |
| 9246 | # 11 Nov 2000 |
| 9247 | # |
| 9248 | ###################################################################### |
| 9249 | |
| 9250 | |
| 9251 | use strict; |
| 9252 | use Spreadsheet::WriteExcel; |
| 9253 | |
| 9254 | use Time::Local; |
| 9255 | |
| 9256 | use vars qw/$DATE_SYSTEM/; |
| 9257 | |
| 9258 | # Use 1900 date system on all platforms other than Apple Mac (for which |
| 9259 | # use 1904 date system). |
| 9260 | $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0; |
| 9261 | |
| 9262 | my $workbook = Spreadsheet::WriteExcel->new("dates.xls"); |
| 9263 | my $worksheet = $workbook->add_worksheet(); |
| 9264 | |
| 9265 | my $format_date = $workbook->add_format(); |
| 9266 | $format_date->set_num_format('d mmmm yyy'); |
| 9267 | |
| 9268 | $worksheet->set_column(0,1,21); |
| 9269 | |
| 9270 | $worksheet->write_string (0,0,"The epoch (GMT)"); |
| 9271 | $worksheet->write_number (0,1,&calc_serial(0,1),0x16); |
| 9272 | |
| 9273 | $worksheet->write_string (1,0,"The epoch (localtime)"); |
| 9274 | $worksheet->write_number (1,1,&calc_serial(0,0),0x16); |
| 9275 | |
| 9276 | $worksheet->write_string (2,0,"Today"); |
| 9277 | $worksheet->write_number (2,1,&calc_serial(),$format_date); |
| 9278 | |
| 9279 | my $christmas2000 = timelocal(0,0,0,25,11,100); |
| 9280 | $worksheet->write_string (3,0,"Christmas 2000"); |
| 9281 | $worksheet->write_number (3,1,&calc_serial($christmas2000),$format_date); |
| 9282 | |
| 9283 | $workbook->close(); |
| 9284 | |
| 9285 | #----------------------------------------------------------- |
| 9286 | # calc_serial() |
| 9287 | # |
| 9288 | # Called with (up to) 2 parameters. |
| 9289 | # 1. Unix timestamp. If omitted, uses current time. |
| 9290 | # 2. GMT flag. Set to '1' to return serial in GMT. |
| 9291 | # If omitted, returns serial in appropriate timezone. |
| 9292 | # |
| 9293 | # Returns date/time serial according to $DATE_SYSTEM selected |
| 9294 | #----------------------------------------------------------- |
| 9295 | sub calc_serial { |
| 9296 | my $time = (defined $_[0]) ? $_[0] : time(); |
| 9297 | my $gmtflag = (defined $_[1]) ? $_[1] : 0; |
| 9298 | |
| 9299 | # Divide timestamp by number of seconds in a day. |
| 9300 | # This gives a date serial with '0' on 1 Jan 1970. |
| 9301 | my $serial = $time / 86400; |
| 9302 | |
| 9303 | # Adjust the date serial by the offset appropriate to the |
| 9304 | # currently selected system (1900/1904). |
| 9305 | if ($DATE_SYSTEM == 0) { # use 1900 system |
| 9306 | $serial += 25569; |
| 9307 | } else { # use 1904 system |
| 9308 | $serial += 24107; |
| 9309 | } |
| 9310 | |
| 9311 | unless ($gmtflag) { |
| 9312 | # Now have a 'raw' serial with the right offset. But this |
| 9313 | # gives a serial in GMT, which is false unless the timezone |
| 9314 | # is GMT. We need to adjust the serial by the appropriate |
| 9315 | # timezone offset. |
| 9316 | # Calculate the appropriate timezone offset by seeing what |
| 9317 | # the differences between localtime and gmtime for the given |
| 9318 | # time are. |
| 9319 | |
| 9320 | my @gmtime = gmtime($time); |
| 9321 | my @ltime = localtime($time); |
| 9322 | |
| 9323 | # For the first 7 elements of the two arrays, adjust the |
| 9324 | # date serial where the elements differ. |
| 9325 | for (0 .. 6) { |
| 9326 | my $diff = $ltime[$_] - $gmtime[$_]; |
| 9327 | if ($diff) { |
| 9328 | $serial += _adjustment($diff,$_); |
| 9329 | } |
| 9330 | } |
| 9331 | } |
| 9332 | |
| 9333 | # Perpetuate the error that 1900 was a leap year by decrementing |
| 9334 | # the serial if we're using the 1900 system and the date is prior to |
| 9335 | # 1 Mar 1900. This has the effect of making serial value '60' |
| 9336 | # 29 Feb 1900. |
| 9337 | |
| 9338 | # This fix only has any effect if UNIX/Perl time on the platform |
| 9339 | # can represent 1900. Many can't. |
| 9340 | |
| 9341 | unless ($DATE_SYSTEM) { |
| 9342 | $serial-- if ($serial < 61); # '61' is 1 Mar 1900 |
| 9343 | } |
| 9344 | return $serial; |
| 9345 | } |
| 9346 | |
| 9347 | sub _adjustment { |
| 9348 | # Based on the difference in the localtime/gmtime array elements |
| 9349 | # number, return the adjustment required to the serial. |
| 9350 | |
| 9351 | # We only look at some elements of the localtime/gmtime arrays: |
| 9352 | # seconds unlikely to be different as all known timezones |
| 9353 | # have an offset of integral multiples of 15 minutes, |
| 9354 | # but it's easy to do. |
| 9355 | # minutes will be different for timezone offsets which are |
| 9356 | # not an exact number of hours. |
| 9357 | # hours very likely to be different. |
| 9358 | # weekday will differ when localtime/gmtime difference |
| 9359 | # straddles midnight. |
| 9360 | # |
| 9361 | # Assume that difference between localtime and gmtime is less than |
| 9362 | # 5 days, then don't have to do maths for day of month, month number, |
| 9363 | # year number, etc... |
| 9364 | |
| 9365 | my ($delta,$element) = @_; |
| 9366 | my $adjust = 0; |
| 9367 | |
| 9368 | if ($element == 0) { # Seconds |
| 9369 | $adjust = $delta/86400; # 60 * 60 * 24 |
| 9370 | } elsif ($element == 1) { # Minutes |
| 9371 | $adjust = $delta/1440; # 60 * 24 |
| 9372 | } elsif ($element == 2) { # Hours |
| 9373 | $adjust = $delta/24; # 24 |
| 9374 | } elsif ($element == 6) { # Day of week number |
| 9375 | # Catch difference straddling Sat/Sun in either direction |
| 9376 | $delta += 7 if ($delta < -4); |
| 9377 | $delta -= 7 if ($delta > 4); |
| 9378 | |
| 9379 | $adjust = $delta; |
| 9380 | } |
| 9381 | return $adjust; |
| 9382 | } |
| 9383 | |
| 9384 | |
| 9385 | |
| 9386 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/datecalc1.pl> |
| 9387 | |
| 9388 | =head2 Example: datecalc2.pl |
| 9389 | |
| 9390 | |
| 9391 | |
| 9392 | Example of how to using the Date::Calc module to calculate Excel dates. |
| 9393 | |
| 9394 | NOTE: An easier way of writing dates and times is to use the newer |
| 9395 | write_date_time() Worksheet method. See the date_time.pl example. |
| 9396 | |
| 9397 | |
| 9398 | |
| 9399 | #!/usr/bin/perl -w |
| 9400 | |
| 9401 | ############################################################################### |
| 9402 | # |
| 9403 | # Example of how to using the Date::Calc module to calculate Excel dates. |
| 9404 | # |
| 9405 | # NOTE: An easier way of writing dates and times is to use the newer |
| 9406 | # write_date_time() Worksheet method. See the date_time.pl example. |
| 9407 | # |
| 9408 | # reverse('©'), June 2001, John McNamara, jmcnamara@cpan.org |
| 9409 | # |
| 9410 | |
| 9411 | use strict; |
| 9412 | use Spreadsheet::WriteExcel; |
| 9413 | use Date::Calc qw(Delta_DHMS); # You may need to install this module. |
| 9414 | |
| 9415 | |
| 9416 | # Create a new workbook and add a worksheet |
| 9417 | my $workbook = Spreadsheet::WriteExcel->new("excel_date2.xls"); |
| 9418 | my $worksheet = $workbook->add_worksheet(); |
| 9419 | |
| 9420 | # Expand the first column so that the date is visible. |
| 9421 | $worksheet->set_column("A:A", 25); |
| 9422 | |
| 9423 | |
| 9424 | # Add a format for the date |
| 9425 | my $format = $workbook->add_format(); |
| 9426 | $format->set_num_format('d mmmm yyy HH:MM:SS'); |
| 9427 | |
| 9428 | |
| 9429 | my $date; |
| 9430 | |
| 9431 | # Write some dates and times |
| 9432 | $date = excel_date(1900, 1, 1); |
| 9433 | $worksheet->write("A1", $date, $format); |
| 9434 | |
| 9435 | $date = excel_date(2000, 1, 1); |
| 9436 | $worksheet->write("A2", $date, $format); |
| 9437 | |
| 9438 | $date = excel_date(2000, 4, 17, 14, 33, 15); |
| 9439 | $worksheet->write("A3", $date, $format); |
| 9440 | |
| 9441 | |
| 9442 | ############################################################################### |
| 9443 | # |
| 9444 | # excel_date($years, $months, $days, $hours, $minutes, $seconds) |
| 9445 | # |
| 9446 | # Create an Excel date in the 1900 format. All of the arguments are optional |
| 9447 | # but you should at least add $years. |
| 9448 | # |
| 9449 | # Corrects for Excel's missing leap day in 1900. See excel_time1.pl for an |
| 9450 | # explanation. |
| 9451 | # |
| 9452 | sub excel_date { |
| 9453 | |
| 9454 | my $years = $_[0] || 1900; |
| 9455 | my $months = $_[1] || 1; |
| 9456 | my $days = $_[2] || 1; |
| 9457 | my $hours = $_[3] || 0; |
| 9458 | my $minutes = $_[4] || 0; |
| 9459 | my $seconds = $_[5] || 0; |
| 9460 | |
| 9461 | my @date = ($years, $months, $days, $hours, $minutes, $seconds); |
| 9462 | my @epoch = (1899, 12, 31, 0, 0, 0); |
| 9463 | |
| 9464 | ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date); |
| 9465 | |
| 9466 | my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60); |
| 9467 | |
| 9468 | # Add a day for Excel's missing leap day in 1900 |
| 9469 | $date++ if ($date > 59); |
| 9470 | |
| 9471 | return $date; |
| 9472 | } |
| 9473 | |
| 9474 | ############################################################################### |
| 9475 | # |
| 9476 | # excel_date($years, $months, $days, $hours, $minutes, $seconds) |
| 9477 | # |
| 9478 | # Create an Excel date in the 1904 format. All of the arguments are optional |
| 9479 | # but you should at least add $years. |
| 9480 | # |
| 9481 | # You will also need to call $workbook->set_1904() for this format to be valid. |
| 9482 | # |
| 9483 | sub excel_date_1904 { |
| 9484 | |
| 9485 | my $years = $_[0] || 1900; |
| 9486 | my $months = $_[1] || 1; |
| 9487 | my $days = $_[2] || 1; |
| 9488 | my $hours = $_[3] || 0; |
| 9489 | my $minutes = $_[4] || 0; |
| 9490 | my $seconds = $_[5] || 0; |
| 9491 | |
| 9492 | my @date = ($years, $months, $days, $hours, $minutes, $seconds); |
| 9493 | my @epoch = (1904, 1, 1, 0, 0, 0); |
| 9494 | |
| 9495 | ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date); |
| 9496 | |
| 9497 | my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60); |
| 9498 | |
| 9499 | return $date; |
| 9500 | } |
| 9501 | |
| 9502 | |
| 9503 | |
| 9504 | |
| 9505 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/datecalc2.pl> |
| 9506 | |
| 9507 | =head2 Example: lecxe.pl |
| 9508 | |
| 9509 | |
| 9510 | Utility program to convert an Excel file into a Spreadsheet::WriteExcel |
| 9511 | program using Win32::OLE |
| 9512 | |
| 9513 | |
| 9514 | #!/usr/bin/perl -w |
| 9515 | |
| 9516 | # |
| 9517 | # Utility program to convert an Excel file into a Spreadsheet::WriteExcel |
| 9518 | # program using Win32::OLE |
| 9519 | # |
| 9520 | |
| 9521 | # |
| 9522 | # lecxe program |
| 9523 | # by t0mas@netlords.net |
| 9524 | # |
| 9525 | # Version 0.01a Initial release (alpha) |
| 9526 | |
| 9527 | |
| 9528 | # Modules |
| 9529 | use strict; |
| 9530 | use Win32::OLE; |
| 9531 | use Win32::OLE::Const; |
| 9532 | use Getopt::Std; |
| 9533 | |
| 9534 | |
| 9535 | # Vars |
| 9536 | use vars qw(%opts); |
| 9537 | |
| 9538 | |
| 9539 | # Get options |
| 9540 | getopts('i:o:v',\%opts); |
| 9541 | |
| 9542 | |
| 9543 | # Not enough options |
| 9544 | exit &usage unless ($opts{i} && $opts{o}); |
| 9545 | |
| 9546 | |
| 9547 | # Create Excel object |
| 9548 | my $Excel = new Win32::OLE("Excel.Application","Quit") or |
| 9549 | die "Can't start excel: $!"; |
| 9550 | |
| 9551 | |
| 9552 | # Get constants |
| 9553 | my $ExcelConst=Win32::OLE::Const->Load("Microsoft Excel"); |
| 9554 | |
| 9555 | |
| 9556 | # Show Excel |
| 9557 | $Excel->{Visible} = 1 if ($opts{v}); |
| 9558 | |
| 9559 | |
| 9560 | # Open infile |
| 9561 | my $Workbook = $Excel->Workbooks->Open({Filename=>$opts{i}}); |
| 9562 | |
| 9563 | |
| 9564 | # Open outfile |
| 9565 | open (OUTFILE,">$opts{o}") or die "Can't open outfile $opts{o}: $!"; |
| 9566 | |
| 9567 | |
| 9568 | # Print header for outfile |
| 9569 | print OUTFILE <<'EOH'; |
| 9570 | #!/usr/bin/perl -w |
| 9571 | |
| 9572 | |
| 9573 | use strict; |
| 9574 | use Spreadsheet::WriteExcel; |
| 9575 | |
| 9576 | |
| 9577 | use vars qw($workbook %worksheets %formats); |
| 9578 | |
| 9579 | |
| 9580 | $workbook = Spreadsheet::WriteExcel->new("_change_me_.xls"); |
| 9581 | |
| 9582 | |
| 9583 | EOH |
| 9584 | |
| 9585 | |
| 9586 | # Loop all sheets |
| 9587 | foreach my $sheetnum (1..$Excel->Workbooks(1)->Worksheets->Count) { |
| 9588 | |
| 9589 | |
| 9590 | # Format sheet |
| 9591 | my $name=$Excel->Workbooks(1)->Worksheets($sheetnum)->Name; |
| 9592 | print "Sheet $name\n" if ($opts{v}); |
| 9593 | print OUTFILE "# Sheet $name\n"; |
| 9594 | print OUTFILE "\$worksheets{'$name'} = \$workbook->add_worksheet('$name');\n"; |
| 9595 | |
| 9596 | |
| 9597 | # Get usedrange of cells in worksheet |
| 9598 | my $usedrange=$Excel->Workbooks(1)->Worksheets($sheetnum)->UsedRange; |
| 9599 | |
| 9600 | |
| 9601 | # Loop all columns in used range |
| 9602 | foreach my $j (1..$usedrange->Columns->Count){ |
| 9603 | |
| 9604 | |
| 9605 | # Format column |
| 9606 | print "Col $j\n" if ($opts{v}); |
| 9607 | my ($colwidth); |
| 9608 | $colwidth=$usedrange->Columns($j)->ColumnWidth; |
| 9609 | print OUTFILE "# Column $j\n"; |
| 9610 | print OUTFILE "\$worksheets{'$name'}->set_column(".($j-1).",".($j-1). |
| 9611 | ", $colwidth);\n"; |
| 9612 | |
| 9613 | |
| 9614 | # Loop all rows in used range |
| 9615 | foreach my $i (1..$usedrange->Rows->Count){ |
| 9616 | |
| 9617 | |
| 9618 | # Format row |
| 9619 | print "Row $i\n" if ($opts{v}); |
| 9620 | print OUTFILE "# Row $i\n"; |
| 9621 | do { |
| 9622 | my ($rowheight); |
| 9623 | $rowheight=$usedrange->Rows($i)->RowHeight; |
| 9624 | print OUTFILE "\$worksheets{'$name'}->set_row(".($i-1). |
| 9625 | ", $rowheight);\n"; |
| 9626 | } if ($j==1); |
| 9627 | |
| 9628 | |
| 9629 | # Start creating cell format |
| 9630 | my $fname="\$formats{'".$name.'R'.$i.'C'.$j."'}"; |
| 9631 | my $format="$fname=\$workbook->add_format();\n"; |
| 9632 | my $print_format=0; |
| 9633 | |
| 9634 | # Check for borders |
| 9635 | my @bfnames=qw(left right top bottom); |
| 9636 | foreach my $k (1..$usedrange->Cells($i,$j)->Borders->Count) { |
| 9637 | my $lstyle=$usedrange->Cells($i,$j)->Borders($k)->LineStyle; |
| 9638 | if ($lstyle > 0) { |
| 9639 | $format.=$fname."->set_".$bfnames[$k-1]."($lstyle);\n"; |
| 9640 | $print_format=1; |
| 9641 | } |
| 9642 | } |
| 9643 | |
| 9644 | |
| 9645 | # Check for font |
| 9646 | my ($fontattr,$prop,$func,%fontsets,$fontColor); |
| 9647 | %fontsets=(Name=>'set_font', |
| 9648 | Size=>'set_size'); |
| 9649 | while (($prop,$func) = each %fontsets) { |
| 9650 | $fontattr=$usedrange->Cells($i,$j)->Font->$prop; |
| 9651 | if ($fontattr ne "") { |
| 9652 | $format.=$fname."->$func('$fontattr');\n"; |
| 9653 | $print_format=1; |
| 9654 | } |
| 9655 | |
| 9656 | |
| 9657 | } |
| 9658 | %fontsets=(Bold=>'set_bold(1)', |
| 9659 | Italic=>'set_italic(1)', |
| 9660 | Underline=>'set_underline(1)', |
| 9661 | Strikethrough=>'set_strikeout(1)', |
| 9662 | Superscript=>'set_script(1)', |
| 9663 | Subscript=>'set_script(2)', |
| 9664 | OutlineFont=>'set_outline(1)', |
| 9665 | Shadow=>'set_shadow(1)'); |
| 9666 | while (($prop,$func) = each %fontsets) { |
| 9667 | $fontattr=$usedrange->Cells($i,$j)->Font->$prop; |
| 9668 | if ($fontattr==1) { |
| 9669 | $format.=$fname."->$func;\n" ; |
| 9670 | |
| 9671 | $print_format=1; |
| 9672 | } |
| 9673 | } |
| 9674 | $fontColor=$usedrange->Cells($i,$j)->Font->ColorIndex(); |
| 9675 | if ($fontColor>0&&$fontColor!=$ExcelConst->{xlColorIndexAutomatic}) { |
| 9676 | $format.=$fname."->set_color(".($fontColor+7).");\n" ; |
| 9677 | $print_format=1; |
| 9678 | } |
| 9679 | |
| 9680 | |
| 9681 | |
| 9682 | # Check text alignment, merging and wrapping |
| 9683 | my ($halign,$valign,$merge,$wrap); |
| 9684 | $halign=$usedrange->Cells($i,$j)->HorizontalAlignment; |
| 9685 | my %hAligns=($ExcelConst->{xlHAlignCenter}=>"'center'", |
| 9686 | $ExcelConst->{xlHAlignJustify}=>"'justify'", |
| 9687 | $ExcelConst->{xlHAlignLeft}=>"'left'", |
| 9688 | $ExcelConst->{xlHAlignRight}=>"'right'", |
| 9689 | $ExcelConst->{xlHAlignFill}=>"'fill'", |
| 9690 | $ExcelConst->{xlHAlignCenterAcrossSelection}=>"'merge'"); |
| 9691 | if ($halign!=$ExcelConst->{xlHAlignGeneral}) { |
| 9692 | $format.=$fname."->set_align($hAligns{$halign});\n"; |
| 9693 | $print_format=1; |
| 9694 | } |
| 9695 | $valign=$usedrange->Cells($i,$j)->VerticalAlignment; |
| 9696 | my %vAligns=($ExcelConst->{xlVAlignBottom}=>"'bottom'", |
| 9697 | $ExcelConst->{xlVAlignCenter}=>"'vcenter'", |
| 9698 | $ExcelConst->{xlVAlignJustify}=>"'vjustify'", |
| 9699 | $ExcelConst->{xlVAlignTop}=>"'top'"); |
| 9700 | if ($valign) { |
| 9701 | $format.=$fname."->set_align($vAligns{$valign});\n"; |
| 9702 | $print_format=1; |
| 9703 | } |
| 9704 | $merge=$usedrange->Cells($i,$j)->MergeCells; |
| 9705 | if ($merge==1) { |
| 9706 | $format.=$fname."->set_merge();\n"; |
| 9707 | |
| 9708 | $print_format=1; |
| 9709 | } |
| 9710 | $wrap=$usedrange->Cells($i,$j)->WrapText; |
| 9711 | if ($wrap==1) { |
| 9712 | $format.=$fname."->set_text_wrap(1);\n"; |
| 9713 | |
| 9714 | $print_format=1; |
| 9715 | } |
| 9716 | |
| 9717 | |
| 9718 | # Check patterns |
| 9719 | my ($pattern,%pats); |
| 9720 | %pats=(-4142=>0,-4125=>2,-4126=>3,-4124=>4,-4128=>5,-4166=>6, |
| 9721 | -4121=>7,-4162=>8); |
| 9722 | $pattern=$usedrange->Cells($i,$j)->Interior->Pattern; |
| 9723 | if ($pattern&&$pattern!=$ExcelConst->{xlPatternAutomatic}) { |
| 9724 | $pattern=$pats{$pattern} if ($pattern<0 && defined $pats{$pattern}); |
| 9725 | $format.=$fname."->set_pattern($pattern);\n"; |
| 9726 | |
| 9727 | # Colors fg/bg |
| 9728 | my ($cIndex); |
| 9729 | $cIndex=$usedrange->Cells($i,$j)->Interior->PatternColorIndex; |
| 9730 | if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) { |
| 9731 | $format.=$fname."->set_bg_color(".($cIndex+7).");\n"; |
| 9732 | } |
| 9733 | $cIndex=$usedrange->Cells($i,$j)->Interior->ColorIndex; |
| 9734 | if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) { |
| 9735 | $format.=$fname."->set_fg_color(".($cIndex+7).");\n"; |
| 9736 | } |
| 9737 | $print_format=1; |
| 9738 | } |
| 9739 | |
| 9740 | |
| 9741 | # Check for number format |
| 9742 | my ($num_format); |
| 9743 | $num_format=$usedrange->Cells($i,$j)->NumberFormat; |
| 9744 | if ($num_format ne "") { |
| 9745 | $format.=$fname."->set_num_format('$num_format');\n"; |
| 9746 | $print_format=1; |
| 9747 | } |
| 9748 | |
| 9749 | |
| 9750 | # Check for contents (text or formula) |
| 9751 | my ($contents); |
| 9752 | $contents=$usedrange->Cells($i,$j)->Formula; |
| 9753 | $contents=$usedrange->Cells($i,$j)->Text if ($contents eq ""); |
| 9754 | |
| 9755 | |
| 9756 | # Print cell |
| 9757 | if ($contents ne "" or $print_format) { |
| 9758 | print OUTFILE "# Cell($i,$j)\n"; |
| 9759 | print OUTFILE $format if ($print_format); |
| 9760 | print OUTFILE "\$worksheets{'$name'}->write(".($i-1).",".($j-1). |
| 9761 | ",'$contents'"; |
| 9762 | print OUTFILE ",$fname" if ($print_format); |
| 9763 | print OUTFILE ");\n"; |
| 9764 | } |
| 9765 | } |
| 9766 | } |
| 9767 | } |
| 9768 | |
| 9769 | |
| 9770 | # Famous last words... |
| 9771 | print OUTFILE "\$workbook->close();\n"; |
| 9772 | |
| 9773 | |
| 9774 | # Close outfile |
| 9775 | close (OUTFILE) or die "Can't close outfile $opts{o}: $!"; |
| 9776 | |
| 9777 | |
| 9778 | #################################################################### |
| 9779 | sub usage { |
| 9780 | printf STDERR "usage: $0 [options]\n". |
| 9781 | "\tOptions:\n". |
| 9782 | "\t\t-v \tverbose mode\n" . |
| 9783 | "\t\t-i <name>\tname of input file\n" . |
| 9784 | "\t\t-o <name>\tname of output file\n"; |
| 9785 | } |
| 9786 | |
| 9787 | |
| 9788 | #################################################################### |
| 9789 | sub END { |
| 9790 | # Quit excel |
| 9791 | do { |
| 9792 | $Excel->{DisplayAlerts} = 0; |
| 9793 | $Excel->Quit; |
| 9794 | } if (defined $Excel); |
| 9795 | } |
| 9796 | |
| 9797 | |
| 9798 | __END__ |
| 9799 | |
| 9800 | |
| 9801 | =head1 NAME |
| 9802 | |
| 9803 | |
| 9804 | lecxe - A Excel file to Spreadsheet::WriteExcel code converter |
| 9805 | |
| 9806 | |
| 9807 | =head1 DESCRIPTION |
| 9808 | |
| 9809 | |
| 9810 | This program takes an MS Excel workbook file as input and from |
| 9811 | that file, produces an output file with Perl code that uses the |
| 9812 | Spreadsheet::WriteExcel module to reproduce the original |
| 9813 | file. |
| 9814 | |
| 9815 | |
| 9816 | =head1 STUFF |
| 9817 | |
| 9818 | |
| 9819 | Additional hands-on editing of the output file might be neccecary |
| 9820 | as: |
| 9821 | |
| 9822 | |
| 9823 | * This program always names the file produced by output script |
| 9824 | _change_me_.xls |
| 9825 | |
| 9826 | |
| 9827 | * Users of international Excel versions will have som work to do |
| 9828 | on list separators and numeric punctation characters. |
| 9829 | |
| 9830 | |
| 9831 | =head1 SEE ALSO |
| 9832 | |
| 9833 | |
| 9834 | L<Win32::OLE>, L<Win32::OLE::Variant>, L<Spreadsheet::WriteExcel> |
| 9835 | |
| 9836 | |
| 9837 | =head1 BUGS |
| 9838 | |
| 9839 | |
| 9840 | * Picks wrong color on cells sometimes. |
| 9841 | |
| 9842 | |
| 9843 | * Probably a few other... |
| 9844 | |
| 9845 | |
| 9846 | =head1 DISCLAIMER |
| 9847 | |
| 9848 | |
| 9849 | I do not guarantee B<ANYTHING> with this program. If you use it you |
| 9850 | are doing so B<AT YOUR OWN RISK>! I may or may not support this |
| 9851 | depending on my time schedule... |
| 9852 | |
| 9853 | |
| 9854 | =head1 AUTHOR |
| 9855 | |
| 9856 | |
| 9857 | t0mas@netlords.net |
| 9858 | |
| 9859 | |
| 9860 | =head1 COPYRIGHT |
| 9861 | |
| 9862 | |
| 9863 | Copyright 2001, t0mas@netlords.net |
| 9864 | |
| 9865 | |
| 9866 | This package is free software; you can redistribute it and/or |
| 9867 | modify it under the same terms as Perl itself. |
| 9868 | |
| 9869 | |
| 9870 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/lecxe.pl> |
| 9871 | |
| 9872 | =head2 Example: convertA1.pl |
| 9873 | |
| 9874 | |
| 9875 | |
| 9876 | This program contains helper functions to deal with the Excel A1 cell |
| 9877 | reference notation. |
| 9878 | |
| 9879 | These functions have been superseded by L<Spreadsheet::WriteExcel::Utility>. |
| 9880 | |
| 9881 | |
| 9882 | |
| 9883 | #!/usr/bin/perl -w |
| 9884 | |
| 9885 | ############################################################################### |
| 9886 | # |
| 9887 | # This program contains helper functions to deal with the Excel A1 cell |
| 9888 | # reference notation. |
| 9889 | # |
| 9890 | # These functions have been superseded by L<Spreadsheet::WriteExcel::Utility>. |
| 9891 | # |
| 9892 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 9893 | # |
| 9894 | |
| 9895 | use strict; |
| 9896 | |
| 9897 | print "\n"; |
| 9898 | print "Cell B7 is equivalent to ("; |
| 9899 | print join " ", cell_to_rowcol('B7'); |
| 9900 | print ") in row column notation.\n"; |
| 9901 | |
| 9902 | print "Cell \$B7 is equivalent to ("; |
| 9903 | print join " ", cell_to_rowcol('$B7'); |
| 9904 | print ") in row column notation.\n"; |
| 9905 | |
| 9906 | print "Cell B\$7 is equivalent to ("; |
| 9907 | print join " ", cell_to_rowcol('B$7'); |
| 9908 | print ") in row column notation.\n"; |
| 9909 | |
| 9910 | print "Cell \$B\$7 is equivalent to ("; |
| 9911 | print join " ", cell_to_rowcol('$B$7'); |
| 9912 | print ") in row column notation.\n\n"; |
| 9913 | |
| 9914 | print "Row and column (1999, 29) are equivalent to "; |
| 9915 | print rowcol_to_cell(1999, 29), ".\n"; |
| 9916 | |
| 9917 | print "Row and column (1999, 29, 0, 1) are equivalent to "; |
| 9918 | print rowcol_to_cell(1999, 29, 0, 1), ".\n\n"; |
| 9919 | |
| 9920 | print "The base cell is: Z7\n"; |
| 9921 | print "Increment the row: ", inc_cell_row('Z7'), "\n"; |
| 9922 | print "Decrement the row: ", dec_cell_row('Z7'), "\n"; |
| 9923 | print "Increment the column: ", inc_cell_col('Z7'), "\n"; |
| 9924 | print "Decrement the column: ", dec_cell_col('Z7'), "\n\n"; |
| 9925 | |
| 9926 | |
| 9927 | ############################################################################### |
| 9928 | # |
| 9929 | # rowcol_to_cell($row, $col, $row_absolute, $col_absolute) |
| 9930 | # |
| 9931 | # Convert a zero based row and column reference to a A1 reference. For example |
| 9932 | # (0, 2) to C1. $row_absolute, $col_absolute are optional. They are boolean |
| 9933 | # values used to indicate if the row or column value is absolute, i.e. if it is |
| 9934 | # prefixed by a $ sign: eg. (0, 2, 0, 1) converts to $C1. |
| 9935 | # |
| 9936 | # Returns: a cell reference string. |
| 9937 | # |
| 9938 | sub rowcol_to_cell { |
| 9939 | |
| 9940 | my $row = $_[0]; |
| 9941 | my $col = $_[1]; |
| 9942 | my $row_abs = $_[2] || 0; |
| 9943 | my $col_abs = $_[3] || 0; |
| 9944 | |
| 9945 | |
| 9946 | if ($row_abs) { |
| 9947 | $row_abs = '$' |
| 9948 | } |
| 9949 | else { |
| 9950 | $row_abs = '' |
| 9951 | } |
| 9952 | |
| 9953 | if ($col_abs) { |
| 9954 | $col_abs = '$' |
| 9955 | } |
| 9956 | else { |
| 9957 | $col_abs = '' |
| 9958 | } |
| 9959 | |
| 9960 | |
| 9961 | my $int = int ($col / 26); |
| 9962 | my $frac = $col % 26 +1; |
| 9963 | |
| 9964 | my $chr1 =''; |
| 9965 | my $chr2 =''; |
| 9966 | |
| 9967 | |
| 9968 | if ($frac != 0) { |
| 9969 | $chr2 = chr (ord('A') + $frac -1);; |
| 9970 | } |
| 9971 | |
| 9972 | if ($int > 0) { |
| 9973 | $chr1 = chr (ord('A') + $int -1); |
| 9974 | } |
| 9975 | |
| 9976 | $row++; # Zero index to 1-index |
| 9977 | |
| 9978 | return $col_abs . $chr1 . $chr2 . $row_abs. $row; |
| 9979 | } |
| 9980 | |
| 9981 | |
| 9982 | ############################################################################### |
| 9983 | # |
| 9984 | # cell_to_rowcol($cell_ref) |
| 9985 | # |
| 9986 | # Convert an Excel cell reference in A1 notation to a zero based row and column |
| 9987 | # reference; converts C1 to (0, 2, 0, 0). |
| 9988 | # |
| 9989 | # Returns: row, column, row_is_absolute, column_is_absolute |
| 9990 | # |
| 9991 | # |
| 9992 | sub cell_to_rowcol { |
| 9993 | |
| 9994 | my $cell = shift; |
| 9995 | |
| 9996 | $cell =~ /(\$?)([A-I]?[A-Z])(\$?)(\d+)/; |
| 9997 | |
| 9998 | my $col_abs = $1 eq "" ? 0 : 1; |
| 9999 | my $col = $2; |
| 10000 | my $row_abs = $3 eq "" ? 0 : 1; |
| 10001 | my $row = $4; |
| 10002 | |
| 10003 | # Convert base26 column string to number |
| 10004 | # All your Base are belong to us. |
| 10005 | my @chars = split //, $col; |
| 10006 | my $expn = 0; |
| 10007 | $col = 0; |
| 10008 | |
| 10009 | while (@chars) { |
| 10010 | my $char = pop(@chars); # LS char first |
| 10011 | $col += (ord($char) -ord('A') +1) * (26**$expn); |
| 10012 | $expn++; |
| 10013 | } |
| 10014 | |
| 10015 | # Convert 1-index to zero-index |
| 10016 | $row--; |
| 10017 | $col--; |
| 10018 | |
| 10019 | return $row, $col, $row_abs, $col_abs; |
| 10020 | } |
| 10021 | |
| 10022 | |
| 10023 | ############################################################################### |
| 10024 | # |
| 10025 | # inc_cell_row($cell_ref) |
| 10026 | # |
| 10027 | # Increments the row number of an Excel cell reference in A1 notation. |
| 10028 | # For example C3 to C4 |
| 10029 | # |
| 10030 | # Returns: a cell reference string. |
| 10031 | # |
| 10032 | sub inc_cell_row { |
| 10033 | |
| 10034 | my $cell = shift; |
| 10035 | my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); |
| 10036 | |
| 10037 | $row++; |
| 10038 | |
| 10039 | return rowcol_to_cell($row, $col, $row_abs, $col_abs); |
| 10040 | } |
| 10041 | |
| 10042 | |
| 10043 | ############################################################################### |
| 10044 | # |
| 10045 | # dec_cell_row($cell_ref) |
| 10046 | # |
| 10047 | # Decrements the row number of an Excel cell reference in A1 notation. |
| 10048 | # For example C4 to C3 |
| 10049 | # |
| 10050 | # Returns: a cell reference string. |
| 10051 | # |
| 10052 | sub dec_cell_row { |
| 10053 | |
| 10054 | my $cell = shift; |
| 10055 | my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); |
| 10056 | |
| 10057 | $row--; |
| 10058 | |
| 10059 | return rowcol_to_cell($row, $col, $row_abs, $col_abs); |
| 10060 | } |
| 10061 | |
| 10062 | |
| 10063 | ############################################################################### |
| 10064 | # |
| 10065 | # inc_cell_col($cell_ref) |
| 10066 | # |
| 10067 | # Increments the column number of an Excel cell reference in A1 notation. |
| 10068 | # For example C3 to D3 |
| 10069 | # |
| 10070 | # Returns: a cell reference string. |
| 10071 | # |
| 10072 | sub inc_cell_col { |
| 10073 | |
| 10074 | my $cell = shift; |
| 10075 | my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); |
| 10076 | |
| 10077 | $col++; |
| 10078 | |
| 10079 | return rowcol_to_cell($row, $col, $row_abs, $col_abs); |
| 10080 | } |
| 10081 | |
| 10082 | |
| 10083 | ############################################################################### |
| 10084 | # |
| 10085 | # dec_cell_col($cell_ref) |
| 10086 | # |
| 10087 | # Decrements the column number of an Excel cell reference in A1 notation. |
| 10088 | # For example D3 to C3 |
| 10089 | # |
| 10090 | # Returns: a cell reference string. |
| 10091 | # |
| 10092 | sub dec_cell_col { |
| 10093 | |
| 10094 | my $cell = shift; |
| 10095 | my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); |
| 10096 | |
| 10097 | $col--; |
| 10098 | |
| 10099 | return rowcol_to_cell($row, $col, $row_abs, $col_abs); |
| 10100 | } |
| 10101 | |
| 10102 | |
| 10103 | |
| 10104 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/convertA1.pl> |
| 10105 | |
| 10106 | =head2 Example: function_locale.pl |
| 10107 | |
| 10108 | |
| 10109 | |
| 10110 | Generate function names for different locales. |
| 10111 | |
| 10112 | This program can be used to generate the hash of known functions for use in |
| 10113 | the Formula.pm module. By default the function names are in English but you |
| 10114 | can also choose to support the following languages: German, French, Spanish, |
| 10115 | Portuguese, Dutch, Finnish, Italian and Swedish. |
| 10116 | |
| 10117 | This would allow you to do something like the following: |
| 10118 | |
| 10119 | $worksheet->write(0, 0, '=SUM(C1:C3)' ); |
| 10120 | $worksheet->write(1, 0, '=SUMME(C1:C3)'); |
| 10121 | $worksheet->write(2, 0, '=SOMME(C1:C3)'); |
| 10122 | $worksheet->write(3, 0, '=SUMA(C1:C3)' ); |
| 10123 | $worksheet->write(4, 0, '=SOMA(C1:C3)' ); |
| 10124 | $worksheet->write(5, 0, '=SOM(C1:C3)' ); |
| 10125 | $worksheet->write(6, 0, '=SUMMA(C1:C3)'); |
| 10126 | $worksheet->write(7, 0, '=SOMMA(C1:C3)'); |
| 10127 | |
| 10128 | Unfortunately, if you wish to support more than one language there are some |
| 10129 | conflicts between function names: |
| 10130 | |
| 10131 | Function Language 1 Language 2 |
| 10132 | ======== ========== ========== |
| 10133 | NB French Dutch |
| 10134 | NA English/French Finnish |
| 10135 | TRIM French English |
| 10136 | DIA Spanish/Portuguese German |
| 10137 | |
| 10138 | Therefore, if you try to generate a hash of function names to support both |
| 10139 | French and English then the function TRIM will be assigned the meaning of the |
| 10140 | first language that defines it, which in this case is French. You can get |
| 10141 | around this by renaming the function for one of the languages and documenting |
| 10142 | the change, for example: TRIM.EN or TRIM.FR. |
| 10143 | |
| 10144 | Please note that this only partially solves the problem of localisation. |
| 10145 | There are also number formats to consider (1.5 == 1,5) and the fact that the |
| 10146 | list separator "," and the array separator ";" are interchanged in different |
| 10147 | locales. |
| 10148 | |
| 10149 | The longest function name is LOI.NORMALE.STANDARD.INVERSE (29 chars) followed |
| 10150 | by NORM.JAKAUMA.NORMIT.KÄÄNT (25 chars). |
| 10151 | The shortest function name in all languages is T. |
| 10152 | |
| 10153 | |
| 10154 | |
| 10155 | |
| 10156 | #!/usr/bin/perl -w |
| 10157 | |
| 10158 | ############################################################################### |
| 10159 | # |
| 10160 | # Generate function names for different locales. |
| 10161 | # |
| 10162 | # This program can be used to generate the hash of known functions for use in |
| 10163 | # the Formula.pm module. By default the function names are in English but you |
| 10164 | # can also choose to support the following languages: German, French, Spanish, |
| 10165 | # Portuguese, Dutch, Finnish, Italian and Swedish. |
| 10166 | # |
| 10167 | # This would allow you to do something like the following: |
| 10168 | # |
| 10169 | # $worksheet->write(0, 0, '=SUM(C1:C3)' ); |
| 10170 | # $worksheet->write(1, 0, '=SUMME(C1:C3)'); |
| 10171 | # $worksheet->write(2, 0, '=SOMME(C1:C3)'); |
| 10172 | # $worksheet->write(3, 0, '=SUMA(C1:C3)' ); |
| 10173 | # $worksheet->write(4, 0, '=SOMA(C1:C3)' ); |
| 10174 | # $worksheet->write(5, 0, '=SOM(C1:C3)' ); |
| 10175 | # $worksheet->write(6, 0, '=SUMMA(C1:C3)'); |
| 10176 | # $worksheet->write(7, 0, '=SOMMA(C1:C3)'); |
| 10177 | # |
| 10178 | # Unfortunately, if you wish to support more than one language there are some |
| 10179 | # conflicts between function names: |
| 10180 | # |
| 10181 | # Function Language 1 Language 2 |
| 10182 | # ======== ========== ========== |
| 10183 | # NB French Dutch |
| 10184 | # NA English/French Finnish |
| 10185 | # TRIM French English |
| 10186 | # DIA Spanish/Portuguese German |
| 10187 | # |
| 10188 | # Therefore, if you try to generate a hash of function names to support both |
| 10189 | # French and English then the function TRIM will be assigned the meaning of the |
| 10190 | # first language that defines it, which in this case is French. You can get |
| 10191 | # around this by renaming the function for one of the languages and documenting |
| 10192 | # the change, for example: TRIM.EN or TRIM.FR. |
| 10193 | # |
| 10194 | # Please note that this only partially solves the problem of localisation. |
| 10195 | # There are also number formats to consider (1.5 == 1,5) and the fact that the |
| 10196 | # list separator "," and the array separator ";" are interchanged in different |
| 10197 | # locales. |
| 10198 | # |
| 10199 | # The longest function name is LOI.NORMALE.STANDARD.INVERSE (29 chars) followed |
| 10200 | # by NORM.JAKAUMA.NORMIT.KÄÄNT (25 chars). |
| 10201 | # The shortest function name in all languages is T. |
| 10202 | # |
| 10203 | # |
| 10204 | # reverse('©'); John McNamara, March 2001, jmcnamara@cpan.org |
| 10205 | # |
| 10206 | |
| 10207 | |
| 10208 | |
| 10209 | use strict; |
| 10210 | |
| 10211 | |
| 10212 | # |
| 10213 | # Modify the following variables to add the language to the function name hash |
| 10214 | # |
| 10215 | my $english = 1; |
| 10216 | my $german = 0; |
| 10217 | my $french = 0; |
| 10218 | my $spanish = 0; |
| 10219 | my $portuguese = 0; |
| 10220 | my $dutch = 0; |
| 10221 | my $finnish = 0; |
| 10222 | my $italian = 0; |
| 10223 | my $swedish = 0; |
| 10224 | |
| 10225 | my %funcs; |
| 10226 | |
| 10227 | # Ignore the headings |
| 10228 | <DATA>; |
| 10229 | |
| 10230 | # Print the beginning of the hash definition |
| 10231 | print " %functions = (\n"; |
| 10232 | print " #" . " " x 37 ; |
| 10233 | print "ptg args class vol\n"; |
| 10234 | |
| 10235 | while (<DATA>){ |
| 10236 | my @F = split " "; |
| 10237 | my $value = $F[0]; |
| 10238 | my $args = $F[1]; |
| 10239 | my $ref = $F[2]; |
| 10240 | my $vol = $F[3]; |
| 10241 | |
| 10242 | print_function($F[4], $value, $args, $ref, $vol) if $english; |
| 10243 | print_function($F[5], $value, $args, $ref, $vol) if $german; |
| 10244 | print_function($F[6], $value, $args, $ref, $vol) if $french; |
| 10245 | print_function($F[7], $value, $args, $ref, $vol) if $spanish; |
| 10246 | print_function($F[8], $value, $args, $ref, $vol) if $portuguese; |
| 10247 | print_function($F[9], $value, $args, $ref, $vol) if $dutch; |
| 10248 | print_function($F[10], $value, $args, $ref, $vol) if $finnish; |
| 10249 | print_function($F[11], $value, $args, $ref, $vol) if $italian; |
| 10250 | print_function($F[12], $value, $args, $ref, $vol) if $swedish; |
| 10251 | } |
| 10252 | # Print the end of the hash definition |
| 10253 | print " );\n"; |
| 10254 | |
| 10255 | |
| 10256 | ############################################################################### |
| 10257 | # |
| 10258 | # Function to print the function names. It prints a warning if there is a |
| 10259 | # clash. |
| 10260 | # |
| 10261 | sub print_function { |
| 10262 | |
| 10263 | my $func = shift; |
| 10264 | my $value = shift; |
| 10265 | my $args = shift; |
| 10266 | my $ref = shift; |
| 10267 | my $vol = shift; |
| 10268 | |
| 10269 | $func = "'$func'"; |
| 10270 | |
| 10271 | if (not exists $funcs{$func}) { |
| 10272 | printf(" %-31s => [%4d, %4d, %4d, %4d ],\n", |
| 10273 | $func, $value, $args, $ref, $vol); |
| 10274 | $funcs{$func} = $value; |
| 10275 | } |
| 10276 | else { |
| 10277 | if ($funcs{$func} != $value) { |
| 10278 | print " # Warning "; |
| 10279 | print $func, " is already defined in another language\n"; |
| 10280 | printf(" #%-31s => [%4d, %4d, %4d, %4d ],\n", |
| 10281 | $func, $value, $args, $ref, $vol); |
| 10282 | } |
| 10283 | } |
| 10284 | } |
| 10285 | |
| 10286 | |
| 10287 | # Note: The following data contains the function names in the various |
| 10288 | # languages. These lines are LONG. |
| 10289 | |
| 10290 | __DATA__ |
| 10291 | Value Args Refclass Volatile English German French Spanish Portuguese Dutch Finnish Italian Swedish |
| 10292 | 0 -1 0 0 COUNT ANZAHL NB CONTAR CONT.NÚM AANTAL LASKE CONTA.NUMERI ANTAL |
| 10293 | 1 -1 1 0 IF WENN SI SI SE ALS JOS SE OM |
| 10294 | 2 1 1 0 ISNA ISTNV ESTNA ESNOD É.NÃO.DISP ISNB ONPUUTTUU VAL.NON.DISP ÄRSAKNAD |
| 10295 | 3 1 1 0 ISERROR ISTFEHLER ESTERREUR ESERROR ÉERROS ISFOUT ONVIRHE VAL.ERRORE ÄRFEL |
| 10296 | 4 -1 0 0 SUM SUMME SOMME SUMA SOMA SOM SUMMA SOMMA SUMMA |
| 10297 | 5 -1 0 0 AVERAGE MITTELWERT MOYENNE PROMEDIO MÉDIA GEMIDDELDE KESKIARVO MEDIA MEDEL |
| 10298 | 6 -1 0 0 MIN MIN MIN MIN MÍNIMO MIN MIN MIN MIN |
| 10299 | 7 -1 0 0 MAX MAX MAX MAX MÁXIMO MAX MAKS MAX MAX |
| 10300 | 8 -1 0 0 ROW ZEILE LIGNE FILA LIN RIJ RIVI RIF.RIGA RAD |
| 10301 | 9 -1 0 0 COLUMN SPALTE COLONNE COLUMNA COL KOLOM SARAKE RIF.COLONNA KOLUMN |
| 10302 | 10 0 0 0 NA NV NA NOD NÃO.DISP NB PUUTTUU NON.DISP SAKNAS |
| 10303 | 11 -1 1 0 NPV NBW VAN VNA VPL NHW NNA VAN NETNUVÄRDE |
| 10304 | 12 -1 0 0 STDEV STABW ECARTYPE DESVEST DESVPAD STDEV KESKIHAJONTA DEV.ST STDAV |
| 10305 | 13 -1 1 0 DOLLAR DM FRANC MONEDA MOEDA GULDEN VALUUTTA VALUTA VALUTA |
| 10306 | 14 -1 1 0 FIXED FEST CTXT DECIMAL DEF.NÚM.DEC VAST KIINTEÄ FISSO FASTTAL |
| 10307 | 15 1 1 0 SIN SIN SIN SENO SEN SIN SIN SEN SIN |
| 10308 | 16 1 1 0 COS COS COS COS COS COS COS COS COS |
| 10309 | 17 1 1 0 TAN TAN TAN TAN TAN TAN TAN TAN TAN |
| 10310 | 18 1 1 0 ATAN ARCTAN ATAN ATAN ATAN BOOGTAN ATAN ARCTAN ARCTAN |
| 10311 | 19 0 1 0 PI PI PI PI PI PI PII PI.GRECO PI |
| 10312 | 20 1 1 0 SQRT WURZEL RACINE RAIZ RAIZ WORTEL NELIÖJUURI RADQ ROT |
| 10313 | 21 1 1 0 EXP EXP EXP EXP EXP EXP EKSPONENTTI EXP EXP |
| 10314 | 22 1 1 0 LN LN LN LN LN LN LUONNLOG LN LN |
| 10315 | 23 1 1 0 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 |
| 10316 | 24 1 1 0 ABS ABS ABS ABS ABS ABS ITSEISARVO ASS ABS |
| 10317 | 25 1 1 0 INT GANZZAHL ENT ENTERO INT INTEGER KOKONAISLUKU INT HELTAL |
| 10318 | 26 1 1 0 SIGN VORZEICHEN SIGNE SIGNO SINAL POS.NEG ETUMERKKI SEGNO TECKEN |
| 10319 | 27 2 1 0 ROUND RUNDEN ARRONDI REDONDEAR ARRED AFRONDEN PYÖRISTÄ ARROTONDA AVRUNDA |
| 10320 | 28 -1 0 0 LOOKUP VERWEIS RECHERCHE BUSCAR PROC ZOEKEN HAKU CERCA LETAUPP |
| 10321 | 29 -1 0 1 INDEX INDEX INDEX INDICE ÍNDICE INDEX INDEKSI INDICE INDEX |
| 10322 | 30 2 1 0 REPT WIEDERHOLEN REPT REPETIR REPETIR HERHALING TOISTA RIPETI REP |
| 10323 | 31 3 1 0 MID TEIL STXT EXTRAE EXT.TEXTO DEEL POIMI.TEKSTI STRINGA.ESTRAI EXTEXT |
| 10324 | 32 1 1 0 LEN LÄNGE NBCAR LARGO NÚM.CARACT LENGTE PITUUS LUNGHEZZA LÄNGD |
| 10325 | 33 1 1 0 VALUE WERT CNUM VALOR VALOR WAARDE ARVO VALORE TEXTNUM |
| 10326 | 34 0 1 0 TRUE WAHR VRAI VERDADERO VERDADEIRO WAAR TOSI VERO SANT |
| 10327 | 35 0 1 0 FALSE FALSCH FAUX FALSO FALSO ONWAAR EPÄTOSI FALSO FALSKT |
| 10328 | 36 -1 0 0 AND UND ET Y E EN JA E OCH |
| 10329 | 37 -1 0 0 OR ODER OU O OU OF TAI O ELLER |
| 10330 | 38 1 1 0 NOT NICHT NON NO NÃO NIET EI NON ICKE |
| 10331 | 39 2 1 0 MOD REST MOD RESIDUO MOD REST JAKOJ RESTO REST |
| 10332 | 40 3 0 0 DCOUNT DBANZAHL BDNB BDCONTAR BDCONTAR DBAANTAL TLASKE DB.CONTA.NUMERI DANTAL |
| 10333 | 41 3 0 0 DSUM DBSUMME BDSOMME BDSUMA BDSOMA DBSOM TSUMMA DB.SOMMA DSUMMA |
| 10334 | 42 3 0 0 DAVERAGE DBMITTELWERT BDMOYENNE BDPROMEDIO BDMÉDIA DBGEMIDDELDE TKESKIARVO DB.MEDIA DMEDEL |
| 10335 | 43 3 0 0 DMIN DBMIN BDMIN BDMIN BDMÍN DBMIN TMIN DB.MIN DMIN |
| 10336 | 44 3 0 0 DMAX DBMAX BDMAX BDMAX BDMÁX DBMAX TMAKS DB.MAX DMAX |
| 10337 | 45 3 0 0 DSTDEV DBSTDABW BDECARTYPE BDDESVEST BDEST DBSTDEV TKESKIHAJONTA DB.DEV.ST DSTDAV |
| 10338 | 46 -1 0 0 VAR VARIANZ VAR VAR VAR VAR VAR VAR VARIANS |
| 10339 | 47 3 0 0 DVAR DBVARIANZ BDVAR BDVAR BDVAREST DBVAR TVARIANSSI DB.VAR DVARIANS |
| 10340 | 48 2 1 0 TEXT TEXT TEXTE TEXTO TEXTO TEKST TEKSTI TESTO TEXT |
| 10341 | 49 -1 0 0 LINEST RGP DROITEREG ESTIMACION.LINEAL PROJ.LIN LIJNSCH LINREGR REGR.LIN REGR |
| 10342 | 50 -1 0 0 TREND TREND TENDANCE TENDENCIA TENDÊNCIA TREND SUUNTAUS TENDENZA TREND |
| 10343 | 51 -1 0 0 LOGEST RKP LOGREG ESTIMACION.LOGARITMICA PROJ.LOG LOGSCH LOGREGR REGR.LOG EXPREGR |
| 10344 | 52 -1 0 0 GROWTH VARIATION CROISSANCE CRECIMIENTO CRESCIMENTO GROEI KASVU CRESCITA EXPTREND |
| 10345 | 56 -1 1 0 PV BW VA VA VP HW NA VA NUVÄRDE |
| 10346 | 57 -1 1 0 FV ZW VC VF VF TW TULEVA.ARVO VAL.FUT SLUTVÄRDE |
| 10347 | 58 -1 1 0 NPER ZZR NPM NPER NPER NPER NJAKSO NUM.RATE PERIODER |
| 10348 | 59 -1 1 0 PMT RMZ VPM PAGO PGTO BET MAKSU RATA BETALNING |
| 10349 | 60 -1 1 0 RATE ZINS TAUX TASA TAXA RENTE KORKO TASSO RÄNTA |
| 10350 | 61 3 0 0 MIRR QIKV TRIM TIRM MTIR GIR MSISÄINEN TIR.VAR MODIR |
| 10351 | 62 -1 0 0 IRR IKV TRI TIR TIR IR SISÄINEN.KORKO TIR.COST IR |
| 10352 | 63 0 1 1 RAND ZUFALLSZAHL ALEA ALEATORIO ALEATÓRIO ASELECT SATUNNAISLUKU CASUALE SLUMP |
| 10353 | 64 -1 0 0 MATCH VERGLEICH EQUIV COINCIDIR CORRESP VERGELIJKEN VASTINE CONFRONTA PASSA |
| 10354 | 65 3 1 0 DATE DATUM DATE FECHA DATA DATUM PÄIVÄYS DATA DATUM |
| 10355 | 66 3 1 0 TIME ZEIT TEMPS NSHORA TEMPO TIJD AIKA ORARIO KLOCKSLAG |
| 10356 | 67 1 1 0 DAY TAG JOUR DIA DIA DAG PÄIVÄ GIORNO DAG |
| 10357 | 68 1 1 0 MONTH MONAT MOIS MES MÊS MAAND KUUKAUSI MESE MÅNAD |
| 10358 | 69 1 1 0 YEAR JAHR ANNEE AÑO ANO JAAR VUOSI ANNO ÅR |
| 10359 | 70 -1 1 0 WEEKDAY WOCHENTAG JOURSEM DIASEM DIA.DA.SEMANA WEEKDAG VIIKONPÄIVÄ GIORNO.SETTIMANA VECKODAG |
| 10360 | 71 1 1 0 HOUR STUNDE HEURE HORA HORA UUR TUNNIT ORA TIMME |
| 10361 | 72 1 1 0 MINUTE MINUTE MINUTE MINUTO MINUTO MINUUT MINUUTIT MINUTO MINUT |
| 10362 | 73 1 1 0 SECOND SEKUNDE SECONDE SEGUNDO SEGUNDO SECONDE SEKUNNIT SECONDO SEKUND |
| 10363 | 74 0 1 1 NOW JETZT MAINTENANT AHORA AGORA NU NYT ADESSO NU |
| 10364 | 75 1 0 1 AREAS BEREICHE ZONES AREAS ÁREAS BEREIKEN ALUEET AREE OMRÅDEN |
| 10365 | 76 1 0 1 ROWS ZEILEN LIGNES FILAS LINS RIJEN RIVIT RIGHE RADER |
| 10366 | 77 1 0 1 COLUMNS SPALTEN COLONNES COLUMNAS COLS KOLOMMEN SARAKKEET COLONNE KOLUMNER |
| 10367 | 78 -1 0 1 OFFSET BEREICH.VERSCHIEBEN DECALER DESREF DESLOC VERSCHUIVING SIIRTYMÄ SCARTO FÖRSKJUTNING |
| 10368 | 82 -1 1 0 SEARCH SUCHEN CHERCHE HALLAR LOCALIZAR VIND.SPEC KÄY.LÄPI RICERCA SÖK |
| 10369 | 83 1 1 0 TRANSPOSE MTRANS TRANSPOSE TRANSPONER TRANSPOR TRANSPONEREN TRANSPONOI MATR.TRASPOSTA TRANSPONERA |
| 10370 | 86 1 1 0 TYPE TYP TYPE TIPO TIPO TYPE TYYPPI TIPO VÄRDETYP |
| 10371 | 97 2 1 0 ATAN2 ARCTAN2 ATAN2 ATAN2 ATAN2 BOOGTAN2 ATAN2 ARCTAN.2 ARCTAN2 |
| 10372 | 98 1 1 0 ASIN ARCSIN ASIN ASENO ASEN BOOGSIN ASIN ARCSEN ARCSIN |
| 10373 | 99 1 1 0 ACOS ARCCOS ACOS ACOS ACOS BOOGCOS ACOS ARCCOS ARCCOS |
| 10374 | 100 -1 1 0 CHOOSE WAHL CHOISIR ELEGIR ESCOLHER KIEZEN VALITSE.INDEKSI SCEGLI VÄLJ |
| 10375 | 101 -1 0 0 HLOOKUP WVERWEIS RECHERCHEH BUSCARH PROCH HORIZ.ZOEKEN VHAKU CERCA.ORIZZ LETAKOLUMN |
| 10376 | 102 -1 0 0 VLOOKUP SVERWEIS RECHERCHEV BUSCARV PROCV VERT.ZOEKEN PHAKU CERCA.VERT LETARAD |
| 10377 | 105 1 0 0 ISREF ISTBEZUG ESTREF ESREF ÉREF ISVERWIJZING ONVIITT VAL.RIF ÄRREF |
| 10378 | 109 -1 1 0 LOG LOG LOG LOG LOG LOG LOG LOG LOG |
| 10379 | 111 1 1 0 CHAR ZEICHEN CAR CARACTER CARACT TEKEN MERKKI CODICE.CARATT TECKENKOD |
| 10380 | 112 1 1 0 LOWER KLEIN MINUSCULE MINUSC MINÚSCULA KLEINE.LETTERS PIENET MINUSC GEMENER |
| 10381 | 113 1 1 0 UPPER GROSS MAJUSCULE MAYUSC MAIÚSCULA HOOFDLETTERS ISOT MAIUSC VERSALER |
| 10382 | 114 1 1 0 PROPER GROSS2 NOMPROPRE NOMPROPIO PRI.MAIÚSCULA BEGINLETTERS ERISNIMI MAIUSC.INIZ INITIAL |
| 10383 | 115 -1 1 0 LEFT LINKS GAUCHE IZQUIERDA ESQUERDA LINKS VASEN SINISTRA VÄNSTER |
| 10384 | 116 -1 1 0 RIGHT RECHTS DROITE DERECHA DIREITA RECHTS OIKEA DESTRA HÖGER |
| 10385 | 117 2 1 0 EXACT IDENTISCH EXACT IGUAL EXATO GELIJK VERTAA IDENTICO EXAKT |
| 10386 | 118 1 1 0 TRIM GLÄTTEN SUPPRESPACE ESPACIOS ARRUMAR SPATIES.WISSEN POISTA.VÄLIT ANNULLA.SPAZI RENSA |
| 10387 | 119 4 1 0 REPLACE ERSETZEN REMPLACER REEMPLAZAR MUDAR VERVANGEN KORVAA RIMPIAZZA ERSÄTT |
| 10388 | 120 -1 1 0 SUBSTITUTE WECHSELN SUBSTITUE SUSTITUIR SUBSTITUIR SUBSTITUEREN VAIHDA SOSTITUISCI BYT.UT |
| 10389 | 121 1 1 0 CODE CODE CODE CODIGO CÓDIGO CODE KOODI CODICE KOD |
| 10390 | 124 -1 1 0 FIND FINDEN TROUVE ENCONTRAR PROCURAR VIND.ALLES ETSI TROVA HITTA |
| 10391 | 125 -1 0 1 CELL ZELLE CELLULE CELDA CÉL CEL SOLU CELLA CELL |
| 10392 | 126 1 1 0 ISERR ISTFEHL ESTERR ESERR ÉERRO ISFOUT2 ONVIRH VAL.ERR ÄRF |
| 10393 | 127 1 1 0 ISTEXT ISTTEXT ESTTEXTE ESTEXTO ÉTEXTO ISTEKST ONTEKSTI VAL.TESTO ÄRTEXT |
| 10394 | 128 1 1 0 ISNUMBER ISTZAHL ESTNUM ESNUMERO ÉNÚM ISGETAL ONLUKU VAL.NUMERO ÄRTAL |
| 10395 | 129 1 1 0 ISBLANK ISTLEER ESTVIDE ESBLANCO ÉCÉL.VAZIA ISLEEG ONTYHJÄ VAL.VUOTO ÄRTOM |
| 10396 | 130 1 0 0 T T T T T T T T T |
| 10397 | 131 1 0 0 N N N N N N N NUM N |
| 10398 | 140 1 1 0 DATEVALUE DATWERT DATEVAL FECHANUMERO DATA.VALOR DATUMWAARDE PÄIVÄYSARVO DATA.VALORE DATUMVÄRDE |
| 10399 | 141 1 1 0 TIMEVALUE ZEITWERT TEMPSVAL HORANUMERO VALOR.TEMPO TIJDWAARDE AIKA_ARVO ORARIO.VALORE TIDVÄRDE |
| 10400 | 142 3 1 0 SLN LIA AMORLIN SLN DPD LIN.AFSCHR STP AMMORT.COST LINAVSKR |
| 10401 | 143 4 1 0 SYD DIA SYD SYD SDA SYD VUOSIPOISTO AMMORT.ANNUO ÅRSAVSKR |
| 10402 | 144 -1 1 0 DDB GDA DDB DDB BDD DDB DDB AMMORT DEGAVSKR |
| 10403 | 148 -1 1 1 INDIRECT INDIREKT INDIRECT INDIRECTO INDIRETO INDIRECT EPÄSUORA INDIRETTO INDIREKT |
| 10404 | 150 -1 1 0 CALL AUFRUFEN FONCTION.APPELANTE LLAMAR CHAMAR ROEPEN KUTSU RICHIAMA ANROPA |
| 10405 | 162 1 1 0 CLEAN SÄUBERN EPURAGE LIMPIAR TIRAR WISSEN.CONTROL SIIVOA LIBERA STÄDA |
| 10406 | 163 1 2 0 MDETERM MDET DETERMAT MDETERM MATRIZ.DETERM DETERMINANTMAT MDETERM MATR.DETERM MDETERM |
| 10407 | 164 1 2 0 MINVERSE MINV INVERSEMAT MINVERSA MATRIZ.INVERSO INVERSEMAT MKÄÄNTEINEN MATR.INVERSA MINVERT |
| 10408 | 165 2 2 0 MMULT MMULT PRODUITMAT MMULT MATRIZ.MULT PRODUKTMAT MKERRO MATR.PRODOTTO MMULT |
| 10409 | 167 -1 1 0 IPMT ZINSZ INTPER PAGOINT IPGTO IBET IPMT INTERESSI RBETALNING |
| 10410 | 168 -1 1 0 PPMT KAPZ PRINCPER PAGOPRIN PPGTO PBET PPMT P.RATA AMORT |
| 10411 | 169 -1 0 0 COUNTA ANZAHL2 NBVAL CONTARA CONT.VALORES AANTALARG LASKE.A CONTA.VALORI ANTALV |
| 10412 | 183 -1 0 0 PRODUCT PRODUKT PRODUIT PRODUCTO MULT PRODUKT TULO PRODOTTO PRODUKT |
| 10413 | 184 1 1 0 FACT FAKULTÄT FACT FACT FATORIAL FACULTEIT KERTOMA FATTORIALE FAKULTET |
| 10414 | 189 3 0 0 DPRODUCT DBPRODUKT BDPRODUIT BDPRODUCTO BDMULTIPL DBPRODUKT TTULO DB.PRODOTTO DPRODUKT |
| 10415 | 190 1 1 0 ISNONTEXT ISTKTEXT ESTNONTEXTE ESNOTEXTO É.NÃO.TEXTO ISGEENTEKST ONEI_TEKSTI VAL.NON.TESTO ÄREJTEXT |
| 10416 | 193 -1 0 0 STDEVP STABWN ECARTYPEP DESVESTP DESVPADP STDEVP KESKIHAJONTAP DEV.ST.POP STDAVP |
| 10417 | 194 -1 0 0 VARP VARIANZEN VAR.P VARP VARP VARP VARP VAR.POP VARIANSP |
| 10418 | 195 3 0 0 DSTDEVP DBSTDABWN BDECARTYPEP BDDESVESTP BDDESVPA DBSTDEVP TKESKIHAJONTAP DB.DEV.ST.POP DSTDAVP |
| 10419 | 196 3 0 0 DVARP DBVARIANZEN BDVARP BDVARP BDVARP DBVARP TVARIANSSIP DB.VAR.POP DVARIANSP |
| 10420 | 197 -1 1 0 TRUNC KÜRZEN TRONQUE TRUNCAR TRUNCAR GEHEEL KATKAISE TRONCA AVKORTA |
| 10421 | 198 1 1 0 ISLOGICAL ISTLOG ESTLOGIQUE ESLOGICO ÉLÓGICO ISLOGISCH ONTOTUUS VAL.LOGICO ÄRLOGISK |
| 10422 | 199 3 0 0 DCOUNTA DBANZAHL2 BDNBVAL BDCONTARA BDCONTARA DBAANTALC TLASKEA DB.CONTA.VALORI DANTALV |
| 10423 | 212 2 1 0 ROUNDUP AUFRUNDEN ARRONDI.SUP REDONDEAR.MAS ARREDONDAR.PARA.CIMA AFRONDEN.NAAR.BOVEN PYÖRISTÄ.DES.YLÖS ARROTONDA.PER.ECC AVRUNDA.UPPÅT |
| 10424 | 213 2 1 0 ROUNDDOWN ABRUNDEN ARRONDI.INF REDONDEAR.MENOS ARREDONDAR.PARA.BAIXO AFRONDEN.NAAR.BENEDEN PYÖRISTÄ.DES.ALAS ARROTONDA.PER.DIF AVRUNDA.NEDÅT |
| 10425 | 216 -1 0 0 RANK RANG RANG JERARQUIA ORDEM RANG ARVON.MUKAAN RANGO RANG |
| 10426 | 219 -1 1 0 ADDRESS ADRESSE ADRESSE DIRECCION ENDEREÇO ADRES OSOITE INDIRIZZO ADRESS |
| 10427 | 220 -1 1 0 DAYS360 TAGE360 JOURS360 DIAS360 DIAS360 DAGEN360 PÄIVÄT360 GIORNO360 DAGAR360 |
| 10428 | 221 0 1 1 TODAY HEUTE AUJOURDHUI HOY HOJE VANDAAG TÄMÄ.PÄIVÄ OGGI IDAG |
| 10429 | 222 -1 1 0 VDB VDB VDB DVS BDV VDB VDB AMMORT.VAR VDEGRAVSKR |
| 10430 | 227 -1 0 0 MEDIAN MEDIAN MEDIANE MEDIANA MED MEDIAAN MEDIAANI MEDIANA MEDIAN |
| 10431 | 228 -1 2 0 SUMPRODUCT SUMMENPRODUKT SOMMEPROD SUMAPRODUCTO SOMARPRODUTO SOMPRODUKT TULOJEN.SUMMA MATR.SOMMA.PRODOTTO PRODUKTSUMMA |
| 10432 | 229 1 1 0 SINH SINHYP SINH SENOH SENH SINH SINH SENH SINH |
| 10433 | 230 1 1 0 COSH COSHYP COSH COSH COSH COSH COSH COSH COSH |
| 10434 | 231 1 1 0 TANH TANHYP TANH TANH TANH TANH TANH TANH TANH |
| 10435 | 232 1 1 0 ASINH ARCSINHYP ASINH ASENOH ASENH BOOGSINH ASINH ARCSENH ARCSINH |
| 10436 | 233 1 1 0 ACOSH ARCCOSHYP ACOSH ACOSH ACOSH BOOGCOSH ACOSH ARCCOSH ARCCOSH |
| 10437 | 234 1 1 0 ATANH ARCTANHYP ATANH ATANH ATANH BOOGTANH ATANH ARCTANH ARCTANH |
| 10438 | 235 3 0 0 DGET DBAUSZUG BDLIRE BDEXTRAER BDEXTRAIR DBLEZEN TNOUDA DB.VALORI DHÄMTA |
| 10439 | 244 1 1 1 INFO INFO INFO INFO INFORMAÇÃO INFO KUVAUS AMBIENTE.INFO INFO |
| 10440 | 247 -1 1 0 DB GDA2 DB DB BD DB DB AMMORT.FISSO DB |
| 10441 | 252 2 0 0 FREQUENCY HÄUFIGKEIT FREQUENCE FRECUENCIA FREQÜÊNCIA INTERVAL TAAJUUS FREQUENZA FREKVENS |
| 10442 | 261 1 1 0 ERROR.TYPE FEHLER.TYP TYPE.ERREUR TIPO.DE.ERROR TIPO.ERRO TYPE.FOUT VIRHEEN.LAJI ERRORE.TIPO FEL.TYP |
| 10443 | 267 -1 1 0 REGISTER.ID REGISTER.KENNUMMER REGISTRE.NUMERO ID.REGISTRO IDENT.REGISTRO REGISTRATIE.ID REKISTERI.TUNNUS IDENTIFICATORE.REGISTRO REGISTRERA.ID |
| 10444 | 269 -1 0 0 AVEDEV MITTELABW ECART.MOYEN DESVPROM DESV.MÉDIO GEM.DEVIATIE KESKIPOIKKEAMA MEDIA.DEV MEDELAVV |
| 10445 | 270 -1 1 0 BETADIST BETAVERT LOI.BETA DISTR.BETA DISTBETA BETA.VERD BEETAJAKAUMA DISTRIB.BETA BETAFÖRD |
| 10446 | 271 1 1 0 GAMMALN GAMMALN LNGAMMA GAMMA.LN LNGAMA GAMMA.LN GAMMALN LN.GAMMA GAMMALN |
| 10447 | 272 -1 1 0 BETAINV BETAINV BETA.INVERSE DISTR.BETA.INV BETA.ACUM.INV BETA.INV BEETAJAKAUMA.KÄÄNT INV.BETA BETAINV |
| 10448 | 273 4 1 0 BINOMDIST BINOMVERT LOI.BINOMIALE DISTR.BINOM DISTRBINOM BINOMIALE.VERD BINOMIJAKAUMA DISTRIB.BINOM BINOMFÖRD |
| 10449 | 274 2 1 0 CHIDIST CHIVERT LOI.KHIDEUX DISTR.CHI DIST.QUI CHI.KWADRAAT CHIJAKAUMA DISTRIB.CHI CHI2FÖRD |
| 10450 | 275 2 1 0 CHIINV CHIINV KHIDEUX.INVERSE PRUEBA.CHI.INV INV.QUI CHI.KWADRAAT.INV CHIJAKAUMA.KÄÄNT INV.CHI CHI2INV |
| 10451 | 276 2 1 0 COMBIN KOMBINATIONEN COMBIN COMBINAT COMBIN COMBINATIES KOMBINAATIO COMBINAZIONE KOMBIN |
| 10452 | 277 3 1 0 CONFIDENCE KONFIDENZ INTERVALLE.CONFIANCE INTERVALO.CONFIANZA INT.CONFIANÇA BETROUWBAARHEID LUOTTAMUSVÄLI CONFIDENZA KONFIDENS |
| 10453 | 278 3 1 0 CRITBINOM KRITBINOM CRITERE.LOI.BINOMIALE BINOM.CRIT CRIT.BINOM CRIT.BINOM BINOMIJAKAUMA.KRIT CRIT.BINOM KRITBINOM |
| 10454 | 279 1 1 0 EVEN GERADE PAIR REDONDEA.PAR PAR EVEN PARILLINEN PARI JÄMN |
| 10455 | 280 3 1 0 EXPONDIST EXPONVERT LOI.EXPONENTIELLE DISTR.EXP DISTEXPON EXPON.VERD EKSPONENTIAALIJAKAUMA DISTRIB.EXP EXPONFÖRD |
| 10456 | 281 3 1 0 FDIST FVERT LOI.F DISTR.F DISTF F.VERDELING FJAKAUMA DISTRIB.F FFÖRD |
| 10457 | 282 3 1 0 FINV FINV INVERSE.LOI.F DISTR.F.INV INVF F.INVERSE FJAKAUMA.KÄÄNT INV.F FINV |
| 10458 | 283 1 1 0 FISHER FISHER FISHER FISHER FISHER FISHER FISHER FISHER FISHER |
| 10459 | 284 1 1 0 FISHERINV FISHERINV FISHER.INVERSE PRUEBA.FISHER.INV FISHERINV FISHER.INV FISHER.KÄÄNT INV.FISHER FISHERINV |
| 10460 | 285 2 1 0 FLOOR UNTERGRENZE PLANCHER MULTIPLO.INFERIOR ARREDMULTB AFRONDEN.BENEDEN PYÖRISTÄ.KERR.ALAS ARROTONDA.DIFETTO RUNDA.NER |
| 10461 | 286 4 1 0 GAMMADIST GAMMAVERT LOI.GAMMA DISTR.GAMMA DISTGAMA GAMMA.VERD GAMMAJAKAUMA DISTRIB.GAMMA GAMMAFÖRD |
| 10462 | 287 3 1 0 GAMMAINV GAMMAINV LOI.GAMMA.INVERSE DISTR.GAMMA.INV INVGAMA GAMMA.INV GAMMAJAKAUMA.KÄÄNT INV.GAMMA GAMMAINV |
| 10463 | 288 2 1 0 CEILING OBERGRENZE PLAFOND MULTIPLO.SUPERIOR TETO AFRONDEN.BOVEN PYÖRISTÄ.KERR.YLÖS ARROTONDA.ECCESSO RUNDA.UPP |
| 10464 | 289 4 1 0 HYPGEOMDIST HYPGEOMVERT LOI.HYPERGEOMETRIQUE DISTR.HIPERGEOM DIST.HIPERGEOM HYPERGEO.VERD HYPERGEOM.JAKAUMA DISTRIB.IPERGEOM HYPGEOMFÖRD |
| 10465 | 290 3 1 0 LOGNORMDIST LOGNORMVERT LOI.LOGNORMALE DISTR.LOG.NORM DIST.LOGNORMAL LOG.NORM.VERD LOGNORM.JAKAUMA DISTRIB.LOGNORM LOGNORMFÖRD |
| 10466 | 291 3 1 0 LOGINV LOGINV LOI.LOGNORMALE.INVERSE DISTR.LOG.INV INVLOG LOG.NORM.INV LOGNORM.JAKAUMA.KÄÄNT INV.LOGNORM LOGINV |
| 10467 | 292 3 1 0 NEGBINOMDIST NEGBINOMVERT LOI.BINOMIALE.NEG NEGBINOMDIST DIST.BIN.NEG NEG.BINOM.VERD BINOMIJAKAUMA.NEG DISTRIB.BINOM.NEG NEGBINOMFÖRD |
| 10468 | 293 4 1 0 NORMDIST NORMVERT LOI.NORMALE DISTR.NORM DIST.NORM NORM.VERD NORM.JAKAUMA DISTRIB.NORM NORMFÖRD |
| 10469 | 294 1 1 0 NORMSDIST STANDNORMVERT LOI.NORMALE.STANDARD DISTR.NORM.ESTAND DIST.NORMP STAND.NORM.VERD NORM.JAKAUMA.NORMIT DISTRIB.NORM.ST NORMSFÖRD |
| 10470 | 295 3 1 0 NORMINV NORMINV LOI.NORMALE.INVERSE DISTR.NORM.INV INV.NORM NORM.INV NORM.JAKAUMA.KÄÄNT INV.NORM NORMINV |
| 10471 | 296 1 1 0 NORMSINV STANDNORMINV LOI.NORMALE.STANDARD.INVERSE DISTR.NORM.ESTAND.INV INV.NORMP STAND.NORM.INV NORM.JAKAUMA.NORMIT.KÄÄNT INV.NORM.ST NORMSINV |
| 10472 | 297 3 1 0 STANDARDIZE STANDARDISIERUNG CENTREE.REDUITE NORMALIZACION PADRONIZAR NORMALISEREN NORMITA NORMALIZZA STANDARDISERA |
| 10473 | 298 1 1 0 ODD UNGERADE IMPAIR REDONDEA.IMPAR ÍMPAR ONEVEN PARITON DISPARI UDDA |
| 10474 | 299 2 1 0 PERMUT VARIATIONEN PERMUTATION PERMUTACIONES PERMUT PERMUTATIES PERMUTAATIO PERMUTAZIONE PERMUT |
| 10475 | 300 3 1 0 POISSON POISSON LOI.POISSON POISSON POISSON POISSON POISSON POISSON POISSON |
| 10476 | 301 3 1 0 TDIST TVERT LOI.STUDENT DISTR.T DISTT T.VERD TJAKAUMA DISTRIB.T TFÖRD |
| 10477 | 302 4 1 0 WEIBULL WEIBULL LOI.WEIBULL DIST.WEIBULL WEIBULL WEIBULL WEIBULL WEIBULL WEIBULL |
| 10478 | 303 2 2 0 SUMXMY2 SUMMEXMY2 SOMME.XMY2 SUMAXMENOSY2 SOMAXMY2 SOM.XMINY.2 EROTUSTEN.NELIÖSUMMA SOMMA.Q.DIFF SUMMAXMY2 |
| 10479 | 304 2 2 0 SUMX2MY2 SUMMEX2MY2 SOMME.X2MY2 SUMAX2MENOSY2 SOMAX2DY2 SOM.X2MINY2 NELIÖSUMMIEN.EROTUS SOMMA.DIFF.Q SUMMAX2MY2 |
| 10480 | 305 2 2 0 SUMX2PY2 SUMMEX2PY2 SOMME.X2PY2 SUMAX2MASY2 SOMAX2SY2 SOM.X2PLUSY2 NELIÖSUMMIEN.SUMMA SOMMA.SOMMA.Q SUMMAX2PY2 |
| 10481 | 306 2 2 0 CHITEST CHITEST TEST.KHIDEUX PRUEBA.CHI TESTE.QUI CHI.TOETS CHITESTI TEST.CHI CHI2TEST |
| 10482 | 307 2 2 0 CORREL KORREL COEFFICIENT.CORRELATION COEF.DE.CORREL CORREL CORRELATIE KORRELAATIO CORRELAZIONE KORREL |
| 10483 | 308 2 2 0 COVAR KOVAR COVARIANCE COVAR COVAR COVARIANTIE KOVARIANSSI COVARIANZA KOVAR |
| 10484 | 309 3 2 0 FORECAST SCHÄTZER PREVISION PRONOSTICO PREVISÃO VOORSPELLEN ENNUSTE PREVISIONE PREDIKTION |
| 10485 | 310 2 2 0 FTEST FTEST TEST.F PRUEBA.F TESTEF F.TOETS FTESTI TEST.F FTEST |
| 10486 | 311 2 2 0 INTERCEPT ACHSENABSCHNITT ORDONNEE.ORIGINE INTERSECCION INTERCEPÇÃO SNIJPUNT LEIKKAUSPISTE INTERCETTA SKÄRNINGSPUNKT |
| 10487 | 312 2 2 0 PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON |
| 10488 | 313 2 2 0 RSQ BESTIMMTHEITSMASS COEFFICIENT.DETERMINATION COEFICIENTE.R2 RQUAD R.KWADRAAT PEARSON.NELIÖ RQ RKV |
| 10489 | 314 2 2 0 STEYX STFEHLERYX ERREUR.TYPE.XY ERROR.TIPICO.XY EPADYX STAND.FOUT.YX KESKIVIRHE ERR.STD.YX STDFELYX |
| 10490 | 315 2 2 0 SLOPE STEIGUNG PENTE PENDIENTE INCLINAÇÃO RICHTING KULMAKERROIN PENDENZA LUTNING |
| 10491 | 316 4 2 0 TTEST TTEST TEST.STUDENT PRUEBA.T TESTET T.TOETS TTESTI TEST.T TTEST |
| 10492 | 317 -1 2 0 PROB WAHRSCHBEREICH PROBABILITE PROBABILIDAD PROB KANS TODENNÄKÖISYYS PROBABILITÀ SANNOLIKHET |
| 10493 | 318 -1 0 0 DEVSQ SUMQUADABW SOMME.CARRES.ECARTS DESVIA2 DESVQ DEV.KWAD OIKAISTU.NELIÖSUMMA DEV.Q KVADAVV |
| 10494 | 319 -1 0 0 GEOMEAN GEOMITTEL MOYENNE.GEOMETRIQUE MEDIA.GEOM MÉDIA.GEOMÉTRICA MEETK.GEM KESKIARVO.GEOM MEDIA.GEOMETRICA GEOMEDEL |
| 10495 | 320 -1 0 0 HARMEAN HARMITTEL MOYENNE.HARMONIQUE MEDIA.ARMO MÉDIA.HARMÔNICA HARM.GEM KESKIARVO.HARM MEDIA.ARMONICA HARMMEDEL |
| 10496 | 321 -1 0 0 SUMSQ QUADRATESUMME SOMME.CARRES SUMA.CUADRADOS SOMAQUAD KWADRATENSOM NELIÖSUMMA SOMMA.Q KVADRATSUMMA |
| 10497 | 322 -1 0 0 KURT KURT KURTOSIS CURTOSIS CURT KURTOSIS KURT CURTOSI TOPPIGHET |
| 10498 | 323 -1 0 0 SKEW SCHIEFE COEFFICIENT.ASYMETRIE COEFICIENTE.ASIMETRIA DISTORÇÃO SCHEEFHEID JAKAUMAN.VINOUS ASIMMETRIA SNEDHET |
| 10499 | 324 -1 0 0 ZTEST GTEST TEST.Z PRUEBA.Z TESTEZ Z.TOETS ZTESTI TEST.Z ZTEST |
| 10500 | 325 2 0 0 LARGE KGRÖSSTE GRANDE.VALEUR K.ESIMO.MAYOR MAIOR GROOTSTE SUURI GRANDE STÖRSTA |
| 10501 | 326 2 0 0 SMALL KKLEINSTE PETITE.VALEUR K.ESIMO.MENOR MENOR KLEINSTE PIENI PICCOLO MINSTA |
| 10502 | 327 2 0 0 QUARTILE QUARTILE QUARTILE CUARTIL QUARTIL KWARTIEL NELJÄNNES QUARTILE KVARTIL |
| 10503 | 328 2 0 0 PERCENTILE QUANTIL CENTILE PERCENTIL PERCENTIL PERCENTIEL PROSENTTIPISTE PERCENTILE PERCENTIL |
| 10504 | 329 -1 0 0 PERCENTRANK QUANTILSRANG RANG.POURCENTAGE RANGO.PERCENTIL ORDEM.PORCENTUAL PERCENT.RANG PROSENTTIJÄRJESTYS PERCENT.RANGO PROCENTRANG |
| 10505 | 330 -1 2 0 MODE MODALWERT MODE MODA MODO MODUS MOODI MODA TYPVÄRDE |
| 10506 | 331 2 0 0 TRIMMEAN GESTUTZTMITTEL MOYENNE.REDUITE MEDIA.ACOTADA MÉDIA.INTERNA GETRIMD.GEM KESKIARVO.TASATTU MEDIA.TRONCATA TRIMMEDEL |
| 10507 | 332 2 1 0 TINV TINV LOI.STUDENT.INVERSE DISTR.T.INV INVT T.INV TJAKAUMA.KÄÄNT INV.T TINV |
| 10508 | 336 -1 1 0 CONCATENATE VERKETTEN CONCATENER CONCATENAR CONCATENAR TEKST.SAMENVOEGEN KETJUTA CONCATENA SAMMANFOGA |
| 10509 | 337 2 1 0 POWER POTENZ PUISSANCE POTENCIA POTÊNCIA MACHT POTENSSI POTENZA UPPHÖJT.TILL |
| 10510 | 342 1 1 0 RADIANS RADIANT RADIANS RADIANES RADIANOS RADIALEN RADIAANIT RADIANTI RADIANER |
| 10511 | 343 1 1 0 DEGREES GRAD DEGRES GRADOS GRAUS GRADEN ASTEET GRADI GRADER |
| 10512 | 344 -1 0 0 SUBTOTAL TEILERGEBNIS SOUS.TOTAL SUBTOTALES SUBTOTAL SUBTOTAAL VÄLISUMMA SUBTOTALE DELSUMMA |
| 10513 | 345 -1 0 0 SUMIF SUMMEWENN SOMME.SI SUMAR.SI SOMASE SOM.ALS SUMMA.JOS SOMMA.SE SUMMA.OM |
| 10514 | 346 2 0 0 COUNTIF ZÄHLENWENN NB.SI CONTAR.SI CONT.SE AANTAL.ALS LASKE.JOS CONTA.SE ANTAL.OM |
| 10515 | 347 1 0 0 COUNTBLANK ANZAHLLEEREZELLEN NB.VIDE CONTAR.BLANCO CONTAR.VAZIO AANTAL.LEGE.CELLEN LASKE.TYHJÄT CONTA.VUOTE ANTAL.TOMMA |
| 10516 | 354 -1 1 0 ROMAN RÖMISCH ROMAIN NUMERO.ROMANO ROMANO ROMEINS ROMAN ROMANO ROMERSK |
| 10517 | |
| 10518 | |
| 10519 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/function_locale.pl> |
| 10520 | |
| 10521 | =head2 Example: writeA1.pl |
| 10522 | |
| 10523 | |
| 10524 | |
| 10525 | This is an example of how to extend the Spreadsheet::WriteExcel module. |
| 10526 | |
| 10527 | Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing |
| 10528 | the package name. The new code provides a write() method that allows you to |
| 10529 | use Excels A1 style cell references. This is not particularly useful but it |
| 10530 | serves as an example of how the module can be extended without modifying the |
| 10531 | code directly. |
| 10532 | |
| 10533 | |
| 10534 | |
| 10535 | #!/usr/bin/perl -w |
| 10536 | |
| 10537 | ############################################################################### |
| 10538 | # |
| 10539 | # This is an example of how to extend the Spreadsheet::WriteExcel module. |
| 10540 | # |
| 10541 | # Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing |
| 10542 | # the package name. The new code provides a write() method that allows you to |
| 10543 | # use Excels A1 style cell references. This is not particularly useful but it |
| 10544 | # serves as an example of how the module can be extended without modifying the |
| 10545 | # code directly. |
| 10546 | # |
| 10547 | # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org |
| 10548 | # |
| 10549 | |
| 10550 | use strict; |
| 10551 | use Spreadsheet::WriteExcel; |
| 10552 | |
| 10553 | # Create a new workbook called simple.xls and add a worksheet |
| 10554 | my $workbook = Spreadsheet::WriteExcel->new("writeA1.xls"); |
| 10555 | my $worksheet = $workbook->add_worksheet(); |
| 10556 | |
| 10557 | # Write numbers or text |
| 10558 | $worksheet->write (0, 0, "Hello"); |
| 10559 | $worksheet->writeA1("A3", "A3" ); |
| 10560 | $worksheet->writeA1("A5", 1.2345 ); |
| 10561 | |
| 10562 | |
| 10563 | ############################################################################### |
| 10564 | # |
| 10565 | # The following will be appended to the Spreadsheet::WriteExcel::Worksheet |
| 10566 | # package. |
| 10567 | # |
| 10568 | |
| 10569 | package Spreadsheet::WriteExcel::Worksheet; |
| 10570 | |
| 10571 | ############################################################################### |
| 10572 | # |
| 10573 | # writeA1($cell, $token, $format) |
| 10574 | # |
| 10575 | # Convert $cell from Excel A1 notation to $row, $col notation and |
| 10576 | # call write() on $token. |
| 10577 | # |
| 10578 | # Returns: return value of called subroutine or -4 for invalid cell |
| 10579 | # reference. |
| 10580 | # |
| 10581 | sub writeA1 { |
| 10582 | my $self = shift; |
| 10583 | my $cell = shift; |
| 10584 | my $col; |
| 10585 | my $row; |
| 10586 | |
| 10587 | if ($cell =~ /([A-z]+)(\d+)/) { |
| 10588 | ($row, $col) = _convertA1($2, $1); |
| 10589 | $self->write($row, $col, @_); |
| 10590 | } else { |
| 10591 | return -4; |
| 10592 | } |
| 10593 | } |
| 10594 | |
| 10595 | ############################################################################### |
| 10596 | # |
| 10597 | # _convertA1($row, $col) |
| 10598 | # |
| 10599 | # Convert Excel A1 notation to $row, $col notation. Convert base26 column |
| 10600 | # string to a number. |
| 10601 | # |
| 10602 | sub _convertA1 { |
| 10603 | my $row = $_[0]; |
| 10604 | my $col = $_[1]; # String in AA notation |
| 10605 | |
| 10606 | my @chars = split //, $col; |
| 10607 | my $expn = 0; |
| 10608 | $col = 0; |
| 10609 | |
| 10610 | while (@chars) { |
| 10611 | my $char = uc(pop(@chars)); # LS char first |
| 10612 | $col += (ord($char) -ord('A') +1) * (26**$expn); |
| 10613 | $expn++; |
| 10614 | } |
| 10615 | |
| 10616 | # Convert 1 index to 0 index |
| 10617 | $row--; |
| 10618 | $col--; |
| 10619 | |
| 10620 | return($row, $col); |
| 10621 | } |
| 10622 | |
| 10623 | |
| 10624 | Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/writeA1.pl> |
| 10625 | |
| 10626 | =head1 AUTHOR |
| 10627 | |
| 10628 | John McNamara jmcnamara@cpan.org |
| 10629 | |
| 10630 | Contributed examples contain the original author's name. |
| 10631 | |
| 10632 | =head1 COPYRIGHT |
| 10633 | |
| 10634 | Copyright MM-MMX, John McNamara. |
| 10635 | |
| 10636 | All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. |
| 10637 | |
| 10638 | =cut |