- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有人有 Perl 命令列表,可以使用基本的 Win32::OLE 模块在 Windows 上操作 Excel?特别是,我正在考虑以下操作:
最佳答案
这是我在使用 Win32 模块在 Excel 上编写脚本时收集的操作列表。我愿意分享。
请随意发表评论/编辑以改进。
<强>1。打开和保存 Excel/工作簿
#Modules to use
use Cwd 'abs_path';
use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Const "Microsoft Excel";
$Win32::OLE::Warn = 3;
#Need to use absolute path for Excel files
my $excel_file = abs_path("$Excel_path") or die "Error: the file $Excel_path has not been found\n";
# Open Excel application
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
# Open Excel file
my $Book = $Excel->Workbooks->Open($excel_file);
#Make Excel visible
$Excel->{Visible} = 1;
#___ ADD NEW WORKBOOK
my $Book = $Excel->Workbooks->Add;
my $Sheet = $Book->Worksheets("Sheet1");
$Sheet->Activate;
#Save Excel file
$Excel->{DisplayAlerts}=0; # This turns off the "This file already exists" message.
$Book->Save; #Or $Book->SaveAs("C:\\file_name.xls");
$Book->Close; #or $Excel->Quit;
<强>2。工作表的操作
#Get the active Worksheet
my $Book = $Excel->Activewindow;
my $Sheet = $Book->Activesheet;
#List of Worksheet names
my @list_Sheet = map { $_->{'Name'} } (in $Book->{Worksheets});
#Access a given Worksheet
my $Sheet = $Book->Worksheets($list_Sheet[0]);
#Add new Worksheet
$Book->Worksheets->Add({After => $workbook->Worksheets($workbook->Worksheets->{Count})});
#Change Worksheet Name
$Sheet->{Name} = "Name of Worksheet";
#Freeze Pane
$Excel -> ActiveWindow -> {FreezePanes} = "True";
#Delete Sheet
$Sheet -> Delete;
<强>3。细胞操作
#Edit the value of a cell (2 methods)
$Sheet->Range("A1")->{Value} = 1234;
$Sheet->Cells(1,1)->{Value} = 1234;
#Edit the values in a range of cells
$Sheet->Range("A8:C9")->{Value} = [[ undef, 'Xyzzy', 'Plugh' ],
[ 42, 'Perl', 3.1415 ]];
#Edit the formula in a cell (2 types)
$Sheet->Range("A1")->{Formula} = "=A1*9.81";
$Sheet->Range("A3")->{FormulaR1C1} = "=SUM(R[-2]C:R[-1]C)"; # Sum of rows
$Sheet->Range("C1")->{FormulaR1C1} = "=SUM(RC[-2]:RC[-1])"; # Sum of columns
#Edit the format of the text (font)
$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;
#Edit the number format
$Sheet -> Range("G7:H7") -> {NumberFormat} = "\@"; # Text
$Sheet -> Range("A1:H7") -> {NumberFormat} = "\$#,##0.00"; # Currency
$Sheet -> Range("G7:H7") -> {NumberFormat} = "\$#,##0.00_);[Red](\$#,##0.00)"; # Currency - red negatives
$Sheet -> Range("G7:H7") -> {NumberFormat} = "0.00_);[Red](0.00)"; # Numbers with decimals
$Sheet -> Range("G7:H7") -> {NumberFormat} = "#,##0"; # Numbers with commas
$Sheet -> Range("G7:H7") -> {NumberFormat} = "#,##0_);[Red](#,##0)"; # Numbers with commas - red negatives
$Sheet -> Range("G7:H7") -> {NumberFormat} = "0.00%"; # Percents
$Sheet -> Range("G7:H7") -> {NumberFormat} = "m/d/yyyy"; # Dates
#Align text
$Sheet -> Range("G7:H7") -> {HorizontalAlignment} = xlHAlignCenter; # Center text;
$Sheet -> Range("A1:A2") -> {Orientation} = 90; # Rotate text
#Activate Cell
$Sheet -> Range("A2") -> Activate;
$Sheet->Hyperlinks->Add({
Anchor => $range, #Range of cells with the hyperlink; e.g. $Sheet->Range("A1")
Address => $adr, #File path, http address, etc.
TextToDisplay => $txt, #Text in the cell
ScreenTip => $tip, #Tip while hovering the mouse over the hyperlink
});
注意:要检索超链接列表,请查看以下帖子 Getting list of hyperlinks from an Excel worksheet with Perl Win32::OLE
<强>4。行/列的操作
#Insert a row before/after line 22
$Sheet->Rows("22:22")->Insert(xlUp, xlFormatFromRightOrBelow);
$Sheet->Rows("23:23")->Insert(-4121,0); #xlDown is -4121 and that xlFormatFromLeftOrAbove is 0
#Delete a row
$Sheet->Rows("22:22")->Delete();
#Set column width and row height
$Sheet -> Range('A:A') -> {ColumnWidth} = 9.14;
$Sheet -> Range("8:8") -> {RowHeight} = 30;
$Sheet -> Range("G:H") -> {Columns} -> Autofit;
# Get the last row/column
my $last_row = $Sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
my $last_col = $Sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByColumns}) -> {Column};
#Add borders (method 1)
$Sheet -> Range("A3:H3") -> Borders(xlEdgeBottom) -> {LineStyle} = xlDouble;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeBottom) -> {Weight} = xlThick;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeBottom) -> {ColorIndex} = 1;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeLeft) -> {LineStyle} = xlContinuous;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeLeft) -> {Weight} = xlThin;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeTop) -> {LineStyle} = xlContinuous;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeTop) -> {Weight} = xlThin;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeBottom) -> {LineStyle} = xlContinuous;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeBottom) -> {Weight} = xlThin;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeRight) -> {LineStyle} = xlContinuous;
$Sheet -> Range("A3:H3") -> Borders(xlEdgeRight) -> {Weight} = xlThin;
$Sheet -> Range("A3:H3") -> Borders(xlInsideVertical) -> {LineStyle} = xlDashDot
$Sheet -> Range("A3:H3") -> Borders(xlInsideVertical) -> {Weight} = xlMedium;
$Sheet -> Range("A3:I3") -> Borders(xlInsideHorizontal) -> {LineStyle} = xlContinuous;
$Sheet -> Range("A3:I3") -> Borders(xlInsideHorizontal) -> {Weight} = xlThin;
#Add borders (method 2)
my @edges = qw (xlInsideHorizontal xlInsideVertical xlEdgeBottom xlEdgeTop xlEdgeRight);
foreach my $edge (@edges)
{
with (my $Borders = $Sheet->Range("A3:H3")->Borders(eval($edge)),
LineStyle =>xlContinuous, Weight => xlThin , ColorIndex => 1);
}
#Merge Cells
$Sheet -> Range("H10:J10") -> Merge;
#Group Rows
$Sheet -> Range("7:8") -> Group;
#Hide Columns
$Sheet -> Range("G:H") -> EntireColumn -> {Hidden} = "True";
<强>5。其他
#Add a name (i.e. reference)
$Book->Names->Add($name =>"=$ref"); #Where $name points to the range $ref
#Insert a picture
$Sheet -> Pictures -> Insert("picture_name"); # Insert in upper-left corner
$Excel -> ActiveSheet -> Pictures -> Insert("picture_name"); # Insert in active cell
#Print setup
$Sheet -> PageSetup -> {Orientation} = xlLandscape;
$Sheet -> PageSetup -> {Order} = xlOverThenDown;
$Sheet -> PageSetup -> {LeftMargin} = .25;
$Sheet -> PageSetup -> {RightMargin} = .25;
$Sheet -> PageSetup -> {BottomMargin} = .5;
$Sheet -> PageSetup -> {CenterFooter} = "Page &P of &N";
$Sheet -> PageSetup -> {RightFooter} = "Page &P of &N";
$Sheet -> PageSetup -> {LeftFooter} = "Left\nFooter";
$Sheet -> PageSetup -> {Zoom} = 75;
$Sheet -> PageSetup -> FitToPagesWide = 1;
$Sheet -> PageSetup -> FitToPagesTall = 1;
#Add a Page break
$Excel -> ActiveWindow -> SelectedSheets -> HPageBreaks -> Add({Before => $Sheet -> Range("3:3")});
<强>6。有用的链接
有关 Excel 颜色信息的链接: http://dmcritchie.mvps.org/excel/colors.htm
有关 Excel 常量信息的链接: http://msdn.microsoft.com/en-us/library/aa221100%28office.11%29.aspx
来自 Win32::OLE 模块的链接:http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE.pm#EXAMPLES
有关 Excel 使用的有用信息,请访问 this address
关于excel - 使用 Win32::OLE 模块操作 Windows Excel 的 Perl 命令列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613878/
我最近在我的机器上安装了 cx_Oracle 模块,以便连接到远程 Oracle 数据库服务器。 (我身边没有 Oracle 客户端)。 Python:版本 2.7 x86 Oracle:版本 11.
我想从 python timeit 模块检查打印以下内容需要多少时间,如何打印, import timeit x = [x for x in range(10000)] timeit.timeit("
我盯着 vs 代码编辑器上的 java 脚本编码,当我尝试将外部模块包含到我的项目中时,代码编辑器提出了这样的建议 -->(文件是 CommonJS 模块;它可能会转换为 ES6 模块。 )..有什么
我有一个 Node 应用程序,我想在标准 ES6 模块格式中使用(即 "type": "module" in the package.json ,并始终使用 import 和 export)而不转译为
我正在学习将 BlueprintJS 合并到我的 React 网络应用程序中,并且在加载某些 CSS 模块时遇到了很多麻烦。 我已经安装了 npm install @blueprintjs/core和
我需要重构一堆具有这样的调用的文件 define(['module1','module2','module3' etc...], function(a, b, c etc...) { //bun
我是 Angular 的新手,正在学习各种教程(Codecademy、thinkster.io 等),并且已经看到了声明应用程序容器的两种方法。首先: var app = angular.module
我正在尝试将 OUnit 与 OCaml 一起使用。 单元代码源码(unit.ml)如下: open OUnit let empty_list = [] let list_a = [1;2;3] le
我在 Angular 1.x 应用程序中使用 webpack 和 ES6 模块。在我设置的 webpack.config 中: resolve: { alias: { 'angular':
internal/modules/cjs/loader.js:750 return process.dlopen(module, path.toNamespacedPath(filename));
在本教程中,您将借助示例了解 JavaScript 中的模块。 随着我们的程序变得越来越大,它可能包含许多行代码。您可以使用模块根据功能将代码分隔在单独的文件中,而不是将所有内容都放在一个文件
我想知道是否可以将此代码更改为仅调用 MyModule.RED 而不是 MyModule.COLORS.RED。我尝试将 mod 设置为变量来存储颜色,但似乎不起作用。难道是我方法不对? (funct
我有以下代码。它是一个 JavaScript 模块。 (function() { // Object var Cahootsy; Cahootsy = { hello:
关闭。这个问题是 opinion-based 。它目前不接受答案。 想要改进这个问题?更新问题,以便 editing this post 可以用事实和引文来回答它。 关闭 2 年前。 Improve
从用户的角度来看,一个模块能够通过 require 加载并返回一个 table,模块导出的接口都被定义在此 table 中(此 table 被作为一个 namespace)。所有的标准库都是模块。标
Ruby的模块非常类似类,除了: 模块不可以有实体 模块不可以有子类 模块由module...end定义. 实际上...模块的'模块类'是'类的类'这个类的父类.搞懂了吗?不懂?让我们继续看
我有一个脚本,它从 CLI 获取 3 个输入变量并将其分别插入到 3 个变量: GetOptions("old_path=s" => \$old_path, "var=s" =
我有一个简单的 python 包,其目录结构如下: wibble | |-----foo | |----ping.py | |-----bar | |----pong.py 简单的
这种语法会非常有用——这不起作用有什么原因吗?谢谢! module Foo = { let bar: string = "bar" }; let bar = Foo.bar; /* works *
我想运行一个命令: - name: install pip shell: "python {"changed": true, "cmd": "python <(curl https://boot
我是一名优秀的程序员,十分优秀!