【问题标题】:Exporting from Excel to MySQL从 Excel 导出到 MySQL
【发布时间】:2012-05-10 09:50:15
【问题描述】:

我有一个 Excel 文件,其中有 50 个问题,我想在 Java 小程序中包含问卷调查。此问卷的答案将存储在我创建的 MySQL 数据库中。

有什么方法可以从我的 Excel 文件中选择所有(或部分)问题并将它们放入 MySQL 表中的字段(我选择)中?

我环顾四周并在文件中加载数据听起来像是一个可行的选项,但我想选择哪些问题以及放置它们的位置。谁能指出我正确的方向?还是我应该简单地复制和粘贴?

任何帮助都会很棒!

【问题讨论】:

    标签: mysql sql excel


    【解决方案1】:

    将您的 Excel 数据保存在 CSV format 中,然后使用 LOAD DATA 命令将其导入 MySQL。

    关于后者的信息,例如,可在此处找到:

    【讨论】:

      【解决方案2】:

      查看 Excel,将数据保存为 CSV 文件,然后将其加载到 MySQL。尝试根据 MySQL 结构在 Excel 中格式化数据。

      【讨论】:

        【解决方案3】:

        这现在可能看起来有点老了,但会帮助任何其他寻找解决方案的人。

        Excel 有官方的 MySQL 插件,可以在 Excel 中导出、导入、追加和编辑 MySQL 数据。

        这里是链接:http://dev.mysql.com/doc/refman/5.6/en/mysql-for-excel.html

        【讨论】:

        • 找不到页面。
        【解决方案4】:
        # wanna be generic implementation of xls to mysql upsert in perl
        # by this time you should have your mysql connection open ... 
        use Spreadsheet::XLSX;
        use Text::Iconv;
        
        
        
        #
        # -----------------------------------------------------------------------------
        # runs the insert sql by passed data part 
        # by convention is assumed that the first column is unique and update could 
        # be performed on it ... should there be duplicates the update should fail
        # -----------------------------------------------------------------------------
        sub RunUpsertSql {
        
           my $self             = shift ; 
           my $table_name   = shift ; 
           my $refHeaders   = shift ; 
           my $refData      = shift ; 
           my $data_str         = '' ; 
           my @headers      = @$refHeaders ; 
           my @data             = @$refData ; 
        
           print ( "\@data : @data" ) ; 
           print ( "\@headers: @headers" ) ; 
        
           my $sql_str = " INSERT INTO $table_name " ; 
           $sql_str .= '(' ; 
           for ( $i=0; $i<scalar (@headers);$i++ ) {
              $sql_str .= " $headers[$i] " . ' , ' ; 
        
           } #eof for
        
           for (1..3) { chop ( $sql_str) } ; 
           $sql_str .= ')' ; 
        
           foreach my $cellValue ( @data ) {
              # replace the ' chars with \'
              $cellValue        =~ s|\'|\\\'|g ; 
              $data_str .= "'" . "$cellValue" . "' , " ; 
           }
           #eof foreach ' replacement
        
           # remove the " , " at the end 
           for (1..3) { chop ( $data_str ) } ; 
        
           $sql_str .=  " VALUES (" . "$data_str" . ')' ; 
           $sql_str .= ' ON DUPLICATE KEY UPDATE ' ; 
        
           for ( $i=0; $i<scalar(@headers);$i++ ) {
              $sql_str .= "$headers[$i]" . ' = ' . "'" . "$data[$i]" . "' , " ; 
           } #eof for
        
           for (1..3) { chop ( $sql_str) } ; 
        
           print ( "sql_str : $sql_str " ); 
        
           $sth = $dbh->prepare($sql_str ) ; 
           $sth->execute( );
        
        }
        #eof sub RunUpsertSql
        
        
        #
        # -----------------------------------------------------------------------------
        # walk trough the Excel and build the data part of the insert sql
        # -----------------------------------------------------------------------------
        sub ParseExcel {
        
           my $self = shift ; 
           print (  " == START == " ) ; 
           # not sure if it could work without the next line
           # for utf8 strings - slavic , japanese etc. 
           my $converter = Text::Iconv -> new ("utf-8", "utf-8");
        
           # http://search.cpan.org/~dmow/Spreadsheet-XLSX-0.13-withoutworldwriteables/lib/Spreadsheet/XLSX.pm
           my $objExcelParser = Spreadsheet::XLSX -> new ("$FileInputExcel", $converter);
        
           # iterate the sheets
           foreach my $objSheet (@{$objExcelParser-> {Worksheet}}) {
        
              print("Sheet: " . $objSheet->{'Name'});
        
              my $rowCount = 0 ; 
              # iterate the rows 
              my @headerData                    = ();
              foreach my $row ($objSheet -> {'MinRow'} .. $objSheet -> {'MaxRow'}) {
                 my @rowData                    = (); 
                 $objSheet -> {'MaxCol'} ||= $objSheet -> {'MinCol'};
        
                 # iterate the coloumns
                 foreach my $col ($objSheet -> {'MinCol'} ..  $objSheet -> {'MaxCol'}) {
                    my $cell = $objSheet -> {'Cells'} [$row] [$col];
                    if ($cell) {
                       #debug printf("( %s , %s ) => %s\n", $row, $col, $cell -> {'Val'});
                       # the unformatted value
                       #my $cellValue = $cell->{'Val'}  ; 
                       # push the formatted value
                       push ( @rowData , $cell->value() )       if $rowCount != 0 ; 
                       push ( @headerData , $cell->value() )    if $rowCount == 0 ; 
        
                    }  #eof if the cell is defined
                 } 
                 #eof foreach col
              # by convention the name of the xls sheet is the same as the table name
              $self->RunUpsertSql ( $objSheet->{'Name'} , \@headerData , \@rowData) 
                 if $rowCount != 0 ; 
        
                 $rowCount++ ; 
              }
              #eof foreach row
        
           } 
           #eof foreach $objSheet
        
           print (  " == STOP  == " ) ; 
        
        } #eof sub ParseExcel
        

        【讨论】:

          【解决方案5】:

          对我来说最简单的方法是使用 MS Access 打开 Excel 文件(您可以直接打开:文件 -> 打开 ->...)并使用 ODBC 将数据导出到 MySQL(右键单击表 -> 导出-> ODBC 数据库。

          顺便说一句,我用“MySQL for Excel”试验了一个bug,输出只有999行。理论上会在下个1.3.4版本解决。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-16
            • 1970-01-01
            • 2010-11-01
            • 1970-01-01
            • 2011-10-04
            • 1970-01-01
            相关资源
            最近更新 更多