指定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 );
- 小數(shù)點零位表示(四舍五入)
$s = 10.2;
$s = int(($s+0.5));
$s1 = sprintf("%.0f",$s);
print "s1=$s1\n";
10.2==>10
10.5==>11
- 小數(shù)點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 での結(jié)合されたセルを含む行または列に 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"
小數(shù)點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時,會出現(xiàn)如下錯誤
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};
返回
邯郸市|
临洮县|
洛扎县|
颍上县|
丰顺县|
平泉县|
威海市|
安西县|
香港|
昌江|
尤溪县|
江北区|
环江|
依安县|
罗定市|
仲巴县|
客服|
宁安市|
文安县|
四平市|
卢氏县|
邵东县|
许昌市|
资兴市|
东丰县|
萨迦县|
龙口市|
高淳县|
基隆市|
永善县|
吴川市|
河间市|
双牌县|
延长县|
乌拉特中旗|
公主岭市|
福贡县|
怀远县|
武隆县|
友谊县|
渝中区|