简体   繁体   English

Excel :: Writer :: XLSX无法读取的内容错误

[英]Excel::Writer::XLSX Unreadable content error

I just started using Perl and I am using Excel::Writer::XLSX to query a DB2 database and export the data to an .xlsx file. 我刚开始使用Perl,我使用Excel::Writer::XLSX查询DB2数据库并将数据导出到.xlsx文件。 The data is about 250k rows. 数据大约是250k行。

The script is running fine, but when I try to open the Excel file it throws an error and asks to repair the file. 脚本运行正常,但是当我尝试打开Excel文件时,它会抛出错误并要求修复该文件。 Upon repairing some of the data gets replaced by inf . 修复后,一些数据被inf取代。

错误对话框说“Excel在'Chk.xlsx'中找到了不可读的内容。。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击是。”

Below is a snippet from my code. 以下是我的代码中的代码段。

while ( my $sqlStatement = ) {

    $mSQL = $dbh->prepare( $sqlStatement )
            or die "Can't prepare $sqlStatement";
    $mSQL->execute()
            or die "Can't execute $sqlStatement";
}

my $workbook = Excel::Writer::XLSX->new( $ARGV[2] );
$workbook->set_tempdir( '/tempDir/' );
$workbook->set_optimization();

my $worksheet = $workbook->add_worksheet();
$worksheet->keep_leading_zeros();

my $row    = 0;
my $column = 0;

my @emptyRow = ();

$worksheet->write_row( $row++, $column, [ @{ $mSQL->{NAME_uc} } ] );
$worksheet->write_row( $row++, $column, [ @emptyRow ] );

while ( my @Row = $mSQL->fetchrow_array ) {
    $worksheet->write_row( $row++, $column, [ @Row ] );    #, $cellFormat);
    $count++;
}

$workbook->close();

Can someone please advise me on this issue? 有人可以就这个问题告诉我吗?

Finally i figured it out (Thanks to John McNamara). 最后我想出来了(感谢John McNamara)。 This was resolved by adding a write handler that uses regular expressions to check if a particular token is being converted to "inf", and if it does, it invokes the write_string subroutine instead of write_row. 这是通过添加一个写处理程序来解决的,该处理程序使用正则表达式来检查特定标记是否正在转换为“inf”,如果是,则调用write_string子例程而不是write_row。 Below is the code. 下面是代码。

#!/usr/bin/perl

use strict;
use warnings;
use Excel::Writer::XLSX;


my $workbook  = Excel::Writer::XLSX->new( 'write_handler5.xlsx' );
my $worksheet = $workbook->add_worksheet();


# Add a handler to match any numbers in order to check for and handle
# infinity.
$worksheet->add_write_handler( qr[\d], \&write_with_infinity );


# The following function is used by write() to pre-process any the data when a
# match is found. If it finds something that looks like a number but evaluates
# to infinity it write it as a string.
sub write_with_infinity {

    my $worksheet = shift;
    my @args      = @_;
    my $token     = $args[2];

    # Check if token looks like a number, in the same way as write().
    if ( $token =~ /^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/ ) {

    # Check for infinity.
    $token = $token + 0;

    if ($token =~ /inf/) {

        # Write the value as a string instead of a number.
        return $worksheet->write_string( @args );
    }
    }

    # Reject the match and return control to write()
    return undef;
}

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

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