前言
更多内容,请访问我的 个人博客。
- 优点
无需安装模块依赖,功能强大 - 缺点
无法实现跨平台,必须依赖win系统和office软件
如果想实现跨平台处理excel请移步 perl处理Excel(跨平台)
- use FindBin qw($Bin);
-
- use Win32::OLE qw(in with);
- use Win32::OLE::Const 'Microsoft Excel';
-
- my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
- #不显示警告窗口
- $Excel->{DisplayAlerts} = 0;
-
- my $Book = $Excel->Workbooks->Open($file);
- my $Sheet = $Book->Worksheets(1);
- #my $Sheet = $Book->Worksheets($sheetName);
- #my $SheetName = $Book->Worksheets(1)->{Name};
-
- my $maxRow = $Sheet->UsedRange->Rows->Count;
- my $maxCol = $Sheet->UsedRange->Columns->Count;
-
- foreach my $row(1..$maxRow){
- foreach my $col(1..$maxCol){
- #隐藏的单元格,过滤
- next if $Sheet->Range("$row:$row")->EntireRow->{Hidden};
-
- #获取单元格的值
- my $value = $Sheet->Cells($row,$col)->{Value};
-
- #修改单元格填充色
- $Sheet->Cells($row,$col)->Interior->{Color} = $InColor;
-
- #字体加粗
- $sheet -> Range("G7:H7") -> Font -> {Bold} = "True";
-
- #字体斜体
- $sheet -> Range("G7:H7") -> Font -> {Italic} = "True";
-
- #字体下划线
- $sheet -> Range("G7:H7") -> Font -> {Underline} = xlUnderlineStyleSingle;
-
- #字体大小
- $sheet -> Range("G7:H7") -> Font -> {Size} = 8;
-
- #字体名称
- $sheet -> Range("G7:H7") -> Font -> {Name} = "Arial";
-
- #字体颜色
- $sheet -> Range("G7:H7") -> Font -> {ColorIndex} = 4;
-
- #列宽
- $sheet -> Range('A:A') -> {ColumnWidth} = 9.14;
-
- #行高
- $sheet -> Range("8:8") -> {RowHeight} = 30;
- }
- }
-
- $Book->Save();
- $Book->Close();
- $Excel->Quit();
- 复制代码