免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2327 | 回复: 1
打印 上一主题 下一主题

Excel读取日期和时间格式的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-01-07 10:15 |只看该作者 |倒序浏览

使用Win32::OLE读取.xls文件

当单元格内容为:
2016-6-1 7:56


print $Sheet->Cells(1,1)->{'Value'};
内容为:
  1. Win32::OLE::Variant=SCALAR(0x2e5a568)
复制代码


请问,怎么转换格式?

日期和时间分别怎么读取?

谢谢~!

论坛徽章:
0
2 [报告]
发表于 2017-01-07 11:57 |只看该作者
自问自答

  1. #!/usr/bin/perl
  2. use strict;

  3. use Win32::OLE qw(in with);
  4. use Win32::OLE::Const 'Microsoft Excel';
  5. use Win32::OLE::Variant;
  6. use Win32::OLE::NLS qw(:LOCALE :DATE);

  7. $Win32::OLE::Warn = 3; # Die on Errors.


  8. # get already active Excel application or open new
  9. my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
  10.     || Win32::OLE->new('Excel.Application', 'Quit');  
  11. $Excel->{DisplayAlerts}=0;

  12. # open Excel file
  13. use Cwd;
  14. my $dir=getcwd;
  15. $dir=$dir."/test_excel.xls";

  16. #$dir = encode("euc-cn",$dir);
  17. my $Book = $Excel->Workbooks->Open($dir);

  18. my $Sheet = $Book->Worksheets(1);

  19. #找到最后一行 最后一列方法  
  20. my $LastRow = $Sheet->UsedRange->Find({What=>"*",  
  21.     SearchDirection=>xlPrevious,  
  22.     SearchOrder=>xlByRows})->{Row};  
  23.   
  24. my $LastCol = $Sheet->UsedRange->Find({What=>"*",  
  25.                   SearchDirection=>xlPrevious,  
  26.                   SearchOrder=>xlByColumns})->{Column};  


  27. # ::Warn = 2; throws the errors, but #
  28. # expects that the programmer deals  #

  29. my $dt = Variant(VT_DATE, $Sheet->Range("a8")->{Value});
  30. print "$dt\n";
  31. print $dt->Date(DATE_LONGDATE), "\n";

  32. $Sheet->Range("a2")->{Value} =   $dt->Date(DATE_LONGDATE);

  33. print $dt->Date("ddd',' MMM dd yy"), "\n";
  34. $Sheet->Range("a3")->{Value}= $dt->Date("ddd',' MMM dd yy");  

  35. #Some Properties Take Variant ints or longs -
  36. #in this case the Zoom property MUST be set for the
  37. #FitToPages(Wide|Tall) to be acknowledged.

  38. my $vtfalse =  Variant(VT_BOOL, 0);  
  39. my $vtpages =  Variant(VT_I4, 1);  
  40. with ($Sheet->PageSetup,
  41.     'FitToPagesWide'=>$vtpages,
  42.     'FitToPagesTall'=>$vtpages,
  43.     'Zoom'=>$vtfalse,     
  44.     'PrintGridlines'=>0,
  45.     'LeftHeader'=> "Using Perl and Excel",
  46.     'CenterHeader' => "My First Script",
  47.     'RightHeader' => "Test",   
  48.     'LeftFooter' => "Done",
  49.     'CenterFooter' => $dt->Date("ddd',' MMM dd yy"),
  50.     'Orientation' => xlLandscape,
  51.     'RightFooter' => "",
  52.     'PrintHeadings'=>0,
  53.     'FirstPageNumber'=> xlAutomatic,
  54.     'PrintTitleRows' => "1:1");  

  55.    #$Book->Save();
  56.    #$Book = $Excel->Workbooks->Close();
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP