=pod =head1 NAME Spreadsheet::XLSX::Reader::LibXML - Read xlsx spreadsheet files with LibXML =head1 SYNOPSIS The following uses the 'TestBook.xlsx' file found in the t/test_files/ folder #!/usr/bin/env perl use strict; use warnings; use Spreadsheet::XLSX::Reader::LibXML; my $parser = Spreadsheet::XLSX::Reader::LibXML->new(); my $workbook = $parser->parse( 'TestBook.xlsx' ); if ( !defined $workbook ) { die $parser->error(), "\n"; } for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; } } last;# In order not to read all sheets } ########################### # SYNOPSIS Screen Output # 01: Row, Col = (0, 0) # 02: Value = Category # 03: Unformatted = Category # 04: # 05: Row, Col = (0, 1) # 06: Value = Total # 07: Unformatted = Total # 08: # 09: Row, Col = (0, 2) # 10: Value = Date # 11: Unformatted = Date # 12: # 13: Row, Col = (1, 0) # 14: Value = Red # 16: Unformatted = Red # 17: # 18: Row, Col = (1, 1) # 19: Value = 5 # 20: Unformatted = 5 # 21: # 22: Row, Col = (1, 2) # 23: Value = 2017-2-14 #(shows as 2/14/2017 in the sheet) # 24: Unformatted = 41318 # 25: # More intermediate rows ... # 82: # 83: Row, Col = (6, 2) # 84: Value = 2016-2-6 #(shows as 2/6/2016 in the sheet) # 85: Unformatted = 40944 ########################### =head1 DESCRIPTION This is another module for parsing Excel 2007+ workbooks. The goal of this package is three fold. First, as close as possible produce the same output as is visible in an excel spreadsheet with exposure to underlying settings from Excel. Second, adhere as close as is reasonable to the L API (where it doesn't conflict with the first objective) so that less work would be needed to integrate ParseExcel and this package. Third, to provide an XLSX sheet parser that is built on L. The other two primary options for XLSX parsing on CPAN use either a one-off XML parser (L) or L (L). In general if either of them already work for you without issue then there is no reason to change to this package. I personally found some bugs and functionality boundaries in both that I wanted to improve and by the time I had educated myself enough to make improvement suggestions including root causing the bugs to either the XML parser or the reader logic I had written this. In the process of learning and building I also wrote some additional features for this parser that are not found in the L package. For instance in the L the '$parser' and the '$workbook' are actually the same class. You could combine both steps by calling new with the 'file_name' attribute called out. Afterward it is still possible to call ->error on the instance. Another improvement (From my perspective) is date handling. This package allows for a simple pluggable custom output format that is more flexible than other options as well as handling dates older than 1-January-1900. I leveraged coercions from L to do this but anything that follows that general format will work here. Additionally, this is a L based package. As such it is designed to be (fairly) extensible by writing roles and adding them to this package rather than requiring that you extend the package to some new branch. Read the full documentation for all opportunities! In the realm of extensibility, L has multiple ways to read an XML file but this release only has an L parser option. Future iterations could include a DOM parser option. Additionally this package does not (yet) provide the same access to the formatting elements provided in L. That is on the longish and incomplete TODO list. The package operates on the workbook with three primary tiers of classes. All other classes in this package are for architectual extensibility. =over ---> Workbook level (This class) =over ---> L =over ---> L - L =back =back =back =head2 Primary Methods These are the primary ways to use this class. They can be used to open an .xlsx workbook. They are also ways to investigate information at the workbook level. For information on how to retrieve data from the worksheets see the L documentation. For additional workbook options see the L section. The attributes section also documents all the methods used to adjust the attributes of this class. =head3 new( %attributes ) =over B This is the way to instantiate an instance of this class. It can accept all the L, some, or none. If the instance is started with no arguments then the L are needed to open the xlsx file. B the L B An instance of this class =back =head3 parse( $file_name, $formatter ) =over B This is a convenience method to match the L equivalent. It only works if the L attribute was not set with ->new. It is one way to set the L and L B $file_name = of a valid xlsx file (required) $formatter = see the 'default_format_list' attribute for valid options (optional) B itself when passing with the xlsx file loaded or undef for failure =back =head3 worksheet( $name ) =over B This method will return an object to read values in the worksheet. If no value is passed to $name then the 'next' worksheet in physical order is returned. I<'next' will NOT wrap> B the $name string representing the worksheet object you want to open B a L object with the ability to read the worksheet of that name. Or in 'next' mode it returns undef if past the last sheet B using the implied 'next' worksheet; while( my $worksheet = $workbook->worksheet ){ print "Reading: " . $worksheet->name . "\n"; # get the data needed from this worksheet } =back =head3 start_at_the_beginning =over B This restarts the 'next' worksheet at the first worksheet Bnothing B nothing =back =head3 worksheets =over B This method will return all the worksheets in the workbook as an array. I. Bnothing B an array of L objects with all the available worksheets in the array =back =head3 worksheet_name( $Int ) =over B This method returns the worksheet name for a given physical position in the worksheet from left to right. It counts from zero even if the workbook is in 'count_from_one' mode. Bintegers B the worksheet name B To return only worksheet positions 2 through 4 for $x (2..4){ my $worksheet = $workbook->worksheet( $workbook->worksheet_name( $x ) ); # Read the worksheet here } =back =head3 worksheet_names =over B This method returns an array ref of the worksheet names in the workbook. Bnothing B an array ref B Another way to parse a workbook without building all the sheets at once is; for $sheet_name ( @{$workbook->worksheet_names} ){ my $worksheet = $workbook->worksheet( $sheet_name ); # Read the worksheet here } =back =head3 number_of_sheets =over B This method returns the count of worksheets in the workbook Bnothing B an integer =back =head3 error =over B This returns the most recent error message logged by the package. This method is mostly relevant when an unexpected result is returned by some other method. Bnothing B an error string. =back =head3 get_epoch_year =over B This returns the epoch year defined by the worsheet. Bnothing B 1900 (= windows) or 1904 (= 1904) =back =head2 Attributes Data passed to new when creating an instance (parser). For modification of these attributes see the listed 'attribute methods'. For more information on attributes see L. =head3 error_inst =over B This attribute holds an 'error' object instance. It should have several methods for managing errors. Currently no error codes or error translation options are available but this should make implementation of that easier. B a L instance with the attributes set as; ( should_warn => 0 ) B The minimum list of methods to implement for your own instance is; error set_error clear_error set_warnings if_warn B Methods provided to adjust this attribute =over =B =over B returns this instance =back B =over B Used to get the most recently logged error =back B =over B used to set a new error string =back B =over B used to clear the current error string in this attribute =back B =over B used to turn on or off real time warnings when errors are set =back B =over B a method mostly used to extend this package and see if warnings should be emitted. =back =back =back =head3 file_name =over B This attribute holds the full file name and path for the xlsx file to be parsed. B no default - this must be provided to read a file B any unincrypted xlsx file that can be opened in Microsoft Excel B Methods provided to adjust this attribute =over B =over B change the set file name (this will reboot the workbook instance) =back B =over B this is fundamentally a way to see if the workbook loaded correctly =back =back =back =head3 file_creator =over B This holds the information stored in the Excel Metadata for who created the file originally. B B the value from the file B A string B Methods provided to adjust this attribute =over B =over B returns the name of the file creator =back =back =back =head3 file_date_created =over B This holds the created date in the Excel Metadata for when the file was first built. B B the value from the file B A timestamp string (ISO ish) B Methods provided to adjust this attribute =over B =over B returns the date the file was created =back =back =back =head3 file_modified_by =over B This holds the information stored in the Excel Metadata for who modified the file last. B B the value from the file B A string B Methods provided to adjust this attribute =over B =over B returns the user name of the person who last modified the file =back =back =back =head3 file_date_modified =over B This holds the last modified date in the Excel Metadata for when the file was last changed. B B the value from the file B A timestamp string (ISO ish) B Methods provided to adjust this attribute =over B =over B returns the date when the file was last modified =back =back =back =head3 sheet_parser =over B This sets the way the .xlsx file is parsed. For now the only choice is 'reader'. B 'reader' B 'reader' B Methods provided to adjust this attribute =over B =over B the way to change the parser type =back B =over B returns the currently set parser type =back =back =back =head3 count_from_zero =over B Excel spreadsheets count from 1. L counts from zero. This allows you to choose either way. B 1 B 1 = counting from zero like Spreadsheet::ParseExcel, 0 = Counting from 1 lke Excel B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to change the current attribute setting =back =back =back =head3 file_boundary_flags =over B When you request data past the end of a row or past the bottom of the data this package can return 'EOR' or 'EOF' to indicate that state. This is especially helpful in 'while' loops. The other option is to return 'undef'. This is problematic if some cells in your table are empty which also returns undef. B 1 B 1 = return 'EOR' or 'EOF' flags as appropriate, 0 = return undef when requesting a position that is out of bounds B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to change the current attribute setting =back =back =back =head3 empty_is_end =over B The excel convention is to read the table left to right and top to bottom. Some tables have uneven columns from row to row. This allows the several methods that take 'next' values to wrap after the last element with data rather than going to the max column. B 0 B 1 = treat all columns short of the max column for the sheet as being in the table, 0 = end each row after the last cell with data rather than going to the max sheet column B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to set the current attribute setting =back =back =back =head3 from_the_edge =over B Some data tables start in the top left corner. Others do not. I don't reccomend that practice but when aquiring data in the wild it is often good to adapt. This attribute sets whether the file reads from the top left edge or from the top row with data and starting from the leftmost column with data. B 1 B 1 = treat the top left corner of the sheet even if there is no data in the top row or leftmost column, 0 = Set the minimum row and minimum columns to be the first row and first column with data B Methods provided to adjust this attribute =over B =over B a way to set the current attribute setting =back =back =back =head3 default_format_list =over B This is a departure from L for two reasons. First, it doesn't use the same modules. Second, this accepts a role with two methods where ParseExcel accepts an object instance. B Spreadsheet::XLSX::Reader::LibXML::FmtDefault B a L role with the methods 'get_defined_excel_format' and 'change_output_encoding' it should be noted that libxml2 which is the underlying code for L allways attempts to get the data into perl friendly strings. That means this should only tweak the data on the way out and does not affect the data on the way in. B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to set the current attribute setting =back =back =back =head3 format_string_parser =over B This is the interpreter that turns the excel into a L coercion. If you don't like the output or the method you can write your own Moose Role and add it here. B Spreadsheet::XLSX::Reader::LibXML::ParseExcelFormatStrings B a L role with the method 'parse_excel_format_string' B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to set the current attribute setting =back =back =back =head3 group_return_type =over B Traditionally ParseExcel returns a cell object with lots of methods to reveal information about the cell. In reality this is probably not used very much so in the interest of simplifying you can get a cell object instance set to the cell information. Or you can just get the raw value in the cell or you can get the cell value formatted either the way the sheet specified or the way you specify. See the 'custom_formats' attribute for the L class to insert custom targeted formats for use with the parser. All empty cells return undef no matter what. B instance B instance = returns a populated L instance, unformatted = returns the raw value of the cell with no modifications, value = returns just the formatted value stored in the excel cell B Methods provided to adjust this attribute =over B =over B a way to check the current attribute setting =back B =over B a way to set the current attribute setting =back =back =back =head1 BUILD / INSTALL from Source B<1.> Ensure that you have the libxml2 B libraries installed using your favorite package installer L B<2.> Download a compressed file with the code from your favorite source B<3.> Extract the code from the compressed file. If you are using tar this should work: tar -zxvf Spreadsheet-XLSX-Reader-LibXML-v0.xx.tar.gz B<4.> Change (cd) into the extracted directory B<5.> Run the following =over (For Windows find what version of make was used to compile your perl) perl -V:make (for Windows below substitute the correct make function (s/make/dmake/g)?) =back >perl Makefile.PL >make >make test >make install # As sudo/root >make clean =head1 SUPPORT =over L =back =head1 TODO =over B<1.> Build L to load the libxml2-devel libraries from source and require that and L in the build file. So all needed requirements for L are met This includes the libxml2 and libxml2-dev libraries B<2.> Add a pivot table reader (Not just read the values from the sheet) B<3.> Add calc chain methods B<4.> Add more exposure to workbook formatting methods B<5.> Build a DOM parser alternative for the sheets (Theoretically faster than the reader but uses more memory) =back =head1 AUTHOR =over =item Jed Lund =item jandrew@cpan.org =back =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. This software is copyrighted (c) 2014 by Jed Lund =head1 DEPENDENCIES =over B<5.010> - (L) L L L L L L L L - 0.046 L - 1.026 L- cluck L L L L =back =head1 SEE ALSO =over L - Excel 2003 and earlier L - 2007+ L - 2007+ L =over All lines in this package that use Log::Shiras are commented out =back =back =cut