指定EXCEL文件的各种格式
返回
-
# 给B2:C4打黑色的框子
with ($t{sheet}->Range("B2:C4")->Borders(xlEdgeLeft),
Weight => xlMedium );
with ($t{sheet}->Range("B2:C4")->Borders(xlEdgeTop),
Weight => xlMedium );
with ($t{sheet}->Range("B2:C4")->Borders(xlEdgeBottom),
Weight => xlMedium );
with ($t{sheet}->Range("B2:C4")->Borders(xlEdgeRight),
Weight => xlMedium );
- 小数点零位表示(四舍五入)
$s = 10.2;
$s = int(($s+0.5));
$s1 = sprintf("%.0f",$s);
print "s1=$s1\n";
10.2==>10
10.5==>11
- 小数点2位表示(四舍五入)
$s = 10.234;
$s = int(($s*100+0.5))/100;
$s1 = sprintf("%.2f",$s);
print "s1=$s1\n";
10.234==>10.23
10.235==>10.24
- 指定CELL的其他特性
- $t{sheet}->Range("B2:C2")->{MergeCells} = "True";
$t{sheet}->Range("B2:C2")->{WrapText} = "True";
$t{sheet}->Range("B2:C2")->{Value} = "富士山是日本最高的山峰。";
$t{sheet}->Rows(2)->AutoFit;
## Excel での結合されたセルを含む行または列に AutoFit 機能は使用できません。
- 文字列の表示
$t{sheet}->Cells(4,4)->{NumberFormatLocal} = "@";
- 通貨の表示
$t{sheet}->Cells(4,4)->{Value} = "12345699";
$t{sheet}->Cells(4,4)->{NumberFormatLocal} = " #,##0";
#Range("A1").NumberFormatLocal = "$ #,##0"
小数点2点まで
$t{sheet}->Cells(4,4)->{Value} = "12345699.33";
$t{sheet}->Cells(4,4)->{NumberFormatLocal} = " #,##0.#0";
- セルサイズを文字の大きさに合わせる
$t{sheet}->Rows(3)->{AutoFit};
- B3的文字显示靠左,注意:HorizontalAlignment不能加引号!
with ($t{sheet}->Range("B3"),
HorizontalAlignment => xlLeft );
- 文字列の折り返し
$t{sheet}->Cells(3,2)->{WrapText} = "True";
$t{sheet}->Cells(3,2)->{Value} = "あいうえおかきくけこさしすせそ";
- 指定FONT大小和粗体等
$t{sheet}->Cells(3,2)->{Font}->{Size} = "9";
$t{sheet}->Cells(3,2)->{Font}->{Bold} = "False";
$t{sheet}->Cells(3,2)->{Value} = "1985";
- 指定FONT
$t{sheet}->Cells(2,2)->{Font}->{Name} = "MS 明朝";
$t{sheet}->Cells(2,2)->{Value} = "日本富士山";
- 汉字文件名
$t{excel} = "試験データ.xls";
$t{excel} = encode("utf8",$t{excel});
$t{excel} = decode("utf8",$t{excel});
没有encode时,会出现如下错误
Cannot decode string with wide characters at C:/Perl/lib/Encode.pm line 166.
- 显示EXCEL=>$t{ex}->{Visible} = 1;
- 覆盖时跳出的窗口不要=>$t{ex}->{DisplayAlerts} = 'False';
- 生成EXCEL程序
# excel_format.pl
#
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
use utf8;
# set perl's OLE module to return Unicode
Win32::OLE->Option(CP => Win32::OLE::CP_UTF8, Warn=>3);
use Encode;
my(%t);
# use existing instance if Excel is already running
eval {$t{ex} = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $t{ex}) {
$t{ex} = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
}
#$t{ex}->{Visible} = 1; # 显示EXCEL
$t{ex}->{DisplayAlerts} = 'False';
# get a new workbook
$t{book} = $t{ex}->Workbooks->Add;
# write to a particular cell
$t{sheet} = $t{book}->Worksheets(1);
$t{sheet}->Cells(2,2)->{Font}->{Name} = "MS 明朝";
$t{sheet}->Cells(2,2)->{Value} = "日本富士山";
$t{sheet}->Cells(3,2)->{Font}->{Size} = "9";
$t{sheet}->Cells(3,2)->{Font}->{Bold} = "False";
#$t{sheet}->Cells(3,2)->{WrapText} = "True";
$t{sheet}->Cells(3,2)->{Value} = "1983";
with ($t{sheet}->Range("B3"),
HorizontalAlignment => xlLeft );
# save and exit
$t{excel} = "試験データ.xls";
$t{excel} = encode("utf8",$t{excel});
$t{excel} = decode("utf8",$t{excel});
$t{book}->SaveAs($t{excel});
undef $t{book};
undef $t{ex};
返回