perl处理Excel(跨平台)

本文最后更新于:2023年4月25日 下午

前言

用 perl 处理 excel 有两种流行的模块:一种是 win32:OLE 模块,优点是功能强大,在 excel 上能做的事情用这个模块都能做,缺点是无法跨平台,必须在 win 系统上使用且必须安装 office 软件;另一种是 Spreadsheet 模块,优点是跨平台,不依赖系统和软件,缺点是功能不是很多,但基本功能都能实现。

如果在 win 平台处理 excel,建议使用 win32:OLE 模块(教程传送门),本文主要讲解 Spreadsheet 模块的使用方法。

了解

Spreadsheet::ParseExcelSpreadsheet::WriteExcel 的官方资料中我们可以知道:

Spreadsheet::ParseExcel
  • 是用于提取 excel 信息
  • 仅支持 Excel 95-2003 格式
Spreadsheet::WriteExcel
  • 是用于新建 excel 信息
  • 仅支持 Excel 97-2007 格式

也就是说,如果想更好的支持 xlsx 格式的 excel 需要用到 Spreadsheet::ParseXLSXExcel::Writer::XLSX 模块。

另外, Spreadsheet::ParseExcel::SaveParser 可以支持在已存在的 xls 表中修改数据,但是支持效果远没有 win32:OLE 模块强大,修改之后的表格会丢失部分不支持的宏命令和表格等,最遗憾的是仅支持 xls ,并不支持 xlsx

安装模块

在终端中输入 sudo perl -MCPAN -e "install 'Spreadsheet::ParseExcel'" ,回车,如下图:

image

若出现如下图,则安装成功:

image

使用方法 - 提取 Excel

引用 use Spreadsheet::ParseExcel; 类,如下:

1
use Spreadsheet::ParseExcel;

Parser

new()

new()方法可以新建一个提取类,如下:

1
my $parser = Spreadsheet::ParseExcel->new(); #新建提取类

若是 2007 版以上的 excel,则需要用到 Spreadsheet::ParseXLSX ,如下:

1
2
use Spreadsheet::ParseXLSX; #引用
my $parser = Spreadsheet::ParseXLSX->new(); #新建xlsx提取类

若 excel 需要密码才能打开,则新建密码提取类,如下:

1
$parser = Spreadsheet::ParseExcel->new( Password => 'secret' ); #若excel需要密码才能打开,则新建密码提取类
parse($filename)

获取 excel 表的工作薄,若无则返回 under

1
my $workbook = $parser->parse('Book1.xls'); #获取excel表的工作薄,若无则返回 `under`
error()

判断是否存在该 excel 表格

1
2
3
4
5
6
7
8
9
10
# error_code() => error()
# ============ =======
# 0 => ''
# 1 => '文件没有找到'
# 2 => '该文件不是标准的excel格式文档'
# 3 => '文件加密了'

if ( !defined $workbook ) { #一般需要判断是否存在该excel表格
die $parser->error_code(), $parser->error(), ".\n";
}

Workbook

Spreadsheet::ParseExcel::Workbook 是由 Spreadsheet::ParseExcelparse() 方法获得,有如下几种常用方法(完整方法使用传送门):

1
2
3
4
$workbook->worksheets()
$workbook->worksheet()
$workbook->worksheet_count()
$workbook->get_filename()
worksheets()

循环获取所有的 sheet 表,如下:

1
2
3
4
for my $worksheet ( $workbook->worksheets() ) {
#循环获取所有的sheet表
...
}
worksheet()

通过 sheet 表名或表索引获取单一 sheet 表,若无则返回 under ,如下:

1
2
$worksheet = $workbook->worksheet('Sheet1'); #获取名为"Sheet1"的sheet表
$worksheet = $workbook->worksheet(0); #获取第一张sheet表
worksheet_count()

获取 sheet 表的个数,如下:

1
my $worksheet_count = $workbook->worksheet_count(); #获取sheet表的个数
get_filename()

获取 excel 表的全路径+名称,如下:

1
my $filename = $workbook->get_filename(); #获取excel表的全路径+名称

Worksheet

Spreadsheet::ParseExcel::Worksheet 是由 worksheets()worksheet() 方法获得,有如下几种常用方法(完整方法使用传送门):

1
2
3
4
$worksheet->get_cell()
$worksheet->row_range()
$worksheet->col_range()
$worksheet->get_name()
get_cell($row, $col)

由指定行和列获取单元格,若不存在则返回 under ,如下:

1
my $cell = $worksheet->get_cell($row, $col); #由指定行和列获取单元格
row_range()

返回一个二元列表 ($min, $max) ,表示行数的最小值和最大值,如下图:

1
my ( $row_min, $row_max ) = $worksheet->row_range(); #行数的最小值和最大值
col_range()

返回一个二元列表 ($min, $max) ,表示列数的最小值和最大值,如下图:

1
my ( $col_min, $col_max ) = $worksheet->col_range(); #列数的最小值和最大值
get_name()

获取 sheet 的名称,如下图:

1
my $name = $worksheet->get_name(); #获取sheet的名称

Cell

Spreadsheet::ParseExcel::Cell 有如下几种常用方法(完整方法使用传送门):

1
2
$cell->value()
$cell->unformatted()
value()

获取单元格的值(有格式)。例如,在单元格中输入数字 123 ,单元格显示为 123.00 , 方法value() 的返回值为 123.00 。如下:

1
my $value = $cell->value(); #获取单元格的值(有格式)
unformatted()

获取单元格的值(无格式)。例如,在单元格中输入数字 123 ,单元格显示为 123.00 ,方法 unformatted() 的返回值为 123 。如下:

1
my $value = $cell->unformatted(); #获取单元格的值(有格式)

高级方法 - 提取 Excel

Format

获取 单元格格式 ,如下:

1
my $format = $cell->get_format(); #获取单元格格式

Spreadsheet::ParseExcel::Format 类有如下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$format->{Font}
$format->{AlignH}
$format->{AlignV}
$format->{Indent}
$format->{Wrap}
$format->{Shrink}
$format->{Rotate}
$format->{JustLast}
$format->{ReadDir}
$format->{BdrStyle}
$format->{BdrColor}
$format->{BdrDiag}
$format->{Fill}
$format->{Lock}
$format->{Hidden}
$format->{Style}
$format->{Font}

获取单元格的 字体 ,如下:

1
my $font = $format->{Font}
$format->{AlignH}

获取单元格的 水平对齐方式 ,返回值如下:

1
2
3
4
5
6
7
8
0 => 常规
1 => 靠左
2 => 居中
3 => 靠右
4 => 填充
5 => 两端对齐
6 => 跨列居中
7 => 分散对齐
$format->{Indent}

获取单元格的 水平对齐方式靠左 的缩进值

$format->{AlignV}

获取单元格的 垂直对齐方式 ,返回值如下:

1
2
3
4
5
0 => 靠上
1 => 居中
2 => 靠下
3 => 两端对齐
4 => 分散对齐
$format->{Wrap}

是否选择了 文本控制 中的 自动换行

$format->{Shrink}

是否选择了 文本控制 中的 缩小字体填充

$format->{Rotate}

获取 字体旋转方向 ,如下:

1
2
3
4
0 => 无
1 => 垂直
2 => 逆时针90
3 => 顺时针90
$format->{JustLast}

是否选择了 两端分散对齐

$format->{ReadDir}

获取 文本阅读方向

$format->{BdrStyle}

获取 边框线型 ,如下数组:

1
[ $left, $right, $top, $bottom ]
$format->{BdrColor}

获取 边框颜色 ,如下数组:

1
[ $left, $right, $top, $bottom ]
$format->{BdrDiag}

获取 对角线边框种类、线型、颜色 ,如下数组:

1
[$kind, $style, $color ]

边框种类如下:

1
2
3
4
0 => 无
1 => 右上对角线
2 => 右下对角线
3 => 全部
$format->{Fill}

获取 填充图案样式图案颜色背景色 ,如下:

1
[ $pattern, $front_color, $back_color ]
$format->{Lock}

获取是否 锁定 单元格

$format->{Hidden}

获取是否 隐藏 公式

