通过Perl将数据从XLSX插入数据库
我需要将数据从XLSX插入到数据库中,并且此代码工作只能对文件中的所有数据进行2行,我需要跳过第一行CMD告诉我,我需要4个值,但需要3个值,但我的Excel有3行在此处输入图像描述
#!/usr/local/bin/perl
use strict;
use warnings;
use diagnostics;
use Spreadsheet::ParseXLSX;
use DBI;
use Data::Dumper qw(Dumper);
my $parser = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse('test.xlsx');
my $dbh = DBI->connect("DBI:mysql:pokus:localhost", "root", "", { RaiseError => 1}) or die $DBI::errstr;
my $query = 'INSERT INTO soucastky (id, name, age, city) VALUES (?,?,?,?)';
my $sth = $dbh->prepare($query) or die "Prepare failed: " . $dbh->errstr();
if ( !defined $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 ) {
my @values;
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
push @values, $cell->value();
}
$sth->execute(@values) or die $dbh->errstr;
}
}
open my $fh, "<", "test" or die $!;
while (<$fh>)
{
chomp;
my @vals = split;
$sth->execute(@vals);
}
close $fh;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读 for drace documentation for dreversheet :: parseexcel :: parseexcel
电子表格的文档:: xlsx说:
因此,您需要切换到可以解析您使用的电子表格的模块。
If you read the documentation for Spreadsheet::ParseExcel, you'll see it says:
And the documentation for Spreadsheet::XLSX says:
So you need to switch to a module that can parse the spreadsheets that you're using.