Perl中数据类型的问题

今天修改PERL脚本读取EXCEL中的数据时,发现一个问题。有一列存放的是日期格式的数据。使用PERL取出来的VALUE使用print打印出来都是:Win32::OLE::Variant=SCALAR(XXXXX)。

 

网上查找一番。问题很快解决。

 

需要关注一下EXCEL中各种变量类型的转化。下面列出来一些常见的数据类型:

From: http://paginas.fe.up.pt/~jvv/PERL/manual/site/lib/Win32/OLE/Variant.html#variants

A Variant is a data type that is used to pass data between OLE connections.

The default behavior is to convert each perl scalar variable into an OLE Variant according to the internal perl representation. The following type correspondence holds:

        C type          Perl type       OLE type
        ------          ---------       --------
          int              IV            VT_I4
        double             NV            VT_R8
        char *             PV            VT_BSTR
        void *           ref to AV       VT_ARRAY
           ?              undef          VT_ERROR
           ?        Win32::OLE object    VT_DISPATCH

Note that VT_BSTR is a wide character or Unicode string. This presents a problem if you want to pass in binary data as a parameter as 0x00 is inserted between all the bytes in your data. The Variant() method provides a solution to this. With Variants the script writer can specify the OLE variant type that the parameter should be converted to. Currently supported types are:

        VT_UI1     unsigned char
        VT_I2      signed int (2 bytes)
        VT_I4      signed int (4 bytes)
        VT_R4      float      (4 bytes)
        VT_R8      float      (8 bytes)
        VT_DATE    OLE Date
        VT_BSTR    OLE String
        VT_CY      OLE Currency
        VT_BOOL    OLE Boolean

 

根据这个,很快就能找到各种类型的映射。

例如:

我要读取的列为日期类型,则需要在脚本的最前面添加:

 

use Win32::OLE::Variant;

 再使用:

Variant(VT_DATE, $sheet->Range("O$row")->{'Value'});

 访问即可。

 

有兴趣的朋友可以来这里看看:http://www.perlmonks.org/index.pl?node_id=153486

 

如果本博文对您有用,请支持一下吧~~

 

你可能感兴趣的:(perl)