打开excel文件时出错

时间:2018-06-07 11:45:06

标签: excel perl

我有一个excel文件附件并尝试发送电子邮件。我已成功发送电子邮件,但在打开附加的excel文件时收到错误,如下所示:

错误: -

"Excel cannot open the file "test.xlsx" because the file format or file extension is not valid. 
Verify that the file has not been corrupted and that the file extension matches the format of the file." 

代码: -

$WRKDIR="/tmp/xpyt/"
$EXCEL_FILE="test.xlsx"
$EMAIL_FROM="xxx@in.imk.com"
$EMAIL_TO="yyy@in.imk.com"
$EMAIL_SUBJECT="Excel Report"
$EMAIL_TYPE="multipart/mixed"


$msg = MIME::Lite->new(
    From    => $EMAIL_FROM,
    To      => $EMAIL_TO,
    Subject => $EMAIL_SUBJECT,
    Type    => $EMAIL_TYPE,
);

$msg->attach(
    Type     => 'TEXT',
    Data     => "Here's the excel file you wanted"
);

chmod 0755, $EXCEL_FILE_PATH;

$msg->attach(
    Type     => 'application/vnd.ms-excel',
    Path     => $WRKDIR,
    Filename => $EXCEL_FILE,
    Disposition => 'attachment'
);

$msg->send();

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

application/vnd.ms-excel适用于BIFF(.xls)个文件。

对于 Excel 2007 及以上(.xlsx)文件,请使用:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Source)功能

...所以,要么更改您的扩展程序或附件类型,要匹配。

答案 1 :(得分:0)

Path => $WRKDIR更改为Path => $WRKDIR.$EXCEL_FILE,然后重试

path是附件的完整路径,filename是邮件中显示的推荐文件名