Font

获取单元格的 字体 ,如下:

1
my $font = $format->{Font}

Spreadsheet::ParseExcel::Font 类有如下方法:

1
2
3
4
5
6
7
8
9
$font->{Name}
$font->{Bold}
$font->{Italic}
$font->{Height}
$font->{Underline}
$font->{UnderlineStyle}
$font->{Color}
$font->{Strikeout}
$font->{Super}
$font->{Name}

获取 字体名称 ,如 Arial

$font->{Bold}

获取字体是否 加粗

$font->{Italic}

获取字体是否 斜体

$font->{Height}

获取字体 大小

$font->{Underline}

获取字体是否开启 下划线

$font->{UnderlineStyle}

获取字体 下划线样式 ,如下:

1
2
3
4
5
 0 => 无
1 => 下划线
2 => 双下划线
33 => Single accounting
34 => Double accounting
$font->{Color}

获取 字体颜色

$font->{Strikeout}

获取字体是否添加 删除线

$font->{Super}

获取字体是 上标 还是 下标 ,如下:

1
2
3
0 => 无
1 => 上标
2 => 下标

使用方法 - 新建 Excel

引用 use Spreadsheet::WriteExcel; 类,如下:

1
use Spreadsheet::WriteExcel;

workbook

new()

new()方法可以基于文件名新建一个 excel 表,如下:

1
my $workbook  = Spreadsheet::WriteExcel->new('filename.xls'); #新建xls表

若是新建 2007 版以上的 excel,则需要用到 Excel::Writer::XLSX ,如下:

1
2
use Excel::Writer::XLSX; #引用
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); #新建xlsx表

若创建失败,则返回 under,如下:

1
die "Problems creating new Excel file: $!" unless defined $workbook;
compatibility_mode()

用于增强兼容性,如下:

1
$workbook->compatibility_mode(); #用于增强兼容性
add_worksheet($sheetname)

新增一个 sheet,如下:

1
2
3
$worksheet1 = $workbook->add_worksheet();           # Sheet1
$worksheet2 = $workbook->add_worksheet('自定义名称'); # 自定义名称
$worksheet3 = $workbook->add_worksheet(); # Sheet3
add_chart(%properties)

更多详细请移步 Spreadsheet::WriteExcel::Chart

close()

一般情况下,excel 文件会自动关闭,但使用 close() 可以确定关闭 excel 文件,如下:

1
$workbook->close() or die "Error closing file: $!"; #关闭excel文件
set_properties()

设置 excel 表附加信息,如下:

1
2
3
4
5
6
#设置excel表附加信息
$workbook->set_properties(
title => 'This is an example spreadsheet',
author => 'John McNamara',
comments => 'Created with Perl and Spreadsheet::WriteExcel',
);

可设置的信息如下:

1
2
3
4
5
6
7
8
title
subject
author
manager
company
category
keywords
comments

worksheet

write($row, $column, $token, $format)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$worksheet->write(0, 0, 'Hello'                ); # write_string()
$worksheet->write(1, 0, 'One' ); # write_string()
$worksheet->write(2, 0, 2 ); # write_number()
$worksheet->write(3, 0, 3.00001 ); # write_number()
$worksheet->write(4, 0, "" ); # write_blank()
$worksheet->write(5, 0, '' ); # write_blank()
$worksheet->write(6, 0, undef ); # write_blank()
$worksheet->write(7, 0 ); # write_blank()
$worksheet->write(8, 0, 'http://www.perl.com/'); # write_url()
$worksheet->write('A9', 'ftp://ftp.cpan.org/' ); # write_url()
$worksheet->write('A10', 'internal:Sheet1!A1' ); # write_url()
$worksheet->write('A11', 'external:c:\foo.xls' ); # write_url()
$worksheet->write('A12', '=A3 + 3*A4' ); # write_formula()
$worksheet->write('A13', '=SIN(PI()/4)' ); # write_formula()
$worksheet->write('A14', \@array ); # write_row()
$worksheet->write('A15', [\@array] ); # write_col()

$format 参数是可选的,用于定义单元格的格式,如下:

1
2
3
4
5
6
my $format = $workbook->add_format();
$format->set_bold(); #加粗
$format->set_color('red'); #字体红色
$format->set_align('center'); #居中

$worksheet->write(4, 0, 'Hello', $format);

单元格坐标的确认使用格式如下:

1
2
(0, 0)
('A1')

互换使用方法如下:

1
2
3
4
use Spreadsheet::WriteExcel::Utility;

($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
$str = xl_rowcol_to_cell(1, 2); # C2
keep_leading_zeros()

excel 会将类似于数字的内容转换成数字,例如输入 0123 ,excel 会将它转化为 123 ,如果想呈现出 0123 ,则做如下处理:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 如下,结果是 1209
$worksheet->write('A1', '01209');

# 设置格式,结果是 01209
my $format1 = $workbook->add_format(num_format => '00000');
$worksheet->write('A2', '01209', $format1);

# 使用字符串格式,结果是 01209
$worksheet->write_string('A3', '01209');

# 使用keep_leading_zeros()方法,结果是 01209
$worksheet->keep_leading_zeros();
$worksheet->write('A4', '01209');

以上操作呈现出的效果如下:

1
2
3
4
5
6
7
 -----------------------------------------------------------
| | A | B | C | D | ...
-----------------------------------------------------------
| 1 | 1209 | | | | ...
| 2 | 01209 | | | | ...
| 3 | 01209 | | | | ...
| 4 | 01209 | | | | ...

write_row($row, $column, $array_ref, $format) 写入数组数据,如下:

1
2
3
4
5
6
7
8
9
@array      = ('awk', 'gawk', 'mawk');
$array_ref = \@array;

$worksheet->write_row(0, 0, $array_ref);

# 以上代码等价于
$worksheet->write(0, 0, $array[0]);
$worksheet->write(0, 1, $array[1]);
$worksheet->write(0, 2, $array[2]);

若是二维数组,如下:

1
2
3
4
5
6
7
@eec =  (
['maggie', 'milly', 'molly', 'may' ],
[13, 14, 15, 16 ],
['shell', 'star', 'crab', 'stone']
);

$worksheet->write_row('A1', \@eec);

呈现出的效果,如下:

1
2
3
4
5
6
7
8
9
 -----------------------------------------------------------
| | A | B | C | D | E | ...
-----------------------------------------------------------
| 1 | maggie | 13 | shell | ... | ... | ...
| 2 | milly | 14 | star | ... | ... | ...
| 3 | molly | 15 | crab | ... | ... | ...
| 4 | may | 16 | stone | ... | ... | ...
| 5 | ... | ... | ... | ... | ... | ...
| 6 | ... | ... | ... | ... | ... | ...

write_col($row, $column, $array_ref, $format) 写入数组数据,如下:

1
2
3
4
5
6
7
8
9
@array      = ('awk', 'gawk', 'mawk');
$array_ref = \@array;

$worksheet->write_col(0, 0, $array_ref);

# 以上代码等价于
$worksheet->write(0, 0, $array[0]);
$worksheet->write(1, 0, $array[1]);
$worksheet->write(2, 0, $array[2]);

若是二维数组,如下:

1
2
3
4
5
6
7
@eec =  (
['maggie', 'milly', 'molly', 'may' ],
[13, 14, 15, 16 ],
['shell', 'star', 'crab', 'stone']
);

$worksheet->write_col('A1', \@eec);

呈现出的效果,如下:

1
2
3
4
5
6
7
8
9
  -----------------------------------------------------------
| | A | B | C | D | E | ...
-----------------------------------------------------------
| 1 | maggie | milly | molly | may | ... | ...
| 2 | 13 | 14 | 15 | 16 | ... | ...
| 3 | shell | star | crab | stone | ... | ...
| 4 | ... | ... | ... | ... | ... | ...
| 5 | ... | ... | ... | ... | ... | ...
| 6 | ... | ... | ... | ... | ... | ...
write_url($row, $col, $url, $label, $format)

写入超链接,如下:

1
2
3
4
5
$worksheet->write_url(0, 0,  'ftp://www.perl.org/'                  );
$worksheet->write_url(1, 0, 'http://www.perl.com/', 'Perl home' );
$worksheet->write_url('A3', 'http://www.perl.com/', $format );
$worksheet->write_url('A4', 'http://www.perl.com/', 'Perl', $format);
$worksheet->write_url('A5', 'mailto:jmcnamara@cpan.org' );
write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)

