亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 求助一段win32::ole寫入excel的代碼,工作需要 [打印本頁]

作者: feiying00917    時間: 2013-08-25 23:14
標(biāo)題: 求助一段win32::ole寫入excel的代碼,工作需要
現(xiàn)在要往一個已經(jīng)存在并有數(shù)據(jù)的excel表格中寫入數(shù)據(jù),因為表格已經(jīng)有數(shù)據(jù),所以需要在這些數(shù)據(jù)之后插入數(shù)據(jù),能不能用win32:le寫段代碼,工作需要,求大俠幫忙!
比如 %work={'name'=>'jack',
                     'project'=>'venus',
                     'pid'=>'R0629"
                     'add'=>'shanghai'}
需要插入到一個excel.xls文件中,使用win32:le,需要知道excel已經(jīng)存在數(shù)據(jù)的行截至數(shù),然后緊接著下一行插入%work中的值(鍵不需要存入,依次按列插入哈希值,每個cell存入一個值即可),求一段完整代碼,謝謝!
作者: stanley_tam    時間: 2013-08-26 14:13
偶試試{:3_193:}
  1. #!perl
  2. package Xls;

  3. use strict;
  4. use Win32::OLE;

  5. sub new {
  6.     my ($class) = @_;
  7.     my $self = +{};
  8.     bless $self, $class;
  9.     return $self;
  10. }

  11. sub open_excel {
  12.     my $self   = shift;
  13.     my $xl_doc = shift;
  14.     my $excel = Win32::OLE->new('Excel.Application') or
  15.         die "Excel application is not installed:\n";
  16.     my $workbooks;
  17.    
  18.     # make excel visible
  19.     $excel->{Visible} = 1;

  20.     $workbooks = $excel->Workbooks->Open($xl_doc);
  21.     return $workbooks;
  22. }

  23. sub get_worksheet {
  24.     my ($self, $workbooks, $num) = @_;
  25.     my $sheet = $workbooks->Worksheets($num);
  26.     return $sheet;
  27. }

  28. sub get_cell_value {
  29.     my ($self, $sheet, $row, $col) = @_;
  30.     my $cell_obj = $sheet->Cells($row, $col);
  31.     my $value = q{};
  32.    
  33.     if ($cell_obj){
  34.         $value = $sheet->Cells($row, $col)->{Value};
  35.     }
  36.     $value ||= q{};
  37.    
  38.     return $value;
  39. }

  40. sub set_cell_value {
  41.     my ($self, $sheet, $row, $col, $value) = @_;
  42.     my $cell_obj = $sheet->Cells($row, $col);
  43.     $value ||= q{};
  44.    
  45.     if ($cell_obj){
  46.         $sheet->Cells($row, $col)->{Value} = $value;
  47.     }
  48. }

  49. 1;

  50. package main;
  51. use strict;

  52. my $xls_name = '/full/path/to/your/excel.xls';

  53. # parse the first worksheet
  54. my $worksheet_number = 1;

  55. # data to write
  56. my %work = (
  57.     name   =>'jack',
  58.     project=>'venus',
  59.     pid    =>'R0629',
  60.     add    =>'shanghai',
  61. );

  62. my $pm = Xls->new();
  63. my $workbooks = $pm->open_excel($xls_name);
  64. my $worksheet = $pm->get_worksheet($workbooks, $worksheet_number);

  65. # frist cell
  66. my $col = 1;
  67. my $row = 1;

  68. # get the cell in column one that has no value within
  69. while (1) {
  70.     my $cell_value = $pm->get_cell_value($worksheet, $row, $col);
  71.     last if !$cell_value;
  72.     $row++;
  73. }

  74. # write data in ordered sequences
  75. for my $key ('name', 'project', 'pid', 'add'){
  76.     $pm->set_cell_value($worksheet, $row, $col, $work{$key});
  77.     $col++;
  78. }

  79. # close
  80. $workbooks->Close
  81. __END__
復(fù)制代碼

作者: wenbinsan    時間: 2013-09-05 11:07
回復(fù) 2# stanley_tam

學(xué)習(xí)了,bless


   




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2