Workbook.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. # DISCLAIMER OF WARRANTY
  2. # Because this software is licensed free of charge, there is no warranty for the software,
  3. # to the extent permitted by applicable law. Except when otherwise stated in writing
  4. # the copyright holders and/or other parties provide the software "as is" without
  5. # warranty of any kind, either expressed or implied, including, but not limited to,
  6. # the implied warranties of merchantability and fitness for a particular purpose.
  7. # The entire risk as to the quality and performance of the software is with you.
  8. # Should the software prove defective, you assume the cost of all necessary
  9. # servicing, repair, or correction.
  10. # In no event unless required by applicable law or agreed to in writing will any
  11. # copyright holder, or any other party who may modify and/or redistribute the software
  12. # as permitted by the above licence, be liable to you for damages, including any general,
  13. # special, incidental, or consequential damages arising out of the use or inability
  14. # to use the software (including but not limited to loss of data or data being rendered
  15. # inaccurate or losses sustained by you or third parties or a failure of the software
  16. # to operate with any other software), even if such holder or other party
  17. # has been advised of the possibility of such damages.
  18. # AUTHOR
  19. # Current maintainer 0.40+: John McNamara jmcnamara@cpan.org
  20. # Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  21. # Original author: Kawai Takanori (Hippo2000) kwitknr@cpan.org
  22. # COPYRIGHT
  23. # Copyright (c) 2009-2010 John McNamara
  24. # Copyright (c) 2006-2008 Gabor Szabo
  25. # Copyright (c) 2000-2006 Kawai Takanori
  26. # All rights reserved. This is free software. You may distribute under the terms of
  27. # the Artistic License(full text of the Artistic License http://dev.perl.org/licenses/artistic.html).
  28. package Spreadsheet::ParseExcel::SaveParser::Workbook;
  29. ###############################################################################
  30. #
  31. # Spreadsheet::ParseExcel::SaveParser::Workbook - A class for SaveParser Workbooks.
  32. #
  33. # Used in conjunction with Spreadsheet::ParseExcel.
  34. #
  35. # Copyright (c) 2009 John McNamara
  36. # Copyright (c) 2006-2008 Gabor Szabo
  37. # Copyright (c) 2000-2006 Kawai Takanori
  38. #
  39. # perltidy with standard settings.
  40. #
  41. # Documentation after __END__
  42. #
  43. use strict;
  44. use warnings;
  45. use base 'Spreadsheet::ParseExcel::Workbook';
  46. our $VERSION = '0.57';
  47. #==============================================================================
  48. # Spreadsheet::ParseExcel::SaveParser::Workbook
  49. #==============================================================================
  50. sub new {
  51. my ( $sPkg, $oBook ) = @_;
  52. return undef unless ( defined $oBook );
  53. my %oThis = %$oBook;
  54. bless \%oThis, $sPkg;
  55. # re-bless worksheets (and set their _Book properties !!!)
  56. my $sWkP = ref($sPkg) || "$sPkg";
  57. $sWkP =~ s/Workbook$/Worksheet/;
  58. map { bless( $_, $sWkP ); } @{ $oThis{Worksheet} };
  59. map { $_->{_Book} = \%oThis; } @{ $oThis{Worksheet} };
  60. return \%oThis;
  61. }
  62. #------------------------------------------------------------------------------
  63. # Parse (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  64. #------------------------------------------------------------------------------
  65. sub Parse {
  66. my ( $sClass, $sFile, $oWkFmt ) = @_;
  67. my $oBook = Spreadsheet::ParseExcel::Workbook->Parse( $sFile, $oWkFmt );
  68. bless $oBook, $sClass;
  69. # re-bless worksheets (and set their _Book properties !!!)
  70. my $sWkP = ref($sClass) || "$sClass";
  71. $sWkP =~ s/Workbook$/Worksheet/;
  72. map { bless( $_, $sWkP ); } @{ $oBook->{Worksheet} };
  73. map { $_->{_Book} = $oBook; } @{ $oBook->{Worksheet} };
  74. return $oBook;
  75. }
  76. #------------------------------------------------------------------------------
  77. # SaveAs (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  78. #------------------------------------------------------------------------------
  79. sub SaveAs {
  80. my ( $oBook, $sName ) = @_;
  81. # Create a new Excel workbook
  82. my $oWrEx = Spreadsheet::WriteExcel->new($sName);
  83. $oWrEx->compatibility_mode();
  84. my %hFmt;
  85. my $iNo = 0;
  86. my @aAlH = (
  87. 'left', 'left', 'center', 'right',
  88. 'fill', 'justify', 'merge', 'equal_space'
  89. );
  90. my @aAlV = ( 'top', 'vcenter', 'bottom', 'vjustify', 'vequal_space' );
  91. foreach my $pFmt ( @{ $oBook->{Format} } ) {
  92. my $oFmt = $oWrEx->addformat(); # Add Formats
  93. unless ( $pFmt->{Style} ) {
  94. $hFmt{$iNo} = $oFmt;
  95. my $rFont = $pFmt->{Font};
  96. $oFmt->set_font( $rFont->{Name} );
  97. $oFmt->set_size( $rFont->{Height} );
  98. $oFmt->set_color( $rFont->{Color} );
  99. $oFmt->set_bold( $rFont->{Bold} );
  100. $oFmt->set_italic( $rFont->{Italic} );
  101. $oFmt->set_underline( $rFont->{Underline} );
  102. $oFmt->set_font_strikeout( $rFont->{Strikeout} );
  103. $oFmt->set_font_script( $rFont->{Super} );
  104. $oFmt->set_hidden( $rFont->{Hidden} ); #Add
  105. $oFmt->set_locked( $pFmt->{Lock} );
  106. $oFmt->set_align( $aAlH[ $pFmt->{AlignH} ] );
  107. $oFmt->set_align( $aAlV[ $pFmt->{AlignV} ] );
  108. $oFmt->set_rotation( $pFmt->{Rotate} );
  109. $oFmt->set_num_format(
  110. $oBook->{FmtClass}->FmtStringDef( $pFmt->{FmtIdx}, $oBook ) );
  111. $oFmt->set_text_wrap( $pFmt->{Wrap} );
  112. $oFmt->set_pattern( $pFmt->{Fill}->[0] );
  113. $oFmt->set_fg_color( $pFmt->{Fill}->[1] )
  114. if ( ( $pFmt->{Fill}->[1] >= 8 )
  115. && ( $pFmt->{Fill}->[1] <= 63 ) );
  116. $oFmt->set_bg_color( $pFmt->{Fill}->[2] )
  117. if ( ( $pFmt->{Fill}->[2] >= 8 )
  118. && ( $pFmt->{Fill}->[2] <= 63 ) );
  119. $oFmt->set_left(
  120. ( $pFmt->{BdrStyle}->[0] > 7 ) ? 3 : $pFmt->{BdrStyle}->[0] );
  121. $oFmt->set_right(
  122. ( $pFmt->{BdrStyle}->[1] > 7 ) ? 3 : $pFmt->{BdrStyle}->[1] );
  123. $oFmt->set_top(
  124. ( $pFmt->{BdrStyle}->[2] > 7 ) ? 3 : $pFmt->{BdrStyle}->[2] );
  125. $oFmt->set_bottom(
  126. ( $pFmt->{BdrStyle}->[3] > 7 ) ? 3 : $pFmt->{BdrStyle}->[3] );
  127. $oFmt->set_left_color( $pFmt->{BdrColor}->[0] )
  128. if ( ( $pFmt->{BdrColor}->[0] >= 8 )
  129. && ( $pFmt->{BdrColor}->[0] <= 63 ) );
  130. $oFmt->set_right_color( $pFmt->{BdrColor}->[1] )
  131. if ( ( $pFmt->{BdrColor}->[1] >= 8 )
  132. && ( $pFmt->{BdrColor}->[1] <= 63 ) );
  133. $oFmt->set_top_color( $pFmt->{BdrColor}->[2] )
  134. if ( ( $pFmt->{BdrColor}->[2] >= 8 )
  135. && ( $pFmt->{BdrColor}->[2] <= 63 ) );
  136. $oFmt->set_bottom_color( $pFmt->{BdrColor}->[3] )
  137. if ( ( $pFmt->{BdrColor}->[3] >= 8 )
  138. && ( $pFmt->{BdrColor}->[3] <= 63 ) );
  139. }
  140. $iNo++;
  141. }
  142. for ( my $iSheet = 0 ; $iSheet < $oBook->{SheetCount} ; $iSheet++ ) {
  143. my $oWkS = $oBook->{Worksheet}[$iSheet];
  144. my $oWrS = $oWrEx->addworksheet( $oWkS->{Name} );
  145. #Landscape
  146. if ( !$oWkS->{Landscape} ) { # Landscape (0:Horizontal, 1:Vertical)
  147. $oWrS->set_landscape();
  148. }
  149. else {
  150. $oWrS->set_portrait();
  151. }
  152. #Protect
  153. if ( defined $oWkS->{Protect} )
  154. { # Protect ('':NoPassword, Password:Password)
  155. if ( $oWkS->{Protect} ne '' ) {
  156. $oWrS->protect( $oWkS->{Protect} );
  157. }
  158. else {
  159. $oWrS->protect();
  160. }
  161. }
  162. if ( ( $oWkS->{FitWidth} == 1 ) and ( $oWkS->{FitHeight} == 1 ) ) {
  163. # Pages on fit with width and Heigt
  164. $oWrS->fit_to_pages( $oWkS->{FitWidth}, $oWkS->{FitHeight} );
  165. #Print Scale
  166. $oWrS->set_print_scale( $oWkS->{Scale} );
  167. }
  168. else {
  169. #Print Scale
  170. $oWrS->set_print_scale( $oWkS->{Scale} );
  171. # Pages on fit with width and Heigt
  172. $oWrS->fit_to_pages( $oWkS->{FitWidth}, $oWkS->{FitHeight} );
  173. }
  174. # Paper Size
  175. $oWrS->set_paper( $oWkS->{PaperSize} );
  176. # Margin
  177. $oWrS->set_margin_left( $oWkS->{LeftMargin} );
  178. $oWrS->set_margin_right( $oWkS->{RightMargin} );
  179. $oWrS->set_margin_top( $oWkS->{TopMargin} );
  180. $oWrS->set_margin_bottom( $oWkS->{BottomMargin} );
  181. # HCenter
  182. $oWrS->center_horizontally() if ( $oWkS->{HCenter} );
  183. # VCenter
  184. $oWrS->center_vertically() if ( $oWkS->{VCenter} );
  185. # Header, Footer
  186. $oWrS->set_header( $oWkS->{Header}, $oWkS->{HeaderMargin} );
  187. $oWrS->set_footer( $oWkS->{Footer}, $oWkS->{FooterMargin} );
  188. # Print Area
  189. if ( ref( $oBook->{PrintArea}[$iSheet] ) eq 'ARRAY' ) {
  190. my $raP;
  191. for $raP ( @{ $oBook->{PrintArea}[$iSheet] } ) {
  192. $oWrS->print_area(@$raP);
  193. }
  194. }
  195. # Print Title
  196. my $raW;
  197. foreach $raW ( @{ $oBook->{PrintTitle}[$iSheet]->{Row} } ) {
  198. $oWrS->repeat_rows(@$raW);
  199. }
  200. foreach $raW ( @{ $oBook->{PrintTitle}[$iSheet]->{Column} } ) {
  201. $oWrS->repeat_columns(@$raW);
  202. }
  203. # Print Gridlines
  204. if ( $oWkS->{PrintGrid} == 1 ) {
  205. $oWrS->hide_gridlines(0);
  206. }
  207. else {
  208. $oWrS->hide_gridlines(1);
  209. }
  210. # Print Headings
  211. if ( $oWkS->{PrintHeaders} ) {
  212. $oWrS->print_row_col_headers();
  213. }
  214. # Horizontal Page Breaks
  215. $oWrS->set_h_pagebreaks( @{ $oWkS->{HPageBreak} } );
  216. # Veritical Page Breaks
  217. $oWrS->set_v_pagebreaks( @{ $oWkS->{VPageBreak} } );
  218. # PageStart => $oWkS->{PageStart}, # Page number for start
  219. # UsePage => $oWkS->{UsePage}, # Use own start page number
  220. # NoColor => $oWkS->{NoColor}, # Print in blcak-white
  221. # Draft => $oWkS->{Draft}, # Print in draft mode
  222. # Notes => $oWkS->{Notes}, # Print notes
  223. # LeftToRight => $oWkS->{LeftToRight}, # Left to Right
  224. for (
  225. my $iC = $oWkS->{MinCol} ;
  226. defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
  227. $iC++
  228. )
  229. {
  230. if ( defined $oWkS->{ColWidth}[$iC] ) {
  231. if ( $oWkS->{ColWidth}[$iC] > 0 ) {
  232. $oWrS->set_column( $iC, $iC, $oWkS->{ColWidth}[$iC] )
  233. ; #, undef, 1) ;
  234. }
  235. else {
  236. $oWrS->set_column( $iC, $iC, 0, undef, 1 );
  237. }
  238. }
  239. }
  240. for (
  241. my $iR = $oWkS->{MinRow} ;
  242. defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
  243. $iR++
  244. )
  245. {
  246. $oWrS->set_row( $iR, $oWkS->{RowHeight}[$iR] );
  247. for (
  248. my $iC = $oWkS->{MinCol} ;
  249. defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
  250. $iC++
  251. )
  252. {
  253. my $oWkC = $oWkS->{Cells}[$iR][$iC];
  254. if ($oWkC) {
  255. if ( $oWkC->{Merged} ) {
  256. my $oFmtN = $oWrEx->addformat();
  257. $oFmtN->copy( $hFmt{ $oWkC->{FormatNo} } );
  258. $oFmtN->set_merge(1);
  259. $oWrS->write(
  260. $iR,
  261. $iC,
  262. $oBook->{FmtClass}
  263. ->TextFmt( $oWkC->{Val}, $oWkC->{Code} ),
  264. $oFmtN
  265. );
  266. }
  267. else {
  268. $oWrS->write(
  269. $iR,
  270. $iC,
  271. $oBook->{FmtClass}
  272. ->TextFmt( $oWkC->{Val}, $oWkC->{Code} ),
  273. $hFmt{ $oWkC->{FormatNo} }
  274. );
  275. }
  276. }
  277. }
  278. }
  279. }
  280. return $oWrEx;
  281. }
  282. #------------------------------------------------------------------------------
  283. # AddWorksheet (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  284. #------------------------------------------------------------------------------
  285. sub AddWorksheet {
  286. my ( $oBook, $sName, %hAttr ) = @_;
  287. $oBook->AddFormat if ( $#{ $oBook->{Format} } < 0 );
  288. $hAttr{Name} ||= $sName;
  289. $hAttr{LeftMargin} ||= 0;
  290. $hAttr{RightMargin} ||= 0;
  291. $hAttr{TopMargin} ||= 0;
  292. $hAttr{BottomMargin} ||= 0;
  293. $hAttr{HeaderMargin} ||= 0;
  294. $hAttr{FooterMargin} ||= 0;
  295. $hAttr{FitWidth} ||= 0;
  296. $hAttr{FitHeight} ||= 0;
  297. $hAttr{PrintGrid} ||= 0;
  298. my $oWkS = Spreadsheet::ParseExcel::SaveParser::Worksheet->new(%hAttr);
  299. $oWkS->{_Book} = $oBook;
  300. $oWkS->{_SheetNo} = $oBook->{SheetCount};
  301. $oBook->{Worksheet}[ $oBook->{SheetCount} ] = $oWkS;
  302. $oBook->{SheetCount}++;
  303. return $oWkS; #$oBook->{SheetCount} - 1;
  304. }
  305. #------------------------------------------------------------------------------
  306. # AddFont (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  307. #------------------------------------------------------------------------------
  308. sub AddFont {
  309. my ( $oBook, %hAttr ) = @_;
  310. $hAttr{Name} ||= 'Arial';
  311. $hAttr{Height} ||= 10;
  312. $hAttr{Bold} ||= 0;
  313. $hAttr{Italic} ||= 0;
  314. $hAttr{Underline} ||= 0;
  315. $hAttr{Strikeout} ||= 0;
  316. $hAttr{Super} ||= 0;
  317. push @{ $oBook->{Font} }, Spreadsheet::ParseExcel::Font->new(%hAttr);
  318. return $#{ $oBook->{Font} };
  319. }
  320. #------------------------------------------------------------------------------
  321. # AddFormat (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  322. #------------------------------------------------------------------------------
  323. sub AddFormat {
  324. my ( $oBook, %hAttr ) = @_;
  325. $hAttr{Fill} ||= [ 0, 0, 0 ];
  326. $hAttr{BdrStyle} ||= [ 0, 0, 0, 0 ];
  327. $hAttr{BdrColor} ||= [ 0, 0, 0, 0 ];
  328. $hAttr{AlignH} ||= 0;
  329. $hAttr{AlignV} ||= 0;
  330. $hAttr{Rotate} ||= 0;
  331. $hAttr{Landscape} ||= 0;
  332. $hAttr{FmtIdx} ||= 0;
  333. if ( !defined( $hAttr{Font} ) ) {
  334. my $oFont;
  335. if ( defined $hAttr{FontNo} ) {
  336. $oFont = $oBook->{Font}[ $hAttr{FontNo} ];
  337. }
  338. elsif ( !defined $oFont ) {
  339. if ( $#{ $oBook->{Font} } >= 0 ) {
  340. $oFont = $oBook->{Font}[0];
  341. }
  342. else {
  343. my $iNo = $oBook->AddFont;
  344. $oFont = $oBook->{Font}[$iNo];
  345. }
  346. }
  347. $hAttr{Font} = $oFont;
  348. }
  349. push @{ $oBook->{Format} }, Spreadsheet::ParseExcel::Format->new(%hAttr);
  350. return $#{ $oBook->{Format} };
  351. }
  352. #------------------------------------------------------------------------------
  353. # AddCell (for Spreadsheet::ParseExcel::SaveParser::Workbook)
  354. #------------------------------------------------------------------------------
  355. sub AddCell {
  356. my ( $oBook, $iSheet, $iR, $iC, $sVal, $oCell, $sCode ) = @_;
  357. my %rhKey;
  358. $oCell ||= 0;
  359. my $iFmt =
  360. ( UNIVERSAL::isa( $oCell, 'Spreadsheet::ParseExcel::Cell' ) )
  361. ? $oCell->{FormatNo}
  362. : ( ref($oCell) ) ? 0
  363. : $oCell + 0;
  364. $rhKey{FormatNo} = $iFmt;
  365. $rhKey{Format} = $oBook->{Format}[$iFmt];
  366. $rhKey{Val} = $sVal;
  367. $rhKey{Code} = $sCode || '_native_';
  368. $oBook->{_CurSheet} = $iSheet;
  369. my $oNewCell =
  370. Spreadsheet::ParseExcel::_NewCell( $oBook, $iR, $iC, %rhKey );
  371. Spreadsheet::ParseExcel::_SetDimension( $oBook, $iR, $iC, $iC );
  372. return $oNewCell;
  373. }
  374. 1;
  375. __END__
  376. =pod
  377. =head1 NAME
  378. Spreadsheet::ParseExcel::SaveParser::Workbook - A class for SaveParser Workbooks.
  379. =head1 SYNOPSIS
  380. See the documentation for Spreadsheet::ParseExcel.
  381. =head1 DESCRIPTION
  382. This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
  383. =head1 AUTHOR
  384. Maintainer 0.40+: John McNamara jmcnamara@cpan.org
  385. Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  386. Original author: Kawai Takanori kwitknr@cpan.org
  387. =head1 COPYRIGHT
  388. Copyright (c) 2009-2010 John McNamara
  389. Copyright (c) 2006-2008 Gabor Szabo
  390. Copyright (c) 2000-2006 Kawai Takanori
  391. All rights reserved.
  392. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
  393. =cut