在一块区域内写入超链接,如下:

1
2
3
4
5
$worksheet->write_url(0, 0, 0, 3, 'ftp://www.perl.org/'              );
$worksheet->write_url(1, 0, 0, 3, 'http://www.perl.com/', 'Perl home');
$worksheet->write_url('A3:D3', 'internal:Sheet2!A1' );
$worksheet->write_url('A4:D4', 'external:c:\temp\foo.xls' );

write_formula($row, $column, $formula, $format, $value)

写入公式,如下:

1
2
3
4
5
6
$worksheet->write_formula(0, 0, '=$B$3 + B4'  );
$worksheet->write_formula(1, 0, '=SIN(PI()/4)');
$worksheet->write_formula(2, 0, '=SUM(B1:B5)' );
$worksheet->write_formula('A4', '=IF(A3>1,"Yes", "No")' );
$worksheet->write_formula('A5', '=AVERAGE(1, 2, 3, 4)' );
$worksheet->write_formula('A6', '=DATEVALUE("1-Jan-2001")');
write_comment($row, $column, $string, …)

增加批注,如下:

1
2
$worksheet->write        (2, 2, 'Hello');
$worksheet->write_comment(2, 2, '这是个批注');

还可以增加信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# encoding => 若为1,则表明使用UTF-16BE编码
# author => 批注的作者
# author_encoding => 作者字符串是否用UTF-16BE编码
# visible => 是否设置开启excel时批注可见。1为可见;0为不可见
# x_scale => 设置批注框宽度的压缩比
# width => 设置批注框宽度
# y_scale => 设置批注框高度的压缩比
# height => 设置批注框高度
# color => 设置批注框背景色
# start_cell => 设置批注框出现的位置,如“E2”
# start_row => 设置批注框出现的行位置
# start_col => 设置批注框出现的列位置

$worksheet->write_comment('C3', 'Hello', visible => 1, author => 'Perl');
insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)

在 sheet 中插入一个 png、jpeg 或 bmp 格式的图片, $x$y 表示在单元格中图片靠左和靠上的距离, $scale_x$scale_y 表示缩放比例,如下:

1
$worksheet->insert_image('A1', 'perl.bmp', 0, 0, 2, 0.8);
insert_chart($row, $col, $chart, $x, $y, $scale_x, $scale_y)

插入表格,详情请移步 Spreadsheet::WriteExcel::Chart

data_validation()

下拉选项,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#仅能输入大于100的数字
$worksheet->data_validation('B3',
{
validate => 'integer',
criteria => '>',
value => 100,
});

#在下拉选项中选择值
$worksheet->data_validation('B5:B9',
{
validate => 'list',
value => ['open', 'high', 'close'],
});
get_name()

获取 sheet 表的名称,如下:

1
2
3
foreach my $sheet ($workbook->sheets()) {
print $sheet->get_name();
}
activate()

设置默认展示的 sheet 表,如下:

1
2
3
4
5
$worksheet1 = $workbook->add_worksheet('To');
$worksheet2 = $workbook->add_worksheet('the');
$worksheet3 = $workbook->add_worksheet('wind');

$worksheet3->activate(); #默认展示wind表
select()

选中 sheet 表,如下:

1
2
3
$worksheet1->activate();
$worksheet2->select();
$worksheet3->select();
hide()

隐藏 sheet 表,如下:

1
$worksheet2->hide();
set_first_sheet()

设置为第一个 sheet,如下:

1
2
3
4
5
6
7
8
9
for (1..20) {
$workbook->add_worksheet;
}

$worksheet21 = $workbook->add_worksheet();
$worksheet22 = $workbook->add_worksheet();

$worksheet21->set_first_sheet(); #设置为第一个sheet
$worksheet22->activate();
protect($password)

开启保护,密码可选,如下:

1
2
3
$worksheet->protect();
or
$worksheet->protect('drowssap');
set_selection($first_row, $first_col, $last_row, $last_col)

选中指定区域,如下:

1
2
3
4
5
6
$worksheet1->set_selection(3, 3);       # 1. Cell D4.
$worksheet2->set_selection(3, 3, 6, 6); # 2. Cells D4 to G7.
$worksheet3->set_selection(6, 6, 3, 3); # 3. Cells G7 to D4.
$worksheet4->set_selection('D4'); # Same as 1.
$worksheet5->set_selection('D4:G7'); # Same as 2.
$worksheet6->set_selection('G7:D4'); # Same as 3.
set_row($row, $height, $format, $hidden, $level, $collapsed)

设置行属性,如下:

1
$worksheet->set_row(0, 20); # 设置第一行的行高为20
set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed)

设置列属性,同上

merge_range($first_row, $first_col, $last_row, $last_col, $token, $format, $utf_16_be)

合并单元格,如下:

1
2
3
4
5
6
7
my $format = $workbook->add_format(
border => 6,
valign => 'vcenter',
align => 'center',
);

$worksheet->merge_range('B3:D4', 'Vertical and horizontal', $format);
set_zoom($scale)

设置 zoom 属性

right_to_left()

颠倒 A1 的位置

hide_zero()

隐藏为 0 的值,如下:

1
$worksheet->hide_zero();
set_tab_color()

设置 sheet Tab 的颜色,如下:

1
2
$worksheet1->set_tab_color('red');
$worksheet2->set_tab_color(0x0C);
autofilter($first_row, $first_col, $last_row, $last_col)

自动筛选,如下:

1
2
$worksheet->autofilter(0, 0, 10, 3);
$worksheet->autofilter('A1:D11'); # Same as above in A1 notation.
filter_column($column, $expression)

设置筛选条件,如下:

1
2
$worksheet->filter_column('A', 'x > 2000');
$worksheet->filter_column('B', 'x > 2000 and x < 5000');

运算符如下:

1
2
3
4
5
6
7
8
9
10
运算符         近义词
== = eq =~
!= <> ne !=
>
<
>=
<=

and &&
or ||

* 表示一个或多个字符, 表示一个字符

1
2
3
4
5
6
7
8
9
10
11
12
'x == Blanks'     #空白
'x == NonBlanks' #不为空白
'Top 10 Items' #前10个
'Bottom 5 Items' #后5个
'Top 25 %' #前25%
'Bottom 50 %' #后50%
'x =~ b*' # 以b开头
'x !~ b*' # 不以b开头
'x =~ *b' # 以b结尾
'x !~ *b' # 不以b结尾
'x =~ *b*' # 包含b
'x !~ *b*' # 不包含b

format

set_format_properties(%properties)

设置单元格属性,如下:

1
2
my $format = $workbook->add_format();
$format->set_format_properties(bold => 1, color => 'red');
set_font($fontname)

设置字体,默认为 Arial ,如下:

1
$format->set_font('Times New Roman');
set_size()

设置字体大小,默认大小为 10 ,如下:

1
2
my $format = $workbook->add_format();
$format->set_size(30);
set_color()

设置字体颜色,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 'black'
# 'blue'
# 'brown'
# 'cyan'
# 'gray'
# 'green'
# 'lime'
# 'magenta'
# 'navy'
# 'orange'
# 'pink'
# 'purple'
# 'red'
# 'silver'
# 'white'
# 'yellow'

my $format = $workbook->add_format();
$format->set_color('red');
$worksheet->write(0, 0, 'wheelbarrow', $format);
set_bold()

是否开启加粗。一般情况下可以添加 100-1000 的参数。400 表示正常,700 表示加粗,1000 表示非常粗。但最好是不写,如下:

1
$format->set_bold(); #加粗
set_italic()

设置斜体,如下:

1
$format->set_italic();
set_underline()

设置下划线,如下:

1
2
3
4
$format->set_underline(0); #取消下划线
$format->set_underline(); #设置下划线
$format->set_underline(1); #设置下划线
$format->set_underline(2); #设置双下划线
set_font_strikeout()

