通过Perl将数据从XLSX插入数据库

发布于 2025-02-12 23:17:27 字数 1433 浏览 1 评论 0 原文

我需要将数据从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;

I need to insert data from xlsx to database and this code work only do 2 rows not for all data in file and i need to skip first line cmd told me that I inserting 4 values but 3 is needed but my excel have 3 rowsenter image description here

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-02-19 23:17:27

如果您阅读 for drace documentation for dreversheet :: parseexcel :: parseexcel

电子表格:: Parseexcel模块可用于从Excel 95-2003二进制文件中读取信息。

模块无法在Excel 2007打开XML XLSX格式中读取文件。请参阅 evendersheet :: xlsx module。

电子表格的文档:: xlsx说:

此模块使用正则XML的方式存在一些严重的问题。我强烈鼓励切换到 vendersheet :: parsexlsx 采取更可靠的方法。 P>

因此,您需要切换到可以解析您使用的电子表格的模块。

If you read the documentation for Spreadsheet::ParseExcel, you'll see it says:

The Spreadsheet::ParseExcel module can be used to read information from Excel 95-2003 binary files.

The module cannot read files in the Excel 2007 Open XML XLSX format. See the Spreadsheet::XLSX module instead.

And the documentation for Spreadsheet::XLSX says:

This module has some serious issues with the way it uses regexs for parsing the XML. I would strongly encourage switching to Spreadsheet::ParseXLSX which takes a more reliable approach.

So you need to switch to a module that can parse the spreadsheets that you're using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文