This script is a tool that uses Perl to generate excelxls files. Depending on some modules, you can use it in linux to generate xls files. The command line + parameter is used. Very convenient. #! Usrbinperl # mysqltoexcel # lastmodifyat2005-1-5 # copyrightbyhoowa # mysql management tools
This script uses Perl to generate excel xls files. Depending on some modules, you can use it in linux to generate xls files. The command line + parameter is used. Very convenient.
#! /Usr/bin/perl
#====================================
# Mysql to excel
# Lastmodify at 2005-1-5
# Copyright by hoowa
#==================================
Use strict; # strict syntax check
Use DBI; # Database Engine
Use Unicode: Map; # Unicode engine
# Use Spreadsheet: WriteExcel; # Excel report engine
Use Spreadsheet: WriteExcel: Big; # large file Excel report engine
My $ hostname = '2014. 168.1.20 ';
My $ username = 'user ';
My $ password = 'pass ';
My $ dbname = 'DB ';
My $ trans_compress = 1; # Transfer and compression between the database and the program from a non-zero number
$ | = 1;
My @ cols = ('A: A', 'B: B', 'C: C', 'd: d', 'E: E', 'f: f', 'G: G', 'H: H', 'I: I', 'J: J ',
'K: K', 'L: l', 'M: M', 'n': N', 'O: O', 'P: p', 'Q: q', 'R: R','s: S', 'T: t', 'U: U ',
'V: V', 'W: w', 'X: X', 'Y: Y', 'Z: Z', 'AA: A', 'BB: B ', 'CC: C', 'dd: d', 'ee: E ',
'FF: f', 'GG: G', 'hh: H', 'II: I ', 'JJ: J', 'KK: K','ll: l', 'Mm: M', 'nn: N ',
'Oo: O', 'pp: p', 'qq: q', 'rr: R', 'SS: S', 'TT: t', 'uu: u', 'vv: V', 'WW: w ',
'XX: X', 'YY: Y', 'zz: Z ');
# Parsed content
If ($ # ARGV! = '1 '){
Print qq ~ Syntax: my2excel. pl "[Where expression]"
~;
Exit;
}
$ ARGV [1] = ~ S/\ "// g;
Warn qq ~
Mysql to excel
By hoowa. sun
==================================
SQL: $ ARGV [1]
~;
My $ dbh =
DBI-> connect ("DBI: mysql: mysql_compression = $ trans_compress;
Database = $ dbname; host = $ hostname ", $ username, $ password );
My $ Something = $ dbh-> prepare ("$ ARGV [1]") | die $ dbh-> errstr;
My $ rows = $ Something-> execute () or die $ Something-> errstr;
Warn "rows: $ rows found. \ n ";
My @ cols_name =@{$ something-> {'name '}};
If ($ # cols_name >$ # cols ){
Print "table $ ARGV [1] fields out of allow !! (Max num.> ". ($ # cols + 1).") \ n ";
Exit;
}
Warn "write to: $ ARGV [0] \ n ";
# Generate a GB2312 encoding system
My $ map = Unicode: Map-> new ("gb2312 ");
# Generate reports
My $ report = Spreadsheet: WriteExcel: Big-> new ("$ ARGV [0]") | die "cannot generate report files: $! ";
# Create a Report Worksheet
My $ sheet = $ report-> add_worksheet ('data _ Report ');
# Create a format
My $ title_style = $ report-> add_format (); $ title_style-> set_size (11); $ title_style-> set_bold (); $ title_style-> set_align ('center ');
# Initialize data pointer
My $ sheet_col = 0;
# Create a table
For (my $ I = 0; $ I <=$ # cols_name; $ I ++ ){
$ Sheet-> set_column ($ cols [$ I], length ($ cols_name [$ I]) + 4 );
$ Sheet-> write_unicode ($ sheet_col, $ I, $ map-> to_unicode ($ cols_name [$ I]), $ title_style );
}
$ Sheet-> freeze_panes (1, 0); # freeze rows
While (my @ row = $ TH-> fetchrow_array ){
$ Sheet_col ++;
For (my $ I = 0; $ I <=$ # cols_name; $ I ++ ){
Next if ($ row [$ I] eq '');
$ Sheet-> write_unicode ($ sheet_col, $ I, $ map-> to_unicode ($ row [$ I]);
}
}
Warn "all done !!! \ N ";
# End
END {
$ Report-> close () if ($ report );
$ Dbh-> disconnect ();
}