设置删除线

set_font_script()

设置角标,如下:

1
2
3
0 => 正常
1 => 上角标
2 => 下角标
set_num_format()

设置数字格式,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$format01->set_num_format('0.000');
$worksheet->write(0, 0, 3.1415926, $format01); # 3.142

$format02->set_num_format('#,##0');
$worksheet->write(1, 0, 1234.56, $format02); # 1,235

$format03->set_num_format('#,##0.00');
$worksheet->write(2, 0, 1234.56, $format03); # 1,234.56

$format04->set_num_format('$0.00');
$worksheet->write(3, 0, 49.99, $format04); # $49.99

# Note you can use other currency symbols such as the pound or yen as well.
# Other currencies may require the use of Unicode.

$format07->set_num_format('mm/dd/yy');
$worksheet->write(6, 0, 36892.521, $format07); # 01/01/01

$format08->set_num_format('mmm d yyyy');
$worksheet->write(7, 0, 36892.521, $format08); # Jan 1 2001

$format09->set_num_format('d mmmm yyyy');
$worksheet->write(8, 0, 36892.521, $format09); # 1 January 2001

$format10->set_num_format('dd/mm/yyyy hh:mm AM/PM');
$worksheet->write(9, 0, 36892.521, $format10); # 01/01/2001 12:30 AM

$format11->set_num_format('0 "dollar and" .00 "cents"');
$worksheet->write(10, 0, 1.87, $format11); # 1 dollar and .87 cents

# Conditional formatting
$format12->set_num_format('[Green]General;[Red]-General;General');
$worksheet->write(11, 0, 123, $format12); # > 0 Green
$worksheet->write(12, 0, -45, $format12); # < 0 Red
$worksheet->write(13, 0, 0, $format12); # = 0 Default colour

# Zip code
$format13->set_num_format('00000');
$worksheet->write(14, 0, '01209', $format13);
set_locked()

是否上锁,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
my $locked  = $workbook->add_format();
$locked->set_locked(1); # A non-op

my $unlocked = $workbook->add_format();
$locked->set_locked(0);

# Enable worksheet protection
$worksheet->protect();

# This cell cannot be edited.
$worksheet->write('A1', '=1+2', $locked);

# This cell can be edited.
$worksheet->write('A2', '=1+2', $unlocked);
set_hidden()

是否隐藏,如下:

1
2
3
4
5
6
7
8
my $hidden = $workbook->add_format();
$hidden->set_hidden();

# Enable worksheet protection
$worksheet->protect();

# The formula in this cell isn't visible
$worksheet->write('A1', '=1+2', $hidden);
set_align()

设置对齐方式,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Horizontal
# 'left'
# 'center'
# 'right'
# 'fill'
# 'justify'
# 'center_across'

Vertical
# 'top'
# 'vcenter'
# 'bottom'
# 'vjustify'
set_bg_color()

设置背景色

set_fg_color()

设置前景色

set_border()

设置边框,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Index   Name            Weight   Style
===== ============= ====== ===========
0 None 0
1 Continuous 1 -----------
2 Continuous 2 -----------
3 Dash 1 - - - - - -
4 Dot 1 . . . . . .
5 Continuous 3 -----------
6 Double 3 ===========
7 Continuous 0 -----------
8 Dash 2 - - - - - -
9 Dash Dot 1 - . - . - .
10 Dash Dot 2 - . - . - .
11 Dash Dot Dot 1 - . . - . .
12 Dash Dot Dot 2 - . . - . .
13 SlantDash Dot 2 / - . / - .
set_border_color()

设置边框颜色

copy($format)

格式刷

更多详情请移步 Spreadsheet::WriteExcel 的官方资料


如果这篇文章对你有帮助,或者想给我微小的工作一点点资瓷,请随意打赏。
潘高 微信支付

微信支付

潘高 支付宝

支付宝


perl处理Excel(跨平台)
https://blog.pangao.vip/perl处理Excel(跨平台)/
作者
潘高
发布于
2019年3月28日 晚上
更新于
2023年4月25日 下午
许可协议