我一直在努力将仪器生成的 *.xls 文件转换为 Linux 服务器上的文本格式。我无法使用 Spreadsheet::ParseExcel 处理这些文件,除非我手动打开它们,在安全警告上签字并保存它们。否则,它们不会被识别为 Excel(使用示例代码测试)。
!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $file = "/data/excel/matrix.xls";
my $workbook = $parser->parse($file);
if ( !defined $workbook ) {
print "can't find workbook!!!";
die $parser->error(), ".\n";
}
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
print "Row, Col = ($row, $col)\n";
print "Value = ", $cell->value(), "\n";
print "Unformatted = ", $cell->unformatted(), "\n";
print "\n";
}
}
}
我已经尝试将扩展名更改为 *.prn,它允许我在没有警告的情况下手动打开文件,但 Spreadsheet::ParseExcel 也无法识别它们。
文件仅在第一张纸上包含 8 列数据。我想将它们转换为文本文件并使用它们在我的 Perl 脚本中查找值。这是 excel 中的一些示例数据:
Gene Target Barcode1 Barcode2 Barcode3 Barcode4 Barcode5 Barcode6
MOTOR MOTOR_1 343 453 432 345 543 342
MYCN MYCN_2 342 98 87 876 54 765
我最后的选择是使用 VBA,但如果可能的话,我希望读者坚持使用 Perl/Shell 代码。这个问题有直接的解决方案吗?
谢谢,
最佳答案
它不是特别优雅,但您可以尝试使用 Linux 命令“strings”先从电子表格文件中提取可打印字符。然后您可以解析输出,直到您看到列标题,数据应该在列标题之后。
关于linux - 在 Linux 服务器上将 Excel 提取为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40661300/