简体   繁体   English

如何在perl中逐行读取.xlsm文件

[英]how to read .xlsm file line by line in perl

This code reads the data from .xlsm file .this read data each cell by cell like cell(0,0) and like so on from this code i am not able to read data of whole line .I want to read .xlsm file line by line and also only of specified worksheet not all. 这段代码从.xlsm文件中读取数据。此代码像cell(0,0)一样逐个单元格地读取数据,依此类推,我无法读取整行的数据。我想读取.xlsm文件行按行,也仅按指定的工作表,而不是全部。

#!/usr/bin/env perl
use strict;
use warnings;
use Spreadsheet::Reader::ExcelXML qw( :just_the_data);

my $workbook  = Spreadsheet::Reader::ExcelXML->new( file => 'filename.xlsm' 
);

if ( !$workbook->file_opened ) {
     die $workbook->error(), "\n";
 }

for my $worksheet ( $workbook->worksheets) {

    print "Reading worksheet named: " . $worksheet->get_name . "\n";

    while( 1 ){
            my $cell = $worksheet->get_next_value;

            print "Cell is: $cell\n";
            last if $cell eq 'EOF';
     }
 }

output: 输出:

    # 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

According to the documentation, you can use the fetchrow_arrayref and fetchrow_array methods on the workbook level to get a full row. 根据文档,您可以在工作簿级别使用fetchrow_arrayreffetchrow_array方法获得完整的行。

fetchrow_arrayref( $row )

Definition: In an homage to DBI I included this function to return an array ref of the cells or values in the requested $row. 定义:为了向DBI致敬,我包括了此函数以返回所请求的$ row中单元格或值的数组ref。 If no row is requested this returns the 'next' row. 如果未请求任何行,则返回“下一个”行。 In the array ref any empty cell will show as 'undef'. 在数组ref中,任何空白单元格都将显示为“ undef”。

Accepts: undef = next|$row = a row integer indicating the desired row See the attribute "count_from_zero" in Spreadsheet::Reader::ExcelXML to understand which row is returned for $row. 接受:undef = next | $ row =指示所需行的行整数请参阅Spreadsheet :: Reader :: ExcelXML中的属性“ count_from_zero”以了解为$ row返回的行。

Returns: an array ref of all possible column positions in that row with data filled in per the attribute "group_return_type" in Spreadsheet::Reader::ExcelXML. 返回:该行中所有可能列位置的数组引用,并按照Spreadsheet :: Reader :: ExcelXML中的属性“ group_return_type”填充数据。

Note that this module has its own definition of what a row is, and you can change that on import. 请注意,此模块对行是什么有自己的定义,您可以在导入时进行更改。

To load a specific worksheet, use the worksheet method on the workbook object. 若要加载特定的工作表,请在worksheet簿对象上使用worksheet方法。 You need to know the name of the worksheet beforehand, or you can get all of the names with get_sheet_names . 您需要事先知道工作表的名称,或者可以使用get_sheet_names获得所有名称。

worksheet( $name )

Definition: This method will return an object to read values in the identified worksheet. 定义:此方法将返回一个对象,以读取标识的工作表中的值。 If no value is passed to $name then the 'next' worksheet in physical order is returned. 如果没有值传递给$ name,则返回物理顺序的“下一个”工作表。 'next' will NOT wrap It also only iterates through the 'worksheets' in the workbook (not the 'chartsheets'). “下一个”不会包装它也只会遍历工作簿中的“工作表”(而不是“图表”)。

Accepts: the $name string representing the name of the worksheet object you want to open. 接受:$ name字符串,表示要打开的工作表对象的名称。 This name is the word visible on the tab when opening the spreadsheet in Excel. 该名称是在Excel中打开电子表格时在选项卡上可见的单词。 (not the underlying zip member file name - which can be different. It will not accept chart tab names.) (不是基础的zip成员文件名-可以不同。它不接受图表选项卡的名称。)

Returns: a Worksheet object with the ability to read the worksheet of that name. 返回:一个能够读取该名称的工作表的工作表对象。 It returns undef and sets the error attribute if a 'chartsheet' is requested. 如果请求“图表表”,它将返回undef并设置错误属性。 Or in 'next' mode it returns undef if past the last sheet. 或者在“下一个”模式下,如果经过最后一张纸,则返回undef。

Example: using the implied 'next' worksheet; 示例:使用隐含的“下一个”工作表;

Here is a quick, untested example that will open a specific worksheet, get the data line by line and dump out the results. 这是一个未经测试的快速示例,它将打开一个特定的工作表,逐行获取数据并转储结果。

use strict;
use warnings;
use Spreadsheet::Reader::ExcelXML qw( :just_the_data);
use Data::Dumper;

my $workbook  = Spreadsheet::Reader::ExcelXML->new( 
    file => 'filename.xlsm' );

my $worksheet = $workbook->worksheet('foo');

while (my $row = $worksheet->fetchrow_array_ref) {
    print Dumper $row;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM