2012-05-10 83 views
2

我有一個Excel文件,其中有50個問題,我想包括一個java小程序中的調查問卷。這個調查問卷的答案將存儲在我創建的MySQL數據庫中。從Excel導出到MySQL

有沒有什麼辦法可以從我的Excel文件中選擇所有問題(或子集),並將它們放入MySQL表中的字段中(我選擇)?

我環顧四周,加載數據infile聽起來像一個可行的選項,但我想選擇哪些問題,以及在哪裏放置它們。任何人都可以將我指向正確的方向嗎?或者我應該簡單地複製和粘貼?

任何幫助將是偉大的!

回答

0

查看Excel將數據保存爲CSV文件,然後將其加載到MySQL。根據MySQL結構嘗試在Excel中格式化數據。

0
# 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 
0

對我來說,最簡單的方法是打開的Excel與MS Access文件(你可以做到這一點直接:文件 - >打開 - > ...),並與ODBC導出數據到MySQL(右鍵單擊表格 - >導出 - > ODBC數據庫。)

順便說一下,我用「MySQL for Excel」試驗了一個bug,輸出只有999行。理論上,它會用下一個1.3.4版本